当前位置: 首页 > news >正文

【SpringSecurity】二、自定义页面前后端分离

文章目录

    • 1、用户认证流程
      • AuthenticationSuccessHandler AuthenticationFailureHandler
      • SecurityFilterChain配置
      • 用户认证信息
    • 2、会话并发处理
      • 2.1、实现处理器接口
      • 2.2、SecurityFilterChain配置

1、用户认证流程

AuthenticationSuccessHandler AuthenticationFailureHandler

  • 登录成功后调用:AuthenticationSuccessHandler
  • 登录失败后调用:AuthenticationFailureHandler
    在这里插入图片描述

public class SecurityAuthenticationSuccessHandler implements AuthenticationSuccessHandler {@Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {//获取用户身份信息Object principal = authentication.getPrincipal();//创建结果对象HashMap result = new HashMap();result.put("code", 0);result.put("message", "登录成功");result.put("data", principal);//转换成json字符串String json = JSON.toJSONString(result);//返回响应response.setContentType("application/json;charset=UTF-8");response.getWriter().println(json);}
}

SecurityFilterChain配置

form.successHandler(new SecurityAuthenticationSuccessHandler()) //认证成功时的处理

用户认证信息

@RestController
public class IndexController {@GetMapping("/")public Map index(){System.out.println("index controller");SecurityContext context = SecurityContextHolder.getContext();//存储认证对象的上下文Authentication authentication = context.getAuthentication();//认证对象String username = authentication.getName();//用户名Object principal =authentication.getPrincipal();//身份Object credentials = authentication.getCredentials();//凭证(脱敏)Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();//权限System.out.println(username);System.out.println(principal);System.out.println(credentials);System.out.println(authorities);//创建结果对象HashMap result = new HashMap();result.put("code", 0);result.put("data", username);return result;}
}

2、会话并发处理

后登录的账号会使先登录的账号失效

2.1、实现处理器接口

实现接口SessionInformationExpiredStrategy

package com.atguigu.securitydemo.config;public class MySessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {@Overridepublic void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {//创建结果对象HashMap result = new HashMap();result.put("code", -1);result.put("message", "该账号已从其他设备登录");//转换成json字符串String json = JSON.toJSONString(result);HttpServletResponse response = event.getResponse();//返回响应response.setContentType("application/json;charset=UTF-8");response.getWriter().println(json);}
}

2.2、SecurityFilterChain配置

//会话管理
http.sessionManagement(session -> {session.maximumSessions(1).expiredSessionStrategy(new MySessionInformationExpiredStrategy());
});
http://www.lryc.cn/news/516487.html

相关文章:

  • 小兔鲜儿:头部区域的logo,导航,搜索,购物车
  • 什么是VLAN?
  • WPS计算机二级•数据查找分析
  • 计算机网络 (28)虚拟专用网VPN
  • 【Python学习(七)——序列、列表、元组、range、字符串、字典、集合、可变类型不可变类型】
  • MATLAB常用建模方法——常用非参数检验
  • 【多线程初阶篇 ²】创建线程的方式
  • 纵览!报表控件 Stimulsoft Reports、Dashboards 和 Forms 2025.1 新版本发布!
  • 游戏引擎学习第75天
  • Java 23 集合框架详解:Set 接口及实现类(HashSet、TreeSet、LinkedHashSet)
  • ARMv8架构 CortexR52+ 内核 coresight_soc400介绍
  • 1.Python浅过(语法基础)
  • ioremap_nocache函数
  • 【235. 二叉搜索树的最近公共祖先 中等】
  • 构建智能企业:中关村科金大模型企业知识库的技术解析与应用
  • C++进阶——用Hash封装unordered_map和unordered_set
  • b612相机 13.5.5解锁会员hook
  • iOS - 弱引用表(Weak Reference Table)
  • C#语言的网络编程
  • 【操作系统】课程 4调度与死锁 同步测练 章节测验
  • 如何查看已经安装的python版本和相关路径信息
  • 设计模式-结构型-适配器模式
  • 鸿蒙操作系统(HarmonyOS)
  • 基于海思soc的智能产品开发(camera sensor的两种接口)
  • 解密LLM结构化输出:代码示例与原理分析
  • Go语言的数据类型
  • 复杂园区网基本分支的构建
  • 如何很快将文件转换成另外一种编码格式?编码?按指定编码格式编译?如何检测文件编码格式?Java .class文件编码和JVM运行期内存编码?
  • 《C++11》Lambda 匿名函数从入门到进阶 优缺点分析 示例
  • 连接Milvus