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

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

文章目录

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

1、用户认证流程

AuthenticationSuccessHandler AuthenticationFailureHandler

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

public class SecurityAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public 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 {
    @Override
    public 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.kler.cn/a/488152.html

相关文章:

  • TDC-GP30 Data Sheet
  • 从 SQL 到 SPL:组内查找最近的匹配记录
  • 什么是负载均衡?NGINX是如何实现负载均衡的?
  • NO.3 《机器学习期末复习篇》以题(问答题)促习(人学习),满满干huo,大胆学大胆补!
  • 2025运维故障记 3 | 1/8左右 12306 4天3崩
  • 在vscode上
  • 【电子通识】PWM驱动让有刷直流电机恒流工作
  • 【大模型 RAG技术】Elasticsearch (ES) 构建一个基于 RAG问答系统
  • 【全球气候变化】基于R语言的DICE模型实践技术应用
  • 【机器学习:十三、PyTorch简介及实现】
  • HBuilderX打包ios保姆式教程
  • WPF的自定义控件控件学习
  • ubuntu 20.04 安装docker--小白学习之路
  • 警惕恐怖分子使用 ChatGPT 出谋划策
  • 静态路由配置与调试——计算机网络实训day1
  • 景芯SOC设计实战
  • 【漫话机器学习系列】042.提前停止训练的优势(Early Stopping Advantages)
  • Hadoop3.x 万字解析,从入门到剖析源码
  • Three.js - 打开Web 3D世界的大门
  • 【算法刷题】leetcode hot 100 滑动窗口