基于SpringBoot的跨域解决方案

一:问题描述
跨域问题指的是浏览器的同源策略导致页面访问服务器报错的一系列问题。而同源策略指的是以下方式:
DOM同源策略:禁止对不同源页面DOM进行操作。这里主要场景是iframe跨域的情况,不同域名的iframe是限制互相访问的。

XmlHttpRequest同源策略:禁止使用XHR对象向不同源的服务器地址发起HTTP请求。

二:解决方案
简单的解决方案请大家移步到博主的另一文章
解决浏览器跨域问题

然,博主这次讲的是后端基于SpringBoot的基础上的解决方案。

2.1、定义一个Interceptor拦截器
借用拦截器的preHandle方法,预先处理客户端的各种请求。

public class CrossInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        // 跨域资源共享( cors )
        String origin = httpServletRequest.getHeader("Origin");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", origin);
        //允许的方法
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
        //允许的头部参数
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin,Content-Type,Accept,X-os,X-uid,X-token,X-role,X-Requested-With");
        //用户代理是否应该在跨域请求的情况下从其他域发送cookies
        httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
        return true;
    }
}

2.2、拦截器注入配置

@Configuration
@EnableWebMvc
@ComponentScan("com.recruit")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //分页拦截器
        registry.addInterceptor(new PaginationInterceptor()).addPathPatterns("/**");
        //跨域拦截器
        registry.addInterceptor(new CrossInterceptor()).addPathPatterns("/**");
        //权限拦截器
        registry.addInterceptor(authenticationInterceptor()).addPathPatterns("/**");

        super.addInterceptors(registry);
    }
}

完!

猜你喜欢

转载自blog.csdn.net/ruben95001/article/details/80751513