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

文章目录

1、用户认证流程

AuthenticationSuccessHandler AuthenticationFailureHandler

  • 登录成功后调用:AuthenticationSuccessHandler
  • 登录失败后调用:AuthenticationFailureHandler
java 复制代码
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配置

java 复制代码
form.successHandler(new SecurityAuthenticationSuccessHandler()) //认证成功时的处理

用户认证信息

java 复制代码
@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

java 复制代码
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配置

java 复制代码
//会话管理
http.sessionManagement(session -> {
    session
        .maximumSessions(1)
        .expiredSessionStrategy(new MySessionInformationExpiredStrategy());
});
相关推荐
长征coder3 分钟前
Maven Deploy的依赖与引用方的依赖不同
java·maven
努力学习的明11 分钟前
Spring MVC 对 JavaWeb 的优化:从核心组件到注解
java·spring·mvc·web
小雅痞13 分钟前
[Java][Leetcode middle] 238. 除自身以外数组的乘积
java·leetcode
小李不想说话25 分钟前
HTTPS 加密原理
java·网络·网络协议·学习·安全·http·https
斯密码赛我是美女27 分钟前
ssti刷刷刷
java·服务器·前端
进击的小白菜1 小时前
Java回溯算法解决非递减子序列问题(LeetCode 491)的深度解析
java·算法·leetcode
众乐乐_20081 小时前
Java 后端给前端传Long值,精度丢失的问题与解决
java·前端·状态模式
北辰浮光1 小时前
[springboot]SSM日期数据转换易见问题
java·spring boot·后端
木梓辛铭1 小时前
Spring Cache的详细使用
java·后端·spring
招风的黑耳1 小时前
Java视频流RTMP/RTSP协议解析与实战代码
java·视频流