【问题标题】:Spring Security WebFlux logoutSpring Security WebFlux 注销
【发布时间】:2020-04-02 20:32:28
【问题描述】:
在进行类似于
的注销时,在 WebFlux 中使会话无效和删除 cookie 的等效方法是什么?
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.httpBasic()
.and()
.logout().clearAuthentication(true)
.logoutSuccessUrl("/")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.and()
...
【问题讨论】:
标签:
spring-boot
spring-security
spring-webflux
【解决方案1】:
除了cookie“SESSION”和WebSession(WebFlux中的会话名)默认被移除之外,你可以配置一个ServerLogoutSuccessHandler:
.logout()
.logoutSuccessHandler(new ServerLogoutSuccessHandler() {
@Override
public Mono<Void> onLogoutSuccess(WebFilterExchange exchange, Authentication authentication) {
ServerHttpResponse response = exchange.getExchange().getResponse();
response.setStatusCode(HttpStatus.FOUND);
response.getHeaders().setLocation(URI.create("/login.html?logout"));
response.getCookies().remove("JSESSIONID");
return exchange.getExchange().getSession()
.flatMap(WebSession::invalidate);
}
})