博客
关于我
Spring Security 实战干货:实现自定义退出登录
阅读量:419 次
发布时间:2019-03-06

本文共 3510 字,大约阅读时间需要 11 分钟。

Spring Security 退出登录实战

前言

上一篇对 Spring Security 所有内置的进行了介绍。今天我们来实战如何安全退出应用程序。


登录后的处理

登录后,服务端会给用户发一个凭证。常见有以下两种:

  • 基于 Session:服务端存一个 Session,客户端存 cookie。
  • 基于 token:客户端存 token,服务端存校验信息。

  • 退出登录需要做什么

  • 清除服务端的用户状态。
  • 退出登录接口不是 permitAll,只有携带对应用户的凭证才退出。
  • 将退出结果返回给请求方。
  • 退出后用户可以通过重新登录来认证。

  • Spring Security 中的退出登录

    接下来我们分析并实战如何定制退出登录逻辑。


    3.1 LogoutFilter

    退出登录逻辑由过滤器 LogoutFilter 执行。它持有三个接口属性:

  • RequestMatcher logoutRequestMatcher:拦截退出请求的 URL。
  • LogoutHandler handler:处理退出逻辑。
  • LogoutSuccessHandler logoutSuccessHandler:退出成功后执行逻辑。

  • 3.2 LogoutConfigurer

    我们通过 HttpSecurity#logout() 初始化 LogoutConfigurer。接下来实战配置。

    3.2.1 自定义退出登录请求 URL

    LogoutConfigurer 提供两种方式定义退出 URL:logoutRequestMatcher 和 logoutUrl。选择其中一种即可。

    3.2.2 处理具体逻辑

    默认情况下 Spring Security 是基于 Session 的。LogoutConfigurer 提供清除认证信息、删除 cookies、移除 HttpSession 等功能。如果不满足需求,需定制 LogoutHandler 和 LogoutSuccessHandler。

    3.2.3 退出成功逻辑

  • logoutSuccessUrl:退出成功后重定向的 URL。你可以写一个Controller完成,最终返回,需支持 GET 请求和匿名访问。
  • defaultLogoutSuccessHandlerFor:构造默认 LogoutSuccessHandler,可以实现不同 URL 的逻辑。
  • LogoutSuccessHandler:退出成功后执行的逻辑的抽象接口。

  • 3.3 Spring Security 退出登录实战

    现在前后端分离,退出后返回 json。且只有用户在线才能退出。我们采用 LogoutHandler 和 LogoutSuccessHandler 编程方式配置退出逻辑。


    3.3.1 自定义 LogoutHandler

    默认情况下清除认证信息和 Session 已经由 SecurityContextLogoutHandler 完成。我们自定义 LogoutHandler 会在其基础上执行。

    @Slf4jpublic class CustomLogoutHandler implements LogoutHandler {    @Override    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {        User user = (User) authentication.getPrincipal();        String username = user.getUsername();        log.info("username: {} is offline now", username);    }}

    3.3.2 自定义 LogoutSuccessHandler

    如果实现了自定义 LogoutSuccessHandler,就不需要设置 logoutSuccessUrl。处理后会响应给前端。你可以转发到其他控制器,或者实现其他 MediaType,比如 json 或页面。

    @Slf4jpublic class CustomLogoutSuccessHandler implements LogoutSuccessHandler {    @Override    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)             throws IOException, ServletException {        User user = (User) authentication.getPrincipal();        String username = user.getUsername();        log.info("username: {} is offline now", username);        responseJsonWriter(response, RestBody.ok("退出成功"));    }    private static void responseJsonWriter(HttpServletResponse response, Rest rest) throws IOException {        response.setStatus(HttpServletResponse.SC_OK);        response.setCharacterEncoding("utf-8");        response.setContentType(MediaType.APPLICATION_JSON_VALUE);        ObjectMapper objectMapper = new ObjectMapper();        String resBody = objectMapper.writeValueAsString(rest);        PrintWriter printWriter = response.getWriter();        printWriter.print(resBody);        printWriter.flush();        printWriter.close();    }}

    3.3.4 自定义退出的 Spring Security 配置

    为了方便调试注释掉了部分配置。你可以通过 http:localhost:8080/login 登录,然后通过 http:localhost:8080/logout 测试退出。

    @Overrideprotected void configure(HttpSecurity http) throws Exception {    http.csrf().disable()            .cors()            .and()            .authorizeRequests().anyRequest().authenticated()            .and()            .formLogin().loginProcessingUrl(LOGIN_PROCESSING_URL).successForwardUrl("/login/success").failureForwardUrl("/login/failure")            .and().logout()            .addLogoutHandler(new CustomLogoutHandler())            .logoutSuccessHandler(new CustomLogoutSuccessHandler());}

    总结

    本篇实现了 Spring Security 下的自定义退出逻辑。相对比较简单,你可以根据你的业务需要来实现你的退出逻辑。有什么疑问可以通过关注公众号 Felordcn 私信提问。相关DEMO代码也可以通过关注后回复 ss04 获取。

    转载地址:http://xpjuz.baihongyu.com/

    你可能感兴趣的文章
    NHibernate学习[1]
    查看>>
    NHibernate异常:No persister for的解决办法
    查看>>
    NIFI1.21.0_Mysql到Mysql增量CDC同步中_日期类型_以及null数据同步处理补充---大数据之Nifi工作笔记0057
    查看>>
    NIFI1.21.0_NIFI和hadoop蹦了_200G集群磁盘又满了_Jps看不到进程了_Unable to write in /tmp. Aborting----大数据之Nifi工作笔记0052
    查看>>
    NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
    查看>>
    NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_根据binlog实现数据实时delete同步_实际操作04---大数据之Nifi工作笔记0043
    查看>>
    NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置binlog_使用处理器抓取binlog数据_实际操作01---大数据之Nifi工作笔记0040
    查看>>
    NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_实现数据插入数据到目标数据库_实际操作03---大数据之Nifi工作笔记0042
    查看>>
    NIFI从MySql中离线读取数据再导入到MySql中_03_来吧用NIFI实现_数据分页获取功能---大数据之Nifi工作笔记0038
    查看>>
    NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
    查看>>
    NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
    查看>>
    Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
    查看>>
    NIFI大数据进阶_FlowFile拓扑_对FlowFile内容和属性的修改删除添加_介绍和描述_以及实际操作---大数据之Nifi工作笔记0023
    查看>>
    NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
    查看>>
    NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
    查看>>
    NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
    查看>>
    NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
    查看>>
    NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
    查看>>
    NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
    查看>>
    NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
    查看>>