【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());
});
相关推荐
悟空码字1 分钟前
SpringBoot实现系统监控:给应用装上“健康手环”
java·后端·监控
葡萄成熟时 !1 分钟前
快捷键idea
java
吃喝不愁霸王餐APP开发者3 分钟前
外卖霸王餐灰度开关:基于Spring Cloud Config+Bus动态刷新踩坑
java
雨中飘荡的记忆3 分钟前
Spring Security详解
java·spring
小许学java13 分钟前
网络编程套接字
java·网络·udp·socket·tcp·套接字
向葭奔赴♡18 分钟前
Android AlertDialog实战:5种常用对话框实现
android·java·开发语言·贪心算法·gitee
坐不住的爱码20 分钟前
静态资源映射-spring整合
java·spring·状态模式
大佐不会说日语~22 分钟前
基于Spring AI Alibaba的AI聊天系统中,流式输出暂停时出现重复插入问题的分析与解决
java·人工智能·spring
0和1的舞者27 分钟前
API交互:前后端分离开发实战指南
java·spring·tomcat·web3·maven·springmvc·springweb
一 乐31 分钟前
宠物店管理|基于Java+vue的宠物猫店管理管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端