关于java:Spring-Security即将弃用WebSecurityConfigurerAdapter配置类

8次阅读

共计 2805 个字符,预计需要花费 8 分钟才能阅读完成。

用过 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

正文完
 0