前端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() {@Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {printCode(response, 4);}
})

WebSecurityConfig中configure(HttpSecurity http)的完整代码如下

//方法注解方式@Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin().loginProcessingUrl("/doLogin").successHandler(new AuthenticationSuccessHandler() {@Overridepublic void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {printCode(httpServletResponse, 1);}}).failureHandler(new AuthenticationFailureHandler() {@Overridepublic void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {printCode(httpServletResponse, 2);}}).permitAll().and().logout()//注销成功的处理.logoutSuccessHandler(new LogoutSuccessHandler() {@Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {printCode(response, 4);}}).permitAll().and().authorizeRequests().antMatchers("/sysUser/currentUser").permitAll()//下面三行放开测试接口的权限,生产环境一定要删掉.and().authorizeRequests().antMatchers("/emp","/dep","/sysUser","/sysRole","/sysPermission").permitAll().anyRequest().authenticated().and().exceptionHandling().accessDeniedHandler(new AccessDeniedHandler() {@Overridepublic void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {printCode(httpServletResponse, 3);}}).and().headers().frameOptions().sameOrigin()// .and().cors().and().csrf().disable();}

printCode方法如下

private void printCode(HttpServletResponse httpServletResponse, Integer code) {try {httpServletResponse.setCharacterEncoding("UTF-8");PrintWriter out = httpServletResponse.getWriter();ServerResponse sr = new ServerResponse<>();sr.setCode(code);ObjectMapper mapper = new ObjectMapper();String str = mapper.writeValueAsString(sr);out.write(str);out.close();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (JsonProcessingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}

ServerResponse类代码如下

public class ServerResponse<T> implements Serializable {private Integer code;//0 未登录 1 登录成功  2登录失败 4登出成功private Long total;//查询出来的总记录数private T data;//查询出来的数据public ServerResponse() {}public ServerResponse(Long total, T data) {this.total = total;this.data = data;}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public Long getTotal() {return total;}public void setTotal(Long total) {this.total = total;}public T getData() {return data;}public void setData(T data) {this.data = data;}
}

参考文献:
1.《Spring Boot+Vue开发实战》 朱建昕
2. https://www.jianshu.com/p/a061c28d8202

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

  1. 前后端分离 Spring Boot + Vue 开发网易云QQ音乐(附源码)!

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者|xiangzhihong segmentfault.com/ ...

  2. 前后端分离 Spring Boot + Vue 开发网易云amp;QQ音乐(附源码)!

    前言 虽然 B/S 是目前开发的主流,但是 C/S 仍然有很大的市场需求.受限于浏览器的沙盒限制,网页应用无法满足某些场景下的使用需求,而桌面应用可以读写本地文件.调用更多系统资源,再加上 Web 开 ...

  3. Spring Boot + Vue 前后端分离

    前后端分离 Spring Booot + Vue 开发单页面应用 前言     需求: 最近本人在学习SpringBoot,希望自己能搭一个简单的Demo应用出来,但是搭到前端的时候遇到了困惑,因为网 ...

  4. 基于Spring boot + Mybatis +Netty 实现前后端分离的聊天App,部署到阿里云线上服务器...

    前后端分离Spring boot 项目部署 了解前后端分离项目 配置云服务器 java maven tomcat nginx mysql 部署后端项目 部署前端项目 部署Java环境 1.下载JDK软 ...

  5. Springboot + Spring Security 实现前后端分离登录认证及权限控制

    Spring Security简介 Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展 ...

  6. Springboot Spring Security +Jwt+redis+mybatisPlus 动态完成 前后端分离认证授权

    Springboot Spring Security +Jwt 动态完成 前后端分离认证授权 文章目录 Springboot Spring Security +Jwt 动态完成 前后端分离认证授权 前 ...

  7. Spring Security 前后端分离

    前后端分离概述 前后端分离指的就是前后端分离部署,前端 调用后端API,后端 返回 JSON格式数据,页面是由前端渲染并展示到浏览器中. 相比较传统的单体项目 ,页面是由后端渲染完成后返回给浏览器的. ...

  8. Spring Boot+Vue/前后端分离/高并发/秒杀实战课程之spring Security快速搭建oauth2 内存版身份认证

    Springboot快速搭建oauth2 内存版身份认证 环境准备 点击[Create New Project]创建一个新的项目 项目环境配置 配置Thymeleaf 搭建oauth2认证,加入两个依 ...

  9. springBoot整合spring security+JWT实现单点登录与权限管理前后端分离

    在前一篇文章当中,我们介绍了springBoot整合spring security单体应用版,在这篇文章当中,我将介绍springBoot整合spring secury+JWT实现单点登录与权限管理. ...

最新文章

  1. Java:多线程之线程池
  2. linux 命令行 ctrl z,Linux操作系统下运行命令时CTRL+Z的作用
  3. In-Loop Filters in HEVC
  4. [转]ubuntu系统中遇到的一些问题及解决
  5. cd mysql 权限不够_.bash_profile权限不够_cdmysql权限不够
  6. 带偏移量的AES加密工具
  7. 【srcipt】python运行cmd命令
  8. node-gulp插件
  9. c++ lamda表达式调用自身实现定时器
  10. 关于Cocoa框架的争论
  11. 只显示 前100个字 java 实现截取字符串!使用! c:if test=${fn:length(onebeans.info)100 }${ fn:substri...
  12. 感慨公交车766路调整
  13. 玩机搞机---mtk芯片机型线刷救砖的一些基本解决方法和步骤解析 mtk报错代码 SP_Flash平台刷机
  14. 20220508ARCore 初体验
  15. 元宇宙地产演化史:从文本时代到区块链时代
  16. NoSQLBooster for MongoDB 7.0.5
  17. 全球芯片短缺可能很快变成供过于求的危机
  18. MongoDB:排序超出了内存限制的104857600字节
  19. java 对word加密_Word文档中怎样给文件信息加密?大神都这样操作,你还不知道?...
  20. 如何恢复Mac上未保存的Word文件?

热门文章

  1. 解决uniapp静态文件字体包太大打包大小超出限制
  2. 如何在Node.js中获取本机本地IP地址
  3. Python之数据库编程
  4. Python Flask开发简单http api接口 示例
  5. Learning to Rank 简介(转载)
  6. 如何下载tomcat历史版本
  7. SpringMVC与JSON传值,取值,使用SpringMVC实现文件的上传与下载,SpringMVC拦截器
  8. jquery系列之-ajaxSubmit()提交表单示例
  9. http请求返回405 (Method Not Allowed)
  10. case when 的使用