【发布时间】:2019-03-29 12:48:44
【问题描述】:
我有一个 REST 端点,访问权限仅限于 ADMIN 用户:
@GetMapping
@PreAuthorize("hasAuthority('ADMIN')")
fun getAll(): Flux<User> {
return Flux.fromIterable(userRepository.findAll())
}
当我尝试使用非 ADMIN 用户访问此端点时,我收到带有 denied 响应(非 JSON 响应)的 403。
如何自定义响应以仍然收到 403 响应,但带有类似 JSON 的消息
{
"error": "Access denied"
}
我正在实施的SecurityWebFilterChain:
return http.csrf().disable()
.formLogin().disable()
.httpBasic().disable()
.authenticationManager(authenticationManager)
.securityContextRepository(securityContextRepository)
.exceptionHandling().accessDeniedHandler { exchange, denied -> ???? }
.and()
.authorizeExchange()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
.pathMatchers("/auth").permitAll()
.anyExchange().authenticated()
.and().csrf().disable()
.logout().disable()
.build()
【问题讨论】:
标签: spring spring-security kotlin spring-webflux