Webflux 状态码无法更改
Webflux statuscode can not be changed
所以我有一个 AuthenticationWebFilter 可以在有人像这样进行身份验证时添加触发器:
val builder : HttpSecurity.AuthorizeExchangeBuilder = http
.addFilterAt(
CustomAuthenticationWebFilter(securityContextRepository),
SecurityWebFiltersOrder.AUTHENTICATION
)
.authorizeExchange().permitAll()
然后在 "CustomAuthenticationWebFilter" 中,我用一些逻辑更改了 statusCode,如下所示:
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return super.filter(exchange, chain)
.doOnSuccess {
exchange.response.statusCode = UNAUTHORIZED
return@doOnSuccess
}
关键是,当我做一个请求时,流程会先到 WebFilter
AbstractServerHttpResponse#doCommit
然后状态变为 "COMMITTED" 当我调用
AbstractServerHttpResponse#setStatusCode
无法更改。
是否可以在提交前更改状态码?
build.gradle
compile('org.springframework.boot:spring-boot-starter-webflux')
compile('org.springframework.security:spring-security-web')
compile('org.springframework.security:spring-security-config')
好的,这几天测试了很多东西,我把"SecurityWebFiltersOrder"改成"SecurityWebFiltersOrder.AUTHORIZATION"
就解决了
所以我有一个 AuthenticationWebFilter 可以在有人像这样进行身份验证时添加触发器:
val builder : HttpSecurity.AuthorizeExchangeBuilder = http
.addFilterAt(
CustomAuthenticationWebFilter(securityContextRepository),
SecurityWebFiltersOrder.AUTHENTICATION
)
.authorizeExchange().permitAll()
然后在 "CustomAuthenticationWebFilter" 中,我用一些逻辑更改了 statusCode,如下所示:
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return super.filter(exchange, chain)
.doOnSuccess {
exchange.response.statusCode = UNAUTHORIZED
return@doOnSuccess
}
关键是,当我做一个请求时,流程会先到 WebFilter
AbstractServerHttpResponse#doCommit
然后状态变为 "COMMITTED" 当我调用
AbstractServerHttpResponse#setStatusCode
无法更改。
是否可以在提交前更改状态码?
build.gradle
compile('org.springframework.boot:spring-boot-starter-webflux') compile('org.springframework.security:spring-security-web') compile('org.springframework.security:spring-security-config')
好的,这几天测试了很多东西,我把"SecurityWebFiltersOrder"改成"SecurityWebFiltersOrder.AUTHORIZATION"
就解决了