用的挺顺手的 Spring Security 配置类,居然就要被官方弃用了?

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

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

相关的issues已经被处理并关闭

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

早在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
,不限制数量,但是要注意有排序问题。相关的思维导图:

最后

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

本文就是愿天堂没有BUG给大家分享的内容,大家有收获的话可以分享下,想学习更多的话可以到微信公众号里找我,我等你哦。

相关文章
|
5天前
|
存储 Java 数据安全/隐私保护
|
7天前
|
安全 Java 数据安全/隐私保护
|
11天前
|
安全 Java API
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
33 0
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
|
2天前
|
Java 开发者 Spring
Spring Boot中的资源文件属性配置
【4月更文挑战第28天】在Spring Boot应用程序中,配置文件是管理应用程序行为的重要组成部分。资源文件属性配置允许开发者在不重新编译代码的情况下,对应用程序进行灵活地配置和调整。本篇博客将介绍Spring Boot中资源文件属性配置的基本概念,并通过实际示例展示如何利用这一功能。
12 1
|
11天前
|
存储 安全 Java
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)(下)
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)
20 2
|
11天前
|
安全 Cloud Native Java
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)(上)
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)
24 2
|
11天前
|
安全 Java API
第5章 Spring Security 的高级认证技术(2024 最新版)(上)
第5章 Spring Security 的高级认证技术(2024 最新版)
38 0
|
11天前
|
存储 安全 Java
第3章 Spring Security 的用户认证机制(2024 最新版)(下)
第3章 Spring Security 的用户认证机制(2024 最新版)
33 0
|
11天前
|
安全 Java 数据库
第3章 Spring Security 的用户认证机制(2024 最新版)(上)
第3章 Spring Security 的用户认证机制(2024 最新版)
30 0
|
11天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
18 0