0


SpringBoot跨域配置

什么是跨域

简单而言,跨域请求就是当一台服务器资源从另一台服务器(不同 的域名或者端口)请求一个资源或者接口,就会发起一个跨域 HTTP 请求。举个简单的例子,从http://www.baidu.com,发送一个 Ajax 请求,请求地址是 http://www.taobao.com下面的一个接口,这就是发起了一个跨域请求,在不做任何处理的情况下,显然当前跨域请求是无法被成功请求,因为浏览器基于同源策略会对跨域请求做一定的限制。

产生跨域问题的条件

例如: http://192.168.38.438:8080

当协议、IP、端口三部分中有任意一个不同时,即为跨域

后端结局方案

  • nginx反向代理解决跨域
  • 服务端设置Response Header(响应头部)的Access-Control-Allow-Origin
  • 在需要跨域访问的类和方法中设置允许跨域访问(如Spring中使用@CrossOrigin注解);
  • 继承使用Spring Web的CorsFilter(适用于Spring MVC、Spring Boot)
  • 实现WebMvcConfigurer接口(适用于Spring Boot)

一、使用Filter方式进行设置

使用Filter过滤器来过滤服务请求,向请求端设置Response Header(响应头部)的Access-Control-Allow-Origin属性声明允许跨域访问。

@WebFilterpublicclassCorsFilterimplementsFilter{@OverridepublicvoiddoFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {  
        HttpServletResponse response =(HttpServletResponse) res;  
        response.setHeader("Access-Control-Allow-Origin","*");  
        response.setHeader("Access-Control-Allow-Methods","*");  
        response.setHeader("Access-Control-Max-Age","3600");  
        response.setHeader("Access-Control-Allow-Headers","*");
        response.setHeader("Access-Control-Allow-Credentials","true");
        chain.doFilter(req, res);}}

或者

package com.wk.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter;@ConfigurationpublicclassCorsConfig{// 当前跨域请求最大有效时长。这里默认1天privatestaticfinallong MAX_AGE =24*60*60;@Beanpublic CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source =newUrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration =newCorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");// 1 设置访问源地址
        corsConfiguration.addAllowedHeader("*");// 2 设置访问源请求头
        corsConfiguration.addAllowedMethod("*");// 3 设置访问源请求方法
        corsConfiguration.setMaxAge(MAX_AGE);
        source.registerCorsConfiguration("/**", corsConfiguration);// 4 对接口配置跨域设置returnnewCorsFilter(source);}}

二、继承 HandlerInterceptorAdapter

@ComponentpublicclassCrossInterceptorextendsHandlerInterceptorAdapter{@OverridepublicbooleanpreHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Max-Age","3600");
        response.setHeader("Access-Control-Allow-Headers","*");
        response.setHeader("Access-Control-Allow-Credentials","true");returntrue;}}

三、实现 WebMvcConfigurer

@Configuration@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")publicclassAppConfigimplementsWebMvcConfigurer{@OverridepublicvoidaddCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")// 拦截所有的请求.allowedOrigins("http://www.abc.com")// 可跨域的域名,可以为 *.allowCredentials(true).allowedMethods("*")// 允许跨域的方法,可以单独配置
                .allowedHeaders("*");// 允许跨域的请求头,可以单独配置
    }}

四、使用Nginx配置

location / {
   add_header Access-Control-Allow-Origin *;
   add_header Access-Control-Allow-Headers X-Requested-With;
   add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;if($request_method='OPTIONS'){return 204;}}

参考

标签: java spring boot 后端

本文转载自: https://blog.csdn.net/weixin_45977186/article/details/123601258
版权归原作者 野渡无人舟自横(风) 所有, 如有侵权,请联系我们删除。

“SpringBoot跨域配置”的评论:

还没有评论