用过WebSecurityConfigurerAdapter的都知道对Spring Security十分重要,总管Spring Security的配置体系。但是马上这个类要废了,你没有看错,这个类将在5.7版本被@Deprecated所标记了,未来这个类将被移除。

对此对此网友大呼“学着学着就被弃用了”。既然马上要弃用了,总要有个过渡方案或者新玩法吧。

早在2021年3月份胖哥就写了一篇文章,把新玩法给明明白白说清楚了,如果你看了的话,肯定不会学废弃技术。这里把整套的替代方案再搞一遍,可别再学过时技术了。

版本需要Spring Security 5.4.x及以上。

HttpSecurity新旧玩法对比

旧玩法:

@Configuration
static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.authorizeRequests(authorize -> authorize
.anyRequest().authenticated()
);
}
}

新玩法:

@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.antMatcher("/**")
.authorizeRequests(authorize -> authorize
.anyRequest().authenticated()
)
.build();
}

原理去看这一篇文章。

WebSecurity新旧玩法对比

使用WebSecurity.ignoring()忽略某些URL请求,这些请求将被Spring Security忽略,这意味着这些URL将有受到 CSRF、XSS、Clickjacking 等攻击的可能。以下示例仅仅作为演示,请勿使用在生产环境。是不是又学到了呢?

旧玩法:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override
public void configure(WebSecurity web) {
// 仅仅作为演示
web.ignoring().antMatchers("/ignore1", "/ignore2");
} }

新玩法:

@Configuration
public class SecurityConfiguration { @Bean
public WebSecurityCustomizer webSecurityCustomizer() {
// 仅仅作为演示
return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
} }

如果你需要忽略URL,请考虑通过HttpSecurity.authorizeHttpRequestspermitAll来实现。

AuthenticationManager新旧玩法对比

AuthenticationManager配置主要分为全局的(Global )、本地的(Local)。

旧玩法

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication();
}
}

上面是通过WebSecurityConfigurerAdapter开启的是本地配置。开启全局配置需要覆写其authenticationManagerBean()方法并标记为Bean:

       @Bean(name name="myAuthenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

新玩法

本地配置通过HttpSecurity.authenticationManager实现:

@Configuration
public class SecurityConfiguration { @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults())
.authenticationManager(new CustomAuthenticationManager());
} }

全局配置摆脱了依赖WebSecurityConfigurerAdapter.authenticationManagerBean()方法,只需要定义一个AuthenticationManager类型的Bean即可:

    @Bean
AuthenticationManager ldapAuthenticationManager(
BaseLdapPathContextSource contextSource) {
LdapBindAuthenticationManagerFactory factory =
new LdapBindAuthenticationManagerFactory(contextSource);
factory.setUserDnPatterns("uid={0},ou=people");
factory.setUserDetailsContextMapper(new PersonContextMapper());
return factory.createAuthenticationManager();
}

当然还可以通过自定义GlobalAuthenticationConfigurerAdapter并注入Spring IoC来修改AuthenticationManagerBuilder,不限制数量,但是要注意有排序问题。相关的思维导图:

最后

很多技术方案都不是直接更改的,是会有一个变化的过程,只要你紧追变化,其实也就没有变化。

关注公众号:Felordcn 获取更多资讯

个人博客:https://felord.cn

Spring Security即将弃用WebSecurityConfigurerAdapter配置类的更多相关文章

  1. Spring Security(08)——intercept-url配置

    http://elim.iteye.com/blog/2161056 Spring Security(08)--intercept-url配置 博客分类: spring Security Spring ...

  2. Spring Security 之集群Session配置

    1.   新建Maven项目 cluster-session 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4.0. ...

  3. 【Spring Security】七、RememberMe配置

    一.概述 RememberMe 是指用户在网站上能够在 Session 之间记住登录用户的身份的凭证,通俗的来说就是用户登陆成功认证一次之后在制定的一定时间内可以不用再输入用户名和密码进行自动登录.这 ...

  4. Spring boot 配置文件参数映射到配置类属性

    [参考文章]:SpringBoot之@EnableConfigurationProperties分析 [参考文章]:在Spring Boot中使用 @ConfigurationProperties 注 ...

  5. spring security 登录、权限管理配置

    登录流程 1)容器启动(MySecurityMetadataSource:loadResourceDefine加载系统资源与权限列表)  2)用户发出请求  3)过滤器拦截(MySecurityFil ...

  6. Spring boot运行原理-自定义自动配置类

    在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...

  7. Spring Boot2.0+中,自定义配置类扩展springMVC的功能

    在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...

  8. spring mvc \ spring boot 允许跨域请求 配置类

    用@Component 注释下,随便放个地方就可以了 package com.chinaws.wsarchivesserver.core.config; import org.springframew ...

  9. Spring Security(二)--WebSecurityConfigurer配置以及filter顺序

    “致"高级"工程师(BUG工程师) 一颗折腾的心

随机推荐

  1. 《剑指offer》面试题18. 删除链表的节点

    问题描述 给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点. 返回删除后的链表的头节点. 注意:此题对比原题有改动 示例 1: 输入: head = [4,5,1,9], val = ...

  2. 记录一个问题:macos High Sierra 10.13.6 内核内存泄漏,导致内存满而不得不重启

    kernel_task进程占用内存10g以上,使用中突然提示内存不足,要求杀死工作进程,不得不强按电源键来关机重启. 升级之前,版本大约是macos High Sierra 10.13.4, 系统频繁 ...

  3. linux下编译支持opencl的opencv for android

    主要的步骤其他人已经写过,请参考这篇:https://www.cnblogs.com/hrlnw/p/4720977.html 操作的细节请参考附件的pdf:  https://files.cnblo ...

  4. linux新分区无法新建文件夹

    问题 因为最初分区480g随便都给了home,后来发现备份以及导出系统至IOS都要另外插硬盘很麻烦.所以需要重新分区.使用装机U盘的live ubuntu20系统使用Gparted分区后,发现回到Ub ...

  5. Windows和Linux(Centos7)下安装Nginx

    安装Nginx 这篇记录只不过做了一个简单总结,如果对这块没什么概念的话可以看一下知乎的这篇文章 https://zhuanlan.zhihu.com/p/83890573 window下安装 win ...

  6. centos6.6手动安装mysql5.5并配置主从同步

    0.实验环境 主机IP(Master) 192.168.61.150 centos6.6 从机IP(Slave)   192.168.61.157 centos6.6 1.查看centos系统版本 [ ...

  7. 实习之bii--配置esxi重启时,虚拟机也跟随重启

    由于初创环境不稳定又是服务器会重启,而内部安装的多部虚拟机并不默认跟随启动,需要设置,方法如下: 1.在本地通过vsphere client 登录到esxi的服务器上,然后点击配置找到虚拟机启动/关机 ...

  8. JavaScript之ES6常用新特性

    参考:https://www.jianshu.com/p/ac1787f6c50f 变量声明:const 与 let const:常量,必须初始化值   let:变量 格式:const 变量A = & ...

  9. CF1574F Occurrences

    考虑什么样的串是合法的. 直接考虑比较抽象,考虑具象化这个问题. 容易发现一个字符串的限制就相当于如果出现了其中一个字符 \(a_i = c\),那么 \(s\) 中 \(c\) 前 \(i - 1\ ...

  10. Python绘图工具turtle库的使用

    #PythonDraw.py import turtle #引入了一个绘图库(海归库) turtle.setup(650,350,200,200) #设置一个窗体 turtle.penup() #将画 ...