복붙노트

[SPRING] WebFlux의 인증서로 인증 하시겠습니까?

SPRING

WebFlux의 인증서로 인증 하시겠습니까?

Spring Boot Web 용 정규 서블릿 API에는 HttpSecurity 구성의 .x509 ()가 있습니다. 그러나 WebFlux의 ServerHttpSecurity에서는 비슷한 것을 찾을 수 없습니다.

WebFlux에서 x509 (). subjectPrincipalRegex (...)에 해당하는 것은 무엇입니까?

최종 목표는 인증서 제목을 Reactive UserDetailsService에 보낸 사용자 이름으로 가져 오는 것입니다.

해결법

  1. ==============================

    1.나는 이전 버전의 스프링 에서처럼 X509 필터가 있다고 생각하지 않는다. 그래서 당신은 그것의 자신의 버전을 구현해야 할 것이다. 다행스럽게도 편리한 org.springframework.security.web.server.authentication.AuthenticationWebFilter는 인증 플로우의 패턴을 제공하지만 cert / request에서 주체를 추출해야합니다.

    나는 이전 버전의 스프링 에서처럼 X509 필터가 있다고 생각하지 않는다. 그래서 당신은 그것의 자신의 버전을 구현해야 할 것이다. 다행스럽게도 편리한 org.springframework.security.web.server.authentication.AuthenticationWebFilter는 인증 플로우의 패턴을 제공하지만 cert / request에서 주체를 추출해야합니다.

    가장 먼저해야 할 일은 인증 변환기를 설정하여 인증서에서 주체를 추출하는 것입니다.

    public class X509AuthenticationConverter implements Function<ServerWebExchange, Mono<Authentication>> {
    
        @Override
        public Mono<Authentication> apply(ServerWebExchange exchange) {
            ServerHttpRequest request = exchange.getRequest();
            try {
               // extract credentials here
               Authentication authentication = ...
               return Mono.just(authentication);
            } catch (Exception e) {
               // log error here
               return Mono.empty();
            }
        }
    }
    

    이제 우리 config에서 필터와 변환기 빈을 만들고 필터를 변환기로 설정합니다.

    @Bean
    public X509AuthenticationConverter x509AuthenticationConverter() {
        return new X509AuthenticationConverter();
    }
    
    @Bean
    public AuthenticationWebFilter x509AuthenticationWebFilter(ReactiveAuthenticationManager reactiveAuthenticationManager,
                                                              X509AuthenticationConverter x509AuthenticationConverter) {
        AuthenticationWebFilter authenticationWebFilter = new AuthenticationWebFilter(reactiveAuthenticationManager);
        authenticationWebFilter.setAuthenticationConverter(x509AuthenticationConverter);
        return authenticationWebFilter;
    }
    

    마지막으로 보안을 구성하십시오.

    @Bean
    SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http, AuthenticationWebFilter x509AuthenticationWebFilter) {
        return http
                .addFilterAt(x509AuthenticationWebFilter, SecurityWebFiltersOrder.AUTHENTICATION)
                //...
                .build();
    }
    

    이 기능은 다른 인증 메커니즘과도 잘 작동합니다.

  2. from https://stackoverflow.com/questions/48111084/authentication-by-certificate-for-webflux by cc-by-sa and MIT license