Gateway中Spring Security6统一处理CORS

一、起因

使用了gateway微服务作为整体的网关,并且整合了Spring Security6;还有一个system微服务,作为被请求的资源,当浏览器向gateway发送请求,请求system资源时,遇到CORS问题。

于是我在system对应的controller上加了@CrossOrigin,无效;配置WebMvcConfigurer,也无效。
后来发现,会不会是gatewayspring security6在一开始就拦截了CORS跨域请求,导致根本走不到后面的system配置。

查询了一波,果然如此。这里记录解决方法。

二、解决方法

这是依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

这是配置

@EnableWebFluxSecurity
@Configuration
public class SecurityConfig {
   

    //安全拦截配置
    @Bean
    public SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception {
   
        return http
                .cors(cors -> cors.configurationSource(corsConfigurationSource()))
                .authorizeExchange(exchanges ->
                        exchanges
                                .pathMatchers("/**").permitAll()
                                .anyExchange().authenticated()
                )
                .oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()))
                .csrf(ServerHttpSecurity.CsrfSpec::disable)
                .build();
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
   
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.addAllowedOriginPattern("*"); // 允许任何源
        corsConfig.addAllowedMethod("*"); // 允许任何HTTP方法
        corsConfig.addAllowedHeader("*"); // 允许任何HTTP头
        corsConfig.setAllowCredentials(true); // 允许证书(cookies)
        corsConfig.setMaxAge(3600L); // 预检请求的缓存时间(秒)

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig); // 对所有路径应用这个配置
        return source;
    }
}

需要注意的是,在gatewayspring security中处理了CORS问题后,后续的system什么的,就不需要再二次处理了。因为CORS是一个浏览器的策略,只要处理一次,告诉浏览器我允许跨域,浏览器收到后就不再阻拦请求了。

相关推荐

  1. GatewaySpring Security6统一处理CORS

    2024-02-14 10:54:01       31 阅读
  2. Amazon API Gateway CORS 实战

    2024-02-14 10:54:01       32 阅读
  3. Amazon API Gateway CORS 实战

    2024-02-14 10:54:01       38 阅读
  4. Spring MVC 统一异常处理

    2024-02-14 10:54:01       17 阅读
  5. gateway对返回的数据进行处理

    2024-02-14 10:54:01       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-14 10:54:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-14 10:54:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-14 10:54:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-14 10:54:01       20 阅读

热门阅读

  1. 第一章 文档数据库 (DocDB) 简介

    2024-02-14 10:54:01       30 阅读
  2. Ajax 入门

    2024-02-14 10:54:01       35 阅读
  3. 796. 子矩阵的和

    2024-02-14 10:54:01       35 阅读
  4. λ-矩阵的多项式展开

    2024-02-14 10:54:01       31 阅读
  5. 数据库第三章作业-SQL语言

    2024-02-14 10:54:01       31 阅读
  6. 局部加权回归

    2024-02-14 10:54:01       31 阅读
  7. Day32 贪心算法part02

    2024-02-14 10:54:01       33 阅读
  8. c++ STL系列——(五)map

    2024-02-14 10:54:01       21 阅读