极其抽象的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();
    }
相关推荐
Seven979 小时前
SPI机制:服务扩展的核心技术
java
NE_STOP9 小时前
shiro_实现分布式会话SessionManager、限制密码重试次数和并发登录控制
java
Seven979 小时前
剑指offer-63、数据流中的中位数
java
毕设源码-钟学长9 小时前
【开题答辩全过程】以 基于Spring Boot的社区养老服务管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
mjhcsp9 小时前
C++ Manacher 算法:原理、实现与应用全解析
java·c++·算法·manacher 算法
Coder_Boy_9 小时前
基于SpringAI的在线考试系统-企业级软件研发工程应用规范案例
java·运维·spring boot·软件工程·devops
indexsunny9 小时前
互联网大厂Java面试实战:微服务、Spring Boot与Kafka在电商场景中的应用
java·spring boot·微服务·面试·kafka·电商
SUDO-19 小时前
Spring Boot + Vue 2 的企业级 SaaS 多租户招聘管理系统
java·spring boot·求职招聘·sass
sheji34169 小时前
【开题答辩全过程】以 基于spring boot的停车管理系统为例,包含答辩的问题和答案
java·spring boot·后端
重生之后端学习10 小时前
21. 合并两个有序链表
java·算法·leetcode·链表·职场和发展