• WebSecurityConfigurerAdapter过时的替代方式


    SpringSecurity配置,用着用着就过期了,而且还报unsafe异常,真的是不让懒人活着啊。


    万能的网络上找答案

    找了一圈,都说用 @Bean的方式注入,代替继承WebSecurityConfigurerAdapter

    试了一下,老是报异常:

    Found WebSecurityConfigurerAdapter as well as SecurityFilterChain. Please select just one.

    我已经把 

    @EnableWebSecurity

    去掉了,还是报这个,不知道哪里又加载了

    WebSecurityConfiguration

    这个类了。

    索性不管了,直接覆盖。


    看源码

    1. @Configuration(
    2. proxyBeanMethods = false
    3. )
    4. public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAware {
    5. ‘’‘’‘’‘’
    6. @Bean
    7. @DependsOn({"springSecurityFilterChain"})
    8. public SecurityExpressionHandler webSecurityExpressionHandler() {
    9. return this.webSecurity.getExpressionHandler();
    10. }
    11. @Bean(
    12. name = {"springSecurityFilterChain"}
    13. )
    14. public Filter springSecurityFilterChain() throws Exception {
    15. boolean hasConfigurers = this.webSecurityConfigurers != null && !this.webSecurityConfigurers.isEmpty();
    16. boolean hasFilterChain = !this.securityFilterChains.isEmpty();
    17. Assert.state(!hasConfigurers || !hasFilterChain, "Found WebSecurityConfigurerAdapter as well as SecurityFilterChain. Please select just one.");
    18. if (!hasConfigurers && !hasFilterChain) {
    19. WebSecurityConfigurerAdapter adapter = (WebSecurityConfigurerAdapter)this.objectObjectPostProcessor.postProcess(new WebSecurityConfigurerAdapter() {
    20. });
    21. this.webSecurity.apply(adapter);
    22. }
    23. Iterator var7 = this.securityFilterChains.iterator();
    24. while(true) {
    25. while(var7.hasNext()) {
    26. SecurityFilterChain securityFilterChain = (SecurityFilterChain)var7.next();
    27. this.webSecurity.addSecurityFilterChainBuilder(() -> {
    28. return securityFilterChain;
    29. });
    30. Iterator var5 = securityFilterChain.getFilters().iterator();
    31. while(var5.hasNext()) {
    32. Filter filter = (Filter)var5.next();
    33. if (filter instanceof FilterSecurityInterceptor) {
    34. this.webSecurity.securityInterceptor((FilterSecurityInterceptor)filter);
    35. break;
    36. }
    37. }
    38. }
    39. var7 = this.webSecurityCustomizers.iterator();
    40. while(var7.hasNext()) {
    41. WebSecurityCustomizer customizer = (WebSecurityCustomizer)var7.next();
    42. customizer.customize(this.webSecurity);
    43. }
    44. return (Filter)this.webSecurity.build();
    45. }
    46. }
    47. 、、、、、、、
    48. }

    就是 Assert.state(!hasConfigurers || !hasFilterChain, "Found WebSecurityConfigurerAdapter as well as SecurityFilterChain. Please select just one.");

    这句报出来的。

    1. @Configuration
    2. @RequiredArgsConstructor
    3. @EnableWebSecurity(debug = true)
    4. public class WebSecurityConfig
    5. {
    6. private final AuthenticationConfiguration authenticationConfiguration;
    7. @Bean( name = {"springSecurityFilterChain"})
    8. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
    9. http.authorizeRequests()
    10. .requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
    11. .antMatchers("/**").permitAll()
    12. .anyRequest().authenticated();
    13. return http.build();
    14. }
    15. @Bean
    16. public AuthenticationManager authenticationManager() throws Exception{
    17. AuthenticationManager authenticationManager = authenticationConfiguration.getAuthenticationManager();
    18. return authenticationManager;
    19. }
    20. @Bean
    21. public PasswordEncoder passwordEncoder() {
    22. return new BCryptPasswordEncoder();
    23. }
    24. }

     启动不了,看看异常:

    1. Description:
    2. The bean 'springSecurityFilterChain', defined in class path resource [com/micro/exchange/auth/config/WebSecurityConfig.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class] and overriding is disabled.
    3. Action:
    4. Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true


    挺贴心,还给了解决方案,咱们就是为了覆盖,不能修改名字,选第二个吧

    1. spring:
    2. main:
    3. allow-bean-definition-overriding: true

    修改完,启动,还是没启动起来,晕了。

    AuthorizationServerConfigurerAdapter 继承类报 authenticationManager空指针

    原因:继承类先于WebSecurityConfig类加载了。

    网上说给WebSecurityConfig 加 @Order, 但是没有生效,不知道是缓存还是什么原因。

    幸好有

    @AutoConfigureAfter(WebSecurityConfig.class)

    启动,调用,没有生效

    参考:SpringBoot: @AutoConfigureAfter 和 @AutoConfigureBefore失效问题_零点冰.的博客-CSDN博客_autoconfigureafter

    这篇文章比较实用,顺序生效了。

    问题解决。


    解决问题还是得撸下源码,才能真正解决问题

  • 相关阅读:
    Java基础---第九篇
    【LeetCode-中等题】513. 找树左下角的值
    Science子刊新发现:mtDNA损伤或可提前诊断帕金森病
    金仓数据库 KingbaseGIS 使用手册(6.15. 仿射变换函数)
    [Power Query] 数据的透视与逆透视
    技术学习:Python(09)|操作MongoDB
    第三章 UI开发的点点滴滴
    修改centos中Mysql( mariadb)数据默认存储位置
    java中需要加入依赖才能使用的注解
    揭秘华为如此多成功项目的产品关键——Charter模板
  • 原文地址:https://blog.csdn.net/fjza1168/article/details/128080933