【问题标题】:Remove "Using default security password" on Spring Boot删除 Spring Boot 上的“使用默认安全密码”
【发布时间】:2015-08-25 23:53:18
【问题描述】:

我在 Spring Boot 的应用程序中添加了一个自定义安全配置,但是关于“使用默认安全密码”的消息仍然存在于 LOG 文件中。

有什么可以删除的吗?我不需要这个默认密码。 Spring Boot 似乎无法识别我的安全策略。

@Configuration
@EnableWebSecurity
public class CustomSecurityConfig extends WebSecurityConfigurerAdapter {

    private final String uri = "/custom/*";

    @Override
    public void configure(final HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.headers().httpStrictTransportSecurity().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        // Authorize sub-folders permissions
        http.antMatcher(uri).authorizeRequests().anyRequest().permitAll();
    }
}

【问题讨论】:

  • 这个类是否在正确的包中,因此可以找到?几个月前我犯了这个错误......将密码设置为已知值是否足够(我假设不是......)?
  • 也许你有和我一样的问题。我很幸运,Syer 先生给了我答案;-) stackoverflow.com/questions/27981681/…
  • 还有另一个配置类导入此安全配置@Import({ CustomSecurityConfig .class })。这个类由 Spring Boot 扫描,我在另一个与 CAS 安全相关的案例中注意到,Spring Boot 仅在我执行 @Inject public void configureGlobal(final AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { 时删除了此消息
  • 我尝试添加 security.basic.enabled=false 并没有任何改变。
  • 正确答案是:错误的包装。当您复制文件夹并粘贴到您的项目时会出现此错误。您可以将文件夹重构为正确的包并完成。

标签: java spring spring-boot spring-security


【解决方案1】:

我找到了一个关于排除 SecurityAutoConfiguration 类的解决方案。

例子:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
public class ReportApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyApplication.class, args);
    }
}

【讨论】:

  • 此解决方案唯一的不便之处在于它禁用了从 application.properties 读取安全属性的能力,因此您必须在之后控制自己。
  • 我想知道:将 Spring Security 添加到实际上具有良好的自动配置而不是禁用所有内容的 Boot 有什么意义?甚至考虑关闭日志记录?你想要安全吗?我想是这样。你想要一个默认生成的密码吗?也许没有……那你为什么不指定一个呢?只需使用这些属性: security.user.name=user # 默认用户名。 security.user.password = #默认用户名的密码。默认情况下,启动时会记录一个随机密码。或者运行你自己的 UserDetailsS​​ervice…
  • @MichaelSimons ...感谢您的回答。最好将内容放在属性文件中而不是日志中。在这种情况下,您也可以对其进行加密。再次感谢。
  • @MichaelSimons 例如,在我正在进行的项目中,我们只使用第 3 方用户池 - AWS Cognito。所以我们根本不需要 Spring 的自动配置用户服务。我们仅使用 Cognito 提供的代币。所以我只是从 Application 中排除了 ReactiveUserDetailsS​​erviceAutoConfiguration.class(因为我们使用 webflux)
  • 一个有数百万分的完全错误答案的好例子
【解决方案2】:

使用 Spring Boot 2.0.4 我遇到了同样的问题。

排除 SecurityAutoConfiguration.class 确实破坏了我的应用程序。

现在我正在使用@SpringBootApplication(exclude= {UserDetailsServiceAutoConfiguration.class})

适用于 @EnableResourceServer 和 JWT :)

【讨论】:

  • 你能解释一下在哪里写这段代码吗?以及“与@EnableResourceServer 和 JWT 一起工作”的意思(我是 Spring Boot 和 jwt 的新手)谢谢!
  • 对我来说,它在 @EnableAutoConfiguration 而不是 @SpringBootApplication (SpringBoot 2.3.0) 中使用此排除:@SpringBootApplication @EnableAutoConfiguration(exclude={UserDetailsServiceAutoConfiguration.class}) public class MainAppClassName { ... }
  • @EnableAutoConfiguration 是@SpringBootApplication 的一部分。
【解决方案3】:

在application.properties 中添加关注对我有用,

security.basic.enabled=false

记得重新启动应用程序并在控制台中检查。

【讨论】:

  • 在这里分享我的解决方法,management.security.enabled=false,我使用的是 spring 1.5.4 RELEASE。
  • 从(至少)Spring Boot 2 开始,这不再有效。
  • @Tobias 是的,你是对的。你有 Spring Boot 2 的解决方案吗?
  • @mor222 下面 Stefan 的解决方案为我做了。
  • @mor222 我在答案中添加了另一个对我有用的选项。 stackoverflow.com/a/51414492/1195507我正在使用 Spring Boot 2.0
【解决方案4】:

虽然它有效,但正如某些 cmets 中所指出的,当前的解决方案有点矫枉过正。所以这是一个适合我的替代方案,使用最新的 Spring Boot (1.4.3)。

