前后端分离 Spring Security 对登出.logout()的处理

本文介绍了在前后端分离项目中,Spring Security默认登出处理会导致跨域问题。为了解决这个问题,需要自定义登出处理。具体做法是在WebSecurityConfig中配置HttpSecurity,并提供相应的处理逻辑。同时,文中给出了相关代码示例,包括axios的登出请求,Spring Security的配置以及ServerResponse的实现。参考了《Spring Boot+Vue开发实战》和一篇简书文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端axios发出的post请求如下

    logout() {
   
      this.axios.post(this.tools.serverAddr+'/logout')
          .then(function () {
   
            this.$message({
   
              message: "注销成功",
              type: 'success',
              duration: 1000
            });
            this.$router.replace({
   path: '/login'});
          }.bind(this))
          .catch(function (err) {
   
            if (err.response) {
   
              console.log(err.response)
            }
          }.bind(this))
    },

后端spring security默认将/logout重定向到/login?logout,在前后端分离项目中会出现跨域请求问题

需要自定义对登出的处理

.logout()
//注销成功的处理
.logoutSuccessHandler(new LogoutSuccessHandler() {
   
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
   
		printCode(response, 4);
	
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值