반응형
Recent Posts
Recent Comments
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Real Vectorism. 훨씬 더 입체적으로...

스프링 웹플럭스에서 CSRF 토큰 때문에 페이지 요청이 모두 막히는 경우 본문

Java (based by Spring Boot 2)

스프링 웹플럭스에서 CSRF 토큰 때문에 페이지 요청이 모두 막히는 경우

grast 2023. 7. 26. 00:21
반응형

예시 소스코드

@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class WebSecurityConfig {
    private final AuthenticationManager authenticationManager;
    private final SecurityContextRepository securityContextRepository;

    public webSecurityConfig(AuthenticationManager authenticationManager, SecurityContextRepository securityContextRepository) {
        this.authenticationManager = authenticationManager;
        this.securityContextRepository = securityContextRepository;
    }

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        return http
                .exceptionHandling(exceptionHandlingSpec -> exceptionHandlingSpec
                        .authenticationEntryPoint((exchange, ex) -> Mono.fromRunnable(() -> exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED)))
                        .accessDeniedHandler((exchange, denied) -> Mono.fromRunnable(() -> exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND)))
                )
                .csrf(ServerHttpSecurity.CsrfSpec::disable)
                .formLogin(ServerHttpSecurity.FormLoginSpec::disable)
                .httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
                .authenticationManager(authenticationManager)
                .securityContextRepository(securityContextRepository)
                .authorizeExchange(authorizeExchangeSpec -> authorizeExchangeSpec
                        .pathMatchers(HttpMethod.OPTIONS).permitAll()
                        .pathMatchers(HttpMethod.GET).denyAll()
                        .pathMatchers(HttpMethod.POST, "/user/login").permitAll()
                        .pathMatchers(HttpMethod.POST, "/**").authenticated()
                        .anyExchange().denyAll()
                )
                .build();
    }
}

 

웹플럭스 시큐리티를 다음과 같이 설정했는데도 /user/login 이 전혀 통하지 않고 다음 둘 중 한가지의 에러가 발생한다면

An expected CSRF token cannot be found
또는
Invalid Csrf Token

 

 

그냥 클래스 위에 어노테이션 하나 더 추가해주면 된다.

@Configuration

 

Spring Security 5 에서 Spring Security 6 으로 넘어가는 과정에서 @Configuration 이 빠졌다는 그런 내용인가보다. 정확한 내용은 공식페이지에서 번역기 돌려봐야겠지만 공식 doc 보다도 마이그레이션 관련 문서가 가장 읽기 귀찮은건 아무래도 몹쓸 병인것 같다.

반응형
Comments