极其抽象的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();
    }
相关推荐
夏贰四8 分钟前
管控数据加载调度如何规避任务冲突?数据加载运维监控体系该如何搭建?
java·大数据·运维
山甫aa23 分钟前
JavaWeb后端开发学习手册
java·开发语言·数据库·学习·mysql·springboot·web
鱼鳞_33 分钟前
热门八股-JVM
java·jvm
ly768934 分钟前
XML 从入门到实践:语法、命名空间、XPath、XSD 与 Java 安全解析
xml·java·python
长谷深风11143 分钟前
好的 Tool Schema,不是字段越全越好
java·大数据·人工智能·ai agent·agent工作流·智能体设计·ai产品设计
IT 行者1 小时前
用 HiddenHttpMethodFilter 解决浏览器不支持 PUT/DELETE/PATCH 的问题
java·spring
晚安code1 小时前
Redis 内存淘汰策略实战:什么时候淘汰、怎么选、怎么防缓存击穿
java·redis·缓存
goyeer1 小时前
Liunx日志管理与journalctl
java·linux·运维·服务器·运维开发·信息化·信息化企业管理
tianyu2342 小时前
Java 正则表达式终极指南:从 Pattern 到 Matcher,从双重转义到性能优化,一篇全搞定
java·开发语言·正则表达式·group·find·pattern·matcher
Emily156853598704 小时前
Emerson 1C31129G03 Analog Input Module
java·开发语言·前端·plc·emerson·1c31129g03·input module