Spring Security(十):登出Logout

一:Spring Security默认退出处理逻辑

  • 使当前session失效
  • 清楚与当前用户相关的remember-me记录
  • 清空当前的SecurityContext
  • 重定向到登录页

二:Spring Security 登出配置

spring security登出配置sping给出了一套默认值,如果不使用默认值,可以配置自己的值

  • logoutUrl:登出对应的地址
  • logoutSuccessHandler:登出成功后可以在这里处理自己的登出逻辑
  • deleteCookies:登出成功后删除指定的Cookie
protected void configure(HttpSecurity http) throws Exception {
	http.csrf().disable()
	.antMatchers("/login", "/session/invalid", "/logout", "/signOut").permitAll()
	.logout()
	    .logoutUrl("/logout")
	    .logoutSuccessHandler(myLogoutSuccessHandler)
	    .deleteCookies("JSESSIONID")
	    .permitAll();
}    
@Slf4j
@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("退出成功");

        response.sendRedirect("/signOut");
    }
}

三:登出页面

路径与视图的简单映射

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/signOut").setViewName("signOut");
        registry.addViewController("/index").setViewName("index");
    }
}

signOut.html 登出页面

<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>退出</title>
</head>
<body>
退出成功
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风流 少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值