1、跨域

1、简介

  • 跨域:指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制。
  • 同源策略:是指协议,域名,端口都要相同,其中有一个不同都会产生跨域;
  • image.png

    2、跨域流程

    文档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS
    image.png

    3、普通Spring-mvc项目解决方案

    1、使用nginx部署为同一域

    image.png

    2、配置当次请求允许跨域

    1、添加响应头

  • Access-Control-Allow-Origin: 支持哪些来源的请求跨域

  • Access-Control-Allow-Methods: 支持哪些方法跨域
  • Access-Control- Allow-Credentials:跨域请求默认不包含cookie,设置为true可以包含cookie
  • Access-Control-Expose-Headers: 跨域请求暴露的字段
    • CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段:Cache-Control、Content-Language、 Content-Type、 Expires、 Last-Modified、 Pragma。如果想拿到其他字段,就必须在Access-Control-Expose-Headers里面指定。
  • Access-Control-Max-Age: 表明该响应的有效时间为多少秒。在有效时间内,浏览器无须为同一请求再次发起预检请求。请注意,浏览器自身维护了一个最大有效时间,如果该首部字段的值超过了最大有效时间,将不会生效。
  • 示例代码Spring-boot中,可以配置在gatwall中 ```java package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration public class CorsConfiguration {

  1. @Bean
  2. public CorsWebFilter corsWebFilter(){
  3. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  4. CorsConfiguration corsConfiguration = new CorsConfiguration();
  5. //1、配置跨域
  6. corsConfiguration.addAllowedHeader("*");
  7. corsConfiguration.addAllowedMethod("*");
  8. corsConfiguration.addAllowedOrigin("*");
  9. corsConfiguration.setAllowCredentials(true);
  10. source.registerCorsConfiguration("/**",corsConfiguration);
  11. return new CorsWebFilter(source);
  12. }

}

<a name="d6iKK"></a>
## 4、Spring Security项目解决跨域
<a name="XE8S6"></a>
### 1、添加配置类
```java
package com.seat.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;

/**
 * @author djy
 * @createTime 2022/2/23 下午5:12
 * @description
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //1) 允许的域,不要写*,否则cookie就无法使用了
//    config.addAllowedOrigin("http://127.0.0.1:8080");
        config.addAllowedOrigin("*");
        //2) 是否发送Cookie信息
        config.setAllowCredentials(true);
        //3) 允许的请求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4)允许的头信息
        config.addAllowedHeader("*");
        //2.添加映射路径,我们拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new
                UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
}

2、在Spring Security中配置一下跨域

  • 以下是关键代码
          httpSecurity.csrf().disable();//关闭csrf防护
          httpSecurity.cors();//开启跨域
    
      @Override
      protected void configure(HttpSecurity httpSecurity) throws Exception {
          httpSecurity.csrf().disable();//关闭csrf防护
          httpSecurity.cors();//开启跨域
      }