Spring Boot 3 + Oauth2 资源服务器:CORS

问题描述 投票:0回答:1

我正在尝试将新的 Spring boot 2.7.4 应用程序迁移到 Spring boot 3(.0.4)。

我使用依赖项“spring-boot-starter-oauth2-resource-server”。

我的安全配置:

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

http
.cors()
.configurationSource(request -> {
    var cors = new CorsConfiguration();
    cors.setAllowedOrigins(List.of("http://localhost:8080"));
    cors.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
    cors.setAllowedHeaders(List.of("*"));
    cors.setAllowCredentials(true);
    return cors;
}).and()
.authorizeHttpRequests(authorizeRequests ->
        authorizeRequests
                .anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2ResourceServer ->
        oauth2ResourceServer
                .jwt().jwtAuthenticationConverter(getJwtAuthenticationConverter())
)
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    return http.build();
}

应用程序.yml

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          jws-algorithms: RS512
          issuer-uri: https://xxxxx.xxxxx.net
          jwk-set-uri: https://xxxxx.xxxxx.net/ext/oauth/JWKS

它与 Spring boot 2.7.4 和 VueJS(Vue 3 + Axios)配合得很好,我没有 CORS 问题。

但是如果我切换到 spring boot 3.0.4,它不起作用,我有这个错误:

CORS Error

我尝试按照 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide 首先使用 spring boot 更新到 Spring Security 5.8 及其工作,但尽快当我切换到 spring boot 3 时,它不起作用。

有人知道这个问题吗?提前致谢!

spring-boot cors spring-security-oauth2 spring-boot-3
1个回答
0
投票

好吧......配置根本没有被使用......我不知道为什么但我需要在我的类上添加@Configuration而Spring Boot 2不需要它

来自 :

@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfiguration {

到:

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfiguration {
© www.soinside.com 2019 - 2024. All rights reserved.