默认安全密码在 Spring Boot 的 AuthenticationManagerConfiguration 类中配置。如果已经定义了 AuthenticationManager Bean,则此类具有条件注释以防止加载。

以下代码用于阻止 AuthenticationManagerConfiguration 中的代码执行,因为我们将当前的 AuthenticationManager 定义为 bean。

@Configuration
@EnableWebSecurity
public class MyCustomSecurityConfig extends WebSecurityConfigurerAdapter{

[...]

@Override
protected void configure(AuthenticationManagerBuilder authManager) throws Exception {
    // This is the code you usually have to configure your authentication manager.
    // This configuration will be used by authenticationManagerBean() below.
}

@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    // ALTHOUGH THIS SEEMS LIKE USELESS CODE,
    // IT'S REQUIRED TO PREVENT SPRING BOOT AUTO-CONFIGURATION
    return super.authenticationManagerBean();
}

}

【讨论】:

  • @EnableWebSecurity 不需要使其工作
  • 通过添加 authenticationManagerBean() 方法覆盖为我使用 Spring Boot 2.5.2 工作。没有必要使用@EnableWebSecurity。
【解决方案5】:

对于反应式堆栈(Spring Webflux、Netty),您需要排除 ReactiveUserDetailsS​​erviceAutoConfiguration.class

@SpringBootApplication(exclude = {ReactiveUserDetailsServiceAutoConfiguration.class})

或者定义 ReactiveAuthenticationManager bean(有不同的实现,这里是 JWT 的一个例子)

@Bean
public ReactiveJwtDecoder jwtDecoder() {
    return new NimbusReactiveJwtDecoder(keySourceUrl);
}
@Bean
public ReactiveAuthenticationManager authenticationManager() {
    return new JwtReactiveAuthenticationManager(jwtDecoder());
}

