跨域403问题

前后端分离的跨域问题(403)

前后端分离制作web开发经常会碰见报错403问题,以下是解决方案

  1. 在后端的解决方案

在后端代码中添加config文件解决
将下面代码复制到springboot的config包中解决

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 处理跨域问题
 * 403错误的处理
 */
@Configuration
public class CroConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}

  1. 在前端解决

可参考网址https://www.jianshu.com/p/a65c7abcb59e


版权声明:本文为MengHen_0_0原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。