【问题标题】:Authentication by certificate for WebFlux?通过 WebFlux 的证书进行身份验证?
【发布时间】:2018-06-15 02:48:56
【问题描述】:

在Spring Boot Web的常规Servlet API中,有HttpSecurity配置的.x509()。但是在 WebFlux 的 ServerHttpSecurity 中我找不到类似的东西。

WebFlux 中.x509().subjectPrincipalRegex(...) 的等价物是什么

最终目标是将证书主题作为用户名发送到ReactiveUserDetailsService

【问题讨论】:

  • 如果您添加评论而不是投反对票,我将不胜感激,所以我知道我在哪里弄错了自己,或者我的问题中遗漏了什么。只是投反对票是没有帮助的。

标签: java spring spring-boot spring-webflux


【解决方案1】:

我认为没有像以前的 spring 版本那样有 X509 过滤器,因此您必须实现自己的版本。幸运的是,方便的 org.springframework.security.web.server.authentication.AuthenticationWebFilter 提供了身份验证流程的模式,但您必须自己从证书/请求中提取主题。

您要做的第一件事是设置身份验证转换器以从证书中提取主题。

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();
        }
    }
}

现在在我们的配置中,我们创建过滤器和转换器 bean 并将转换器设置到过滤器中。

@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】:

    我建议您将 Spring Security 版本升级到 5.2.0 及更高版本。有类似的 x509 身份验证支持可用。看看下面的链接。 https://docs.spring.io/spring-security/site/docs/5.2.x/reference/html/reactive-x509.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2012-01-20
      • 2019-10-12
      相关资源
      最近更新 更多