极其抽象的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();
    }
相关推荐
wuqingshun3141591 分钟前
依赖注入的方式有几种,各是什么?
java·开发语言
冬夜戏雪11 分钟前
实习面经(十二)
java
lierenvip14 分钟前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
sxhcwgcy17 分钟前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
编码忘我20 分钟前
JVM 运行时数据区详解
java·后端·程序员
阿唯不困21 分钟前
AI智能应用开发(Java)从起点到终点-面向对象
java·后端
m0_7269659822 分钟前
面面面(2)
java·开发语言
05大叔33 分钟前
RAG开发
java·服务器·前端
迷藏49434 分钟前
# 发散创新:用 Rust实现高性能测试框架的底层逻辑与实战演练
java·开发语言·后端·python·rust
XuDream39 分钟前
idea中忽略idea文件不提交git和取消被 Git 追踪
java·git·intellij-idea