Spring - CORS 不适用于安全配置

Jok*_*ker 2 spring spring-security cors spring-webflux

我开发了一个带有 spring webflux 后端的角度应用程序。到目前为止,CorsFilter 运行良好并允许来自前端的请求。

然后我添加了一个SecurityConfig。从那以后 CorsFilter 停止工作,我在 angular 应用程序中遇到异常:

CORS 策略阻止了在“ http://localhost:8080/users/999/folders/%2F/media/ ”处访问 XMLHttpRequest来自源“ http://localhost:4200 ”:对预检请求的响应没有通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头

这个过滤器工作正常:

@Configuration
public class CorsFilter {

    private static final String FRONTEND_LOCALHOST = "http://localhost:4200";
    private static final String FRONTEND_STAGING = "https://somehost.github.io";

    @Bean
    CorsWebFilter corsWebFilter() {
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.applyPermitDefaultValues();
        corsConfig.addAllowedMethod(HttpMethod.PUT);
        corsConfig.addAllowedMethod(HttpMethod.DELETE);
        corsConfig.setAllowedOrigins(Arrays.asList(FRONTEND_LOCALHOST, FRONTEND_STAGING));

        UrlBasedCorsConfigurationSource source =
                new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig);

        return new CorsWebFilter(source);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我使用以下 SecurityConfig 添加了授权(不记名令牌):

@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SecurityConfiguration {

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        http.cors().and().csrf()
            .csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse())
            .and()
            .authorizeExchange()
            .anyExchange().authenticated()
            .and()
            .oauth2ResourceServer()
            .jwt();
        return http.build();
    }
Run Code Online (Sandbox Code Playgroud)

似乎在安全配置中,我的 CorsFilter 不再被考虑在内。我认为需要在配置中明确添加 corsfilter,但我发现的示例不起作用。我希望有人可以帮助并知道原因。

编辑:为了解决重复问题:我已经尝试将cors()和添加cors().configurationSource(corsConfig())到我的安全配置中,但也没有帮助。

Jok*_*ker 10

在 Spring 5 Webflux启用 CORS 中找到了一种工作方式吗?通过实现自定义 corsfilter。

但是,我仍然对解决方案不满意,因为它看起来很像一种解决方法。

最后我找到了罪魁祸首……一个很尴尬。我发布的 SecurityConfig 在错误的包中(它意外地在默认包中),因此配置没有被选中。

当我将代码粘贴到 securityconfig 时,问题中的 cors 过滤器也开始工作。

所以我最终的 SecurityConfig 看起来像这样:

import java.util.Arrays;

import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.csrf.CookieServerCsrfTokenRepository;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SecurityConfiguration {

    private static final String FRONTEND_LOCALHOST = "http://localhost:4200";
    private static final String FRONTEND_STAGING = "https://somehost.github.io";

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        http
                .csrf()
                .csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse())
                .and()
                .authorizeExchange()
                .anyExchange().authenticated()
                .and()
                .oauth2ResourceServer()
                .jwt();
        return http.build();
    }


    @Bean
    CorsConfigurationSource corsConfiguration() {
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.applyPermitDefaultValues();
        corsConfig.addAllowedMethod(HttpMethod.PUT);
        corsConfig.addAllowedMethod(HttpMethod.DELETE);
        corsConfig.setAllowedOrigins(Arrays.asList(FRONTEND_LOCALHOST, FRONTEND_STAGING));

        UrlBasedCorsConfigurationSource source =
                new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig);
        return source;
    }
}
Run Code Online (Sandbox Code Playgroud)