【问题标题】:Avoid basic auth when using x.509 authentication使用 x.509 身份验证时避免基本身份验证
【发布时间】:2022-06-10 20:14:20
【问题描述】:

我创建了一个基于 Spring WebFlux 的 REST API,它通过 X.509 身份验证进行保护。我按照本指南https://www.baeldung.com/x-509-authentication-in-spring-security 创建了所有证书。

路由器实现:

@Configuration
class LogRouter {

    @Bean
    fun functionalRoutes(handler: LogHandler): RouterFunction<ServerResponse> =
        route()
            .route(RequestPredicates.path("/")) {
                ServerResponse.ok().body(Mono.just("I am alive"))
            }
            .nest(RequestPredicates.path("/api").and(RequestPredicates.accept(MediaType.APPLICATION_JSON))) { builder ->
                builder.GET("/fn/mono", handler::monoMessage)
                    .POST("/fn/mono", handler::monoPostMessage)
            }
            .build()
}

和应用实施:

@SpringBootApplication
@EnableWebFluxSecurity
class RestplayApplication {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
        val principalExtractor = SubjectDnX509PrincipalExtractor()

        principalExtractor.setSubjectDnRegex("OU=(.*?)(?:,|$)")

        val authenticationManager = ReactiveAuthenticationManager { authentication: Authentication ->
            authentication.isAuthenticated = "Trusted Org Unit" == authentication.name
            Mono.just(authentication)
        }

        http
            .x509 { x509 ->
                x509
                    .principalExtractor(principalExtractor)
                    .authenticationManager(authenticationManager)
            }
            .authorizeExchange { exchanges ->
                exchanges
                    .anyExchange().authenticated()
            }
        return http.build()

    }
}

fun main(args: Array<String>) {
    runApplication<RestplayApplication>( *args)
}

我使用 Firefox 浏览器测试 x.509 身份验证,并将自签名证书(rootCA.crt)添加到 Firefox:

包含客户端证书(clientBob.p12)。

在浏览器中调用链接时,它会显示基本身份验证表单:

但是,我希望不会出现身份验证表单,因为我在浏览器中提供了有效的客户端证书。

为什么每次都会出现基本形式?

代码托管在https://github.com/softshipper/restplay。证书的密码始终是changeit

【问题讨论】:

  • 我也有同样的问题,而且很累。我悬赏这个问题。

标签: spring spring-boot kotlin spring-security


猜你喜欢
  • 2014-09-15
  • 2011-03-22
  • 2021-06-20
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2013-05-30
相关资源
最近更新 更多