当前位置:首页 » 《随便一记》 » 正文

关于Spring Boot 2.7后WebSecurityConfigurerAdapter的过期问题,SpringSecurity的最新最全配置

7 人参与  2024年03月30日 13:00  分类 : 《随便一记》  评论

点击全文阅读


前言

进入springboot 2.7 后,一个重要的类WebSecurityConfigurerAdapter过期了

正文

①:如果想使用自定义的用户,只需要向Spring容器中注入一个UserDetailsService实例即可,此时用户是存在内存中的。如果用户是存在数据库中的,需要提供UserDetailsService接口的实现类并注入到Spring容器中

@Configurationpublic class SecurityConfig {    UserDetailsService userDetailsService(){        InMemoryUserDetailsManager users = new InMemoryUserDetailsManager();        users.createUser(User.withUsername("root").password("{noop}123456").roles("admin").build());        return users;    }}

②:给公共资源放行,就是希望用户不用登录就能访问

(1):配置WebSecurity:注册WebSecurityCustomizer的一个实例

@BeanWebSecurityCustomizer webSecurityCustomizer() {    return new WebSecurityCustomizer() {        @Override        public void customize(WebSecurity web) {            // 在 Spring Security 5.8 中,弃用 antMatchers 了 、 mvcMatchers 和 regexMatchers 方法,            // 转而使用新 requestMatchers 方法            web.ignoring().requestMatchers("/hello");// 放行/hello        }    };}

(2):配置HttpSecurity

@BeanSecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{    http.authorizeRequests()        // 在 Spring Security 5.8 中,弃用 antMatchers 了 、 mvcMatchers 和 regexMatchers 方法,        // 转而使用新 requestMatchers 方法        .requestMatchers("/login").permitAll()        .anyRequest().authenticated()        .and().formLogin();    return http.build();}

两种方式最大的区别在于,第一种方式是不走 Spring Security 过滤器链,而第二种方式走 Spring Security 过滤器链,在过滤器链中,给请求放行。

在我们使用 Spring Security 的时候,有的资源可以使用第一种方式额外放行,不需要验证,例如前端页面的静态资源,就可以按照第一种方式配置放行。

有的资源放行,则必须使用第二种方式,例如登录接口。大家知道,登录接口也是必须要暴露出来的,不需要登录就能访问到的,但是我们却不能将登录接口用第一种方式暴露出来,登录请求必须要走 Spring Security 过滤器链,因为在这个过程中,还有其他事情要做

③:自定义登录成功与失败处理

public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {    @Override    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {        Map<String, Object> result=new HashMap<>();        result.put("message","登录成功");        result.put("status",200);        response.setContentType("application/json;charset=UTF-8");        // 返回结果        String s = new ObjectMapper().writeValueAsString(result);        response.getWriter().println(s);    }}
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {    @Override    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {        Map<String, Object> result=new HashMap<>();        result.put("message","登录失败:"+exception.getMessage());        result.put("status",500);        response.setContentType("application/json;charset=UTF-8");        // 返回结果        String s = new ObjectMapper().writeValueAsString(result);        response.getWriter().println(s);    }}

在HttpSecurity中进行配置

@BeanSecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{    http.authorizeRequests()        // 在 Spring Security 5.8 中,弃用 antMatchers 了 、 mvcMatchers 和 regexMatchers 方法,        // 转而使用新 requestMatchers 方法        .requestMatchers("/login").permitAll()        .anyRequest().authenticated()        .and().formLogin()        .successHandler(new MyAuthenticationSuccessHandler())        .failureHandler(new MyAuthenticationFailureHandler())        .and().csrf().disable();    return http.build();}

结果

在这里插入图片描述

在这里插入图片描述

④:自定义注销登录

@Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {    Map<String, Object> result = new HashMap<String, Object>();    result.put("msg", "注销成功");    result.put("status", 200);    response.setContentType("application/json;charset=UTF-8");    String s = new        ObjectMapper().writeValueAsString(result);    response.getWriter().println(s);}

在HttpSecurity中进行配置

@BeanSecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{    http.authorizeRequests()        // 在 Spring Security 5.8 中,弃用 antMatchers 了 、 mvcMatchers 和 regexMatchers 方法,        // 转而使用新 requestMatchers 方法        .requestMatchers("/login").permitAll()        .anyRequest().authenticated()        .and().formLogin()        .successHandler(new MyAuthenticationSuccessHandler())        .failureHandler(new MyAuthenticationFailureHandler())        .and().logout()        .logoutRequestMatcher(new OrRequestMatcher(            new AntPathRequestMatcher("/logout","GET"),            new AntPathRequestMatcher("/logout1","GET")))        .invalidateHttpSession(true)        .clearAuthentication(true)        .logoutSuccessHandler(new MyLogoutSuccessHandler())        .and().csrf().disable();    return http.build();}

结果

在这里插入图片描述


点击全文阅读


本文链接:http://zhangshiyu.com/post/88087.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 丁榆:结局+番外看点十足(和渣男小叔叔的游戏日常)全书无套路阅读
  • 法医狂妃:王爷你命中缺我小说(苏七夜景辰)全本完整阅读最新章节(法医狂妃:王爷你命中缺我)_笔趣阁
  • (头条)洛甯陆乘渊小说(闪婚老公是卧底,消失三年回来了)整本免费版阅读无广告(洛甯陆乘渊)
  • 陆少今天又秀恩爱了吗后续(陆怀骁苏棠)(陆少今天又秀恩爱了吗)整本畅享在线+无广告结局
  • 独家江柏俊夏清恬无删减(重生之团长俊夫要离婚)(江柏俊夏清恬)TXT免费版阅读
  • 全网首发重生下乡嫁糙汉,渣男全家悔哭了彩蛋(师明凯颜惜雪)(重生下乡嫁糙汉,渣男全家悔哭了)全本完整阅读无弹窗
  • 我是让他不屑一顾,不愿多看一眼的路人小说(安喻意商晋深)(我是让他不屑一顾,不愿多看一眼的路人)在线畅读阅读连载中
  • 丁榆后续(和渣男小叔叔的游戏日常)全文在线下载在线+纯净版结局
  • 洛甯陆乘渊::结局+番外评价五颗星-闪婚老公是卧底,消失三年回来了:结局+番外新上热文
  • 前传孟流年周丽珍续集(孟流年周丽珍)终章阅读极简(孟流年周丽珍)
  • 夏语栀祁墨寒我们各归人海,此生,也不必再见彩蛋小说结尾+附加(我们各归人海,此生,也不必再见)清爽版阅读
  • 重生下乡嫁糙汉,渣男全家悔哭了小说完结篇(师明凯颜惜雪)(重生下乡嫁糙汉,渣男全家悔哭了)全书无套路阅读无广告小说大结局

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1