【发布时间】:2020-07-03 02:24:42
【问题描述】:
我的 spring webflux 应用程序中有几个不同的 API,它们需要对失败的身份验证做出不同的响应。我正在尝试为每个 API 设置不同的 ServerAuthenticationEntryPoints 来处理这些情况。
我发现 this 示例配置显示了如何为不同的资源配置不同的 AuthenticationWebFilter,这使您可以单独设置 ServerAuthenticationSuccessHandler 和 ServerAuthenticationFailureHandler,但是我不确定如何在没有完全独立的 SecurityWebFilterChains 的情况下配置不同的 ServerAuthenticationEntryPoints。
如果我必须配置单独的 SecurityWebFilterChains,我该怎么做?
我的 SecurityWebFilterChain 目前是这样配置的 - 不幸的是,您不能单独设置 exceptionHandling,并且第二次调用 authenticationEntryPoint 是先例:
@Bean
fun securityWebFilterChain(
http: ServerHttpSecurity,
userServerAuthenticationEntryPoint: ServerAuthenticationEntryPoint,
userAuthenticationWebFilter: AuthenticationWebFilter,
deviceServerAuthenticationEntryPoint: ServerAuthenticationEntryPoint,
deviceAuthenticationWebFilter: AuthenticationWebFilter,
serverSecurityContextRepository: ServerSecurityContextRepository,
authenticationManager: ReactiveAuthenticationManager,
serverAccessDeniedHandler: ServerAccessDeniedHandler
): SecurityWebFilterChain {
http
.addFilterAt(userAuthenticationWebFilter, SecurityWebFiltersOrder.AUTHENTICATION)
.exceptionHandling()
.authenticationEntryPoint(userServerAuthenticationEntryPoint)
.and()
.authorizeExchange()
.pathMatchers(GET, "/sign-in").permitAll()
.pathMatchers("/authentication/**").permitAll()
.pathMatchers(GET, "/landing").hasAnyAuthority("USER", "ADMIN")
.pathMatchers("/user-api/**").hasAnyAuthority("USER", "ADMIN")
http
.addFilterAt(deviceAuthenticationWebFilter, SecurityWebFiltersOrder.AUTHENTICATION)
.exceptionHandling()
.authenticationEntryPoint(deviceServerAuthenticationEntryPoint)
.and()
.authorizeExchange()
.pathMatchers("/device-api/**").hasAuthority("DEVICE")
// GLOBAL
http
.httpBasic().disable()
.formLogin().disable()
.csrf().disable()
.cors().disable()
.securityContextRepository(serverSecurityContextRepository)
.authenticationManager(authenticationManager)
.exceptionHandling()
.accessDeniedHandler(serverAccessDeniedHandler)
.and()
.authorizeExchange()
.pathMatchers(GET, "/webjars/**").permitAll()
.pathMatchers(GET, "/assets/**").permitAll()
.anyExchange().authenticated()
return http.build()
}
【问题讨论】:
-
您可以根据请求 url 模式添加多个入口点。例如,参见baeldung.com/spring-security-multiple-entry-points 的第 3.2 节
-
@Ritesh 对于常规 Spring MVC 应用程序来说是正确的,但是 Spring Webflux ServerHttpSecurity 的 ExceptionHandlingSpec 不允许选择性异常处理(据我所知)
-
你说得对,这篇文章不是针对 webflux 的,我也没有在 webflux 中使用过多个入口点。查看代码ServerHttpSecurity,它似乎将
defaultEntryPoints作为DelegateEntry的列表,并且默认设置了RedirectServerAuthenticationEntryPoint。 -
是的,应该可以。您可以使用 ServerWebExchangeMatchers 对其进行限制。谢谢。
标签: spring-boot kotlin spring-security spring-webflux