【讨论】:

    【解决方案6】:

    只需使用下面的行:

    spring.security.user.name=XXX
    spring.security.user.password=XXX
    

    设置默认的安全用户名和密码 在 Spring 应用程序的上下文中,在您的 application.properties(名称可能不同)处。

    要完全避免默认配置(作为 SpringBoot 自动配置的一部分) - 使用前面的答案中提到的方法:

    @SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
    

    或

    @EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class })
    

    【讨论】:

      【解决方案7】:

      要删除默认用户,您需要配置没有用户的身份验证管理器,例如:

      @configuration
      class WebSecurityConfig extends WebSecurityConfigurerAdapter {
      
          @Override
          protected void configure(AuthenticationManagerBuilder auth) throws Exception {
              auth.inMemoryAuthentication();
          }
      }
      

      这将删除默认密码消息和默认用户,因为在这种情况下,您正在配置 InMemoryAuthentication,并且您不会在后续步骤中指定任何用户

      【讨论】:

        【解决方案8】:

        在 Spring Boot 2 应用程序中,您可以从自动配置中排除服务配置:

        spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
        

        或者如果您只是想在日志中隐藏消息,您可以简单地更改日志级别:

        logging.level.org.springframework.boot.autoconfigure.security=WARN
        

        更多信息可以在这里找到:https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-security.html

        【讨论】:

          【解决方案9】:

          查找:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html

          从AuthenticationManagerConfiguration.java 看代码,我在下面看到。此外,如果没有根据Javadoc 提供身份验证管理器,则内存配置也是一种后备。您之前注入身份验证管理器的尝试会起作用,因为您将不再使用内存中身份验证,并且此类将无法显示。

          @Override
              public void configure(AuthenticationManagerBuilder auth) throws Exception {
                  if (auth.isConfigured()) {
                      return;
                  }
                  User user = this.securityProperties.getUser();
                  if (user.isDefaultPassword()) {
                      logger.info("\n\nUsing default security password: " + user.getPassword()
                              + "\n");
                  }
                  Set<String> roles = new LinkedHashSet<String>(user.getRole());
                  withUser(user.getName()).password(user.getPassword()).roles(
                          roles.toArray(new String[roles.size()]));
                  setField(auth, "defaultUserDetailsService", getUserDetailsService());
                  super.configure(auth);
              }
          

          如果您使用默认的内存身份验证,请为 org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration 自定义记录器配置并删除此消息。

          【讨论】:

          • 如你所说,我已经覆盖了我的配置方法,并且 Spring 消息仍然显示。一个不使用 Boot 的简单 Spring MVC 应用程序有什么好玩的,它可以正常工作。问题与 Spring Boot 有关,请查看添加到我的 CustomSecurityConfig 以覆盖该方法的代码:@Override public void configure(AuthenticationManagerBuilder auth) throws Exception {}
          【解决方案10】:

          当使用 spring boot 时,我们应该在应用程序类和您正在配置安全性的位置排除 SecurityAutoConfiguration.class,如下所示。

          只有我们才能避免默认的安全密码。

          import org.springframework.boot.SpringApplication;
          import org.springframework.boot.autoconfigure.SpringBootApplication;
          import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
          import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
          import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
          
          @SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
          @EnableJpaRepositories
          @EnableResourceServer
          public class Application {
          
              public static void main(String[] args) {
                  SpringApplication.run(Application.class, args);
              }
          }
          

          import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
          import org.springframework.context.annotation.Configuration;
          import org.springframework.security.config.annotation.web.builders.HttpSecurity;
          import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
          import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
          
              @Configuration
              @EnableWebSecurity
              @EnableAutoConfiguration(exclude = { 
                      org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class 
                  })
              public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
          
                  @Override
                  protected void configure(HttpSecurity httpSecurity) throws Exception {
                      httpSecurity.authorizeRequests().anyRequest().authenticated();
                      httpSecurity.headers().cacheControl();
                  }
              }
          

          【讨论】:

            【解决方案11】:

            我遇到了同样的问题,将这一行添加到我的 application.properties 解决了这个问题。

            spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
            

            它是 Spring 的 Automatic 之一,您将其排除在外,就像排除执行器等其他东西一样。我建议看看这个link

            【讨论】:

              【解决方案12】:

              如果你启用了actuator功能(spring-boot-starter-actuator),应在application.yml中添加额外的排除:

              spring:
                autoconfigure:
                  exclude: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration
              

              在 Spring Boot 版本 2.3.4.RELEASE 中测试。

              【讨论】:

                【解决方案13】:

                检查 org.springframework.boot.autoconfigure.security.servlet.UserDetailsS​​erviceAutoConfiguration 的文档 有条件自动配置会停止。

                在我的例子中,我忘记将我的自定义 AuthenticationProvider 定义为 bean。

                @Configuration
                public class SecurityConfig extends WebSecurityConfigurerAdapter {
                
                    @Override
                    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                        auth.authenticationProvider(getAuthenticationProvider());
                    }
                
                    @Bean
                    AuthenticationProvider getAuthenticationProvider() {
                        return new CustomAuthenticationProvider(adminService, onlyCorporateEmail);
                    }
                }
                

                【讨论】:

                  【解决方案14】:

                  当我使用 @SpringBootApplication 注释排除 SecurityAutoConfiguration 时它对我不起作用,但是当我在 @EnableAutoConfiguration 中排除它时它确实起作用:

                  @EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class })
                  

                  【讨论】:

                    【解决方案15】:

                    您只需要排除 UserDetailsS​​erviceAutoConfiguration。

                    spring:
                      autoconfigure:
                        exclude: org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
                    

                    【讨论】:

                      【解决方案16】:

                      如果你使用 Spring Security 和 spring cloud gateway,你可以排除ReactiveUserDetailsServiceAutoConfiguration.class。

                      这样

                      @SpringBootApplication(exclude = ReactiveUserDetailsServiceAutoConfiguration.class)
                      public class SpringClientApplication {
                      
                      

                      【讨论】:

                        【解决方案17】:

                        如果您使用的是 Spring Boot 版本 >= 2.0,请尝试在您的配置中设置此 bean:

                        @Bean
                        public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
                            http.authorizeExchange().anyExchange().permitAll();
                            return http.build();
                        }
                        

                        参考:https://stackoverflow.com/a/47292134/1195507

                        【讨论】:

                          【解决方案18】:

                          如果您在单独的包中声明您的配置, 确保像这样添加组件扫描:

                          @SpringBootApplication
                          @ComponentScan("com.mycompany.MY_OTHER_PACKAGE.account.config")
                          
                              public class MyApplication {
                          
                                  public static void main(String[] args) {
                                      SpringApplication.run(MyApplication.class, args);
                                  }
                          
                          
                          
                              }
                          

                          您可能还需要像这样在配置类中添加@component 注释:

                            @Component
                          @EnableWebSecurity
                          public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                          
                              @Override
                              protected void configure(HttpSecurity http) throws Exception {
                                  http
                                      .authorizeRequests()
                          
                          .....
                          
                          1. 同时清除浏览器缓存并以隐身模式运行 Spring Boot 应用

                          【讨论】:

                            【解决方案19】:

                            在带有 webflux 的 spring boot 2 上,您需要定义一个 ReactiveAuthenticationManager

                            【讨论】:

                              【解决方案20】:

                              也可以在属性中关闭特定类的日志记录:

                              logging.level.org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration=WARN

                              【讨论】:

                                【解决方案21】:

                                只需将以下属性添加到 application.properties

                                spring.security.user.name=xyz
                                spring.security.user.password=xxxxxxx
                                

                                【讨论】:

                                  猜你喜欢
                                  • 2016-04-14
                                  • 2020-11-28
                                  • 2016-09-20
                                  • 2018-04-26
                                  • 2017-05-17
                                  • 2023-03-12
                                  • 1970-01-01
                                  相关资源
                                  最近更新 更多