Spring Security即将弃用WebSecurityConfigurerAdapter配置类
用过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.authorizeHttpRequests
的permitAll
来实现。
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 获取更多资讯
Spring Security即将弃用WebSecurityConfigurerAdapter配置类的更多相关文章
- Spring Security(08)——intercept-url配置
http://elim.iteye.com/blog/2161056 Spring Security(08)--intercept-url配置 博客分类: spring Security Spring ...
- Spring Security 之集群Session配置
1. 新建Maven项目 cluster-session 2. pom.xml <project xmlns="http://maven.apache.org/POM/4.0. ...
- 【Spring Security】七、RememberMe配置
一.概述 RememberMe 是指用户在网站上能够在 Session 之间记住登录用户的身份的凭证,通俗的来说就是用户登陆成功认证一次之后在制定的一定时间内可以不用再输入用户名和密码进行自动登录.这 ...
- Spring boot 配置文件参数映射到配置类属性
[参考文章]:SpringBoot之@EnableConfigurationProperties分析 [参考文章]:在Spring Boot中使用 @ConfigurationProperties 注 ...
- spring security 登录、权限管理配置
登录流程 1)容器启动(MySecurityMetadataSource:loadResourceDefine加载系统资源与权限列表) 2)用户发出请求 3)过滤器拦截(MySecurityFil ...
- Spring boot运行原理-自定义自动配置类
在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...
- Spring Boot2.0+中,自定义配置类扩展springMVC的功能
在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...
- spring mvc \ spring boot 允许跨域请求 配置类
用@Component 注释下,随便放个地方就可以了 package com.chinaws.wsarchivesserver.core.config; import org.springframew ...
- Spring Security(二)--WebSecurityConfigurer配置以及filter顺序
“致"高级"工程师(BUG工程师) 一颗折腾的心
随机推荐
- 利用python绘制分析路易斯安那州巴吞鲁日市的人口密度格局
前言 数据来源于王法辉教授的GIS和数量方法,以后有空,我会利用python来实现里面的案例,这里向王法辉教授致敬. 绘制普查人口密度格局 使用属性查询提取区边界 import numpy as np ...
- python2.7发送邮件失败之——代码问题
使用python2.7发送邮件,代码如下: from email.header import Headerfrom email.mime.text import MIMETextimport smtp ...
- Java基础(十)——枚举与注解
一.枚举 1.介绍 枚举类:类的对象只有有限个,确定的.当需要定义一组常量时,强烈建议使用枚举类.如果枚举类中只有一个对象,则可以作为单例模式的实现. 使用 enum 定义的枚举类默认继承了 java ...
- C#进阶——记一次USB HID的各种坑(x86,x64,win10,win7)
一.简叙 写工控上位机的搬砖人,难免会遇到USB通讯,在一个项目中,我写的上位机使用USB HID协议和STM32通讯传输数据,从零大概花了几天找例程,找资料,最后是各种搬砖修补,终于出来了一个出版D ...
- manjaro20夜灯夜间模式开关
- 初识WorldWind——WorldWind编译生成,解决乱码等问题
本文中World Wind的GitHub源码下载地址:https://github.com/hujiulin/WorldWind 美国国家航空航天局(National Aeronautics and ...
- Solon 1.6.18 发布,轻量级应用开发框架
关于官网 千呼万唤始出来: https://solon.noear.org .整了一个月多了...还得不断接着整! 关于 Solon Solon 是一个轻量级应用开发框架.支持 Web.Data.Jo ...
- 怎么重载网卡?ip修改 HHS服务器
目录 一:目录结构知识详述 1.网卡配置文件 2,ip修改 3.重载网卡信息 4.关闭网络管理器(因为已经有了network)所有要关闭NetworkManager不然会发生冲突 5.判断SSH服务是 ...
- 使用ajax登录验证,第一次点击登录按钮无反应,第二次点击才能正常运行。
问题描述: 使用ajax进行登录验证时,第一次点击登录按钮无反应,第二次点击才能进去. 解决方法: 原来的代码 <form action="" method="po ...
- 关于spring MVC 绑定json字符串与实体类绑定
1 如果前台传json字符串,后台用@RequestBody 接收 前端 "content-Type":"application/json", 2 前台用fo ...