用过WebSecurityConfigurerAdapter的都知道,它对SpringSecurity非常重要,负责SpringSecurity的配置系统。但是这个类很快就会被废除,你没有看错,这个类在5.7版本中会被@Deprecated标记,以后会被移除。相关问题已处理关闭。网友大呼“学完就弃”。既然上线在即,必然会有过渡方案或者新的玩法。胖哥早在2021年3月就写了一篇文章,把新玩法解释的清清楚楚。看过了,肯定学不会过时的技术。这里把整套备选方案再做一遍,不要学过时的技术。版本需要SpringSecurity5.4.x及以上版本。HttpSecurity新老方式对比老方式:@ConfigurationstaticclassSecurityConfigextendsWebSecurityConfigurerAdapter{@Overrideprotectedvoidconfigure(HttpSecurityhttp)throwsException{http.antMatcher("/**").authorizeRequests(authorize->authorize.anyRequestic()));}}新玩法:@BeanSecurityFilterChainfilterChain(HttpSecurityhttp)throwsException{returnhttp.antMatcher("/**").authorizeRequests(authorize->authorize.anyRequest().authenticated()).build();}WebSecurity新老玩法对比使用WebSecurity.ignoring()忽略一些URL请求,这些请求会被SpringSecurity忽略,也就是说这些URL会受到CSRF、XSS、Clickjacking等攻击。以下示例仅供演示,请勿在生产环境中使用。你又学会了吗?老玩法:@ConfigurationpublicclassSecurityConfigurationextendsWebSecurityConfigurerAdapter{@Overridepublicvoidconfigure(WebSecurityweb){//只是为了演示web.ignoring().antMatchers("/ignore1","/ignore2");}}新玩法:@ConfigurationpublicclassSecurityConfiguration{@BeanpublicWebSecurityCustomizerwebSecurityCustomizer(){//仅供演示return(web)->web.ignoring().antMatchers("/ignore1","/ignore2");如果需要忽略URL,请考虑通过HttpSecurity.authorizeHttpRequests允许全部。AuthenticationManager新老玩法对比AuthenticationManager的配置主要分为全局(Global)和局部(Local)。旧方法:@ConfigurationpublicclassSecurityConfigurationextendsWebSecurityConfigurerAdapter{@Overrideprotectedvoidconfigure(AuthenticationManagerBuilderauth)throwsException{auth.jdbcAuthentication();}}以上是WebSecurityConfigurerAdapter启用的本地配置。要启用全局配置,您需要覆盖其authenticationManagerBean()方法并将其标记为Bean:HttpSecurity.authenticationManager实现:@ConfigurationpublicclassSecurityConfiguration{@BeanpublicSecurityFilterChainfilterChain(HttpSecurityhttp)throwsException{http.authorizeHttpRequests((authz)->authz.anyRequest().authenticated()).httpBasic(withDefaults()).authenticationManager(新的CustomAuthenticationManager());}}全局配置摆脱了对WebSecurityConfigurerAdapter.authenticationManagerBean()方法的依赖,只需要定义一个AuthenticationManager类型的Bean即可:LdapBindAuthenticationManagerFactory(contextSource);factory.setUserDnPatterns("uid={0},ou=people");factory.setUserDetailsContextMapper(newPersonContextMapper());返回工厂.createAuthenticationManager();spring}当然也可以自定义GlobalAuthenticationConfigurerAdapter,注入IoC用于修改AuthenticationManagerBuilder,数量不限,但需要注意思维导图相关排序问题:最后很多技术方案都是不是直接改变的,会有一个改变的过程。只要跟上变化,就不会有变化。.你从这篇文章中学到了很多吗?
