极其抽象的SpringSecurity理解

原始:A → B

Security:A → S → B 太抽象了,看不懂啊T_T

抽象故事

故事大概:C是一个大区,拥有巨大的火力(C准备联合B吞并掉A),A得到了这个消息,派出间谍伪装成B区人,对C进行爆破,C开始怀疑B是假合作,但又不能明目张胆去破坏联盟又不能完全相信B,于是在BC大桥的C区进口建立海口进行对B区来的人进行防范

在理想状态下,我们是属于互不侵犯原则,但是呢,理想不是现实,总有人会因为某种原因进行攻击我们,在这种情况下,我们需要建立自己的海关进行防范,SpringSecurity就是一个目前具有实战经验的一种门

检查内容

第一层验证:我们要验证什么,进行对什么的验证,是只要进来的都要通过验证,还是部分需要验证

复制代码
authorizeHttpRequests()

第二次验证:居民信息认证(用户登录认证)userDetailsService()

第三层验证:进入验证,fromLougin

第四层验证:离开验证,logout

第五层验证:是否为跳转人员,csrf

第六层构建:build

java 复制代码
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
        return http
                .authorizeHttpRequests(authorizeChen ->{
                    authorizeChen.anyRequest().authenticated();
                })
                .formLogin(Login ->{
                    Login.loginProcessingUrl("/api/auth/login");
                    Login.successHandler(new AuthenticationSuccessHandler() {
//                        成功登录响应的内容
                        @Override
                        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
                            response.setCharacterEncoding("gbk");
                            response.getWriter().write(JSONObject.toJSONString(jsonEntity.json("登录成功")));
                        }
                    });
//                       登录失败响应内容
                    Login.failureHandler(new AuthenticationFailureHandler() {
                        @Override
                        public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
                            response.setCharacterEncoding("gbk");
                            response.getWriter().write(JSONObject.toJSONString(jsonEntity.failureJson(302,exception.getMessage())));
                        }
                    });
                })
                .logout(Logout ->{
                    Logout.logoutUrl("/api/auth/logout");
                })
                .userDetailsService(userservice)
                .csrf(AbstractHttpConfigurer::disable)
                .build();
    }
相关推荐
IT界的老黄牛7 小时前
Spring Boot 的 `/actuator/health` 不是免费的:健康检查打爆连接池的反面教材
spring boot·hikaricp·actuator·健康检查·线上排障·架构反模式
都叫我大帅哥9 小时前
Java JJWT 完全指南:从入门到生产级实践
java
前端开发张小七10 小时前
Java 学习笔记 · 第一课:基础语法与核心概念
java·后端
ThsPool11 小时前
【ENVI二次开发学习整理 04】ENVI功能扩展与二次开发
java·前端·javascript
赤壁小虾11 小时前
【渲染流水线】[逐片元阶段]-[透明度测试]以UnityURP为例
java·前端·数据库
青山木12 小时前
Hot 100 --- 搜索插入位置
java·数据结构·算法·leetcode
Jul1en_12 小时前
【Claude Code Compact】源码级别的学习上下文压缩
java·前端·学习·github·ai编程
摇滚侠12 小时前
《SpringBoot 3:入门与应用实战》第 15 章 生产级特性 管理 Spring Boot 应用 阅读笔记
java·spring boot·笔记
XWalnut13 小时前
SpringBoot快速入门
java·spring boot·后端
前端 贾公子13 小时前
第06章:结构化输出 (上)
java·服务器·前端