Spring webflux 与 jwt + csrf 令牌

wth*_*ira 1 csrf spring-security vue.js spring-webflux

我需要对我的后端实施 CSRF 保护。我正在使用以下配置。但应用程序允许没有 CSRF 令牌的 Post 和 Get 请求。

@Slf4j
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SecurityConfig {

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

在 HTTP 请求中包含实际的 CSRF 令牌

 @ControllerAdvice
    public class SecurityControllerAdvice {
        @ModelAttribute
        Mono<CsrfToken> csrfToken(ServerWebExchange exchange) {
            Mono<CsrfToken> csrfToken = exchange.getAttribute(CsrfToken.class.getName());
            return csrfToken.doOnSuccess(token -> {
                exchange.getAttributes()
                        .put(CsrfRequestDataValueProcessor.DEFAULT_CSRF_ATTR_NAME, token);
            });
        }
    }
Run Code Online (Sandbox Code Playgroud)

我尝试使用邮递员的API。但这对我不起作用。

春季版

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/>
   </parent> 
Run Code Online (Sandbox Code Playgroud)

依赖项:

<dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-resource-server</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-jose</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

最终目标:我的前端是Vuejs + nuxtjs。有人可以帮助我找到实现此目的的最佳方法吗?

Mar*_*gio 5

通过使用oauth2ResourceServer()DSL,您告诉Spring Security您没有使用基于cookie的身份验证,因此您不需要CSRF保护。

如果您查看一下,OAuth2ResourceServerConfigurer#registerDefaultCsrfOverride您会发现它不会通过Bearer使用BearerTokenRequestMatcher.

private void registerDefaultCsrfOverride(H http) {
    CsrfConfigurer<H> csrf = http.getConfigurer(CsrfConfigurer.class);
    if (csrf != null) {
        csrf.ignoringRequestMatchers(this.requestMatcher);
    }
}
Run Code Online (Sandbox Code Playgroud)

CSRF 利用自动附加请求 Cookie 的浏览器行为,因此我可以欺骗网站上的用户www.malicioussite.example单击按钮并向www.fakebank.example/transferMoneyToMe.

当您使用Authorization标头发送 JWT 时,浏览器不知道访问令牌,因此不会自动附加标头。

您可以从此答案开始更深入地研究此行为https://security.stackexchange.com/questions/189326/do-i-need-csrf-protection-in-this-setup-with-a-rest-api-支持 oauth2 和 a