Spring boot 和 Spring Actuator - 禁用安全性

Posted

技术标签:

【中文标题】Spring boot 和 Spring Actuator - 禁用安全性【英文标题】:Spring boot and Spring Actuator - disable security 【发布时间】:2016-05-03 14:27:35 【问题描述】:

我在我的应用程序中使用 spring-boot 1.3.1 和 spring-boot-actuator。我在pom.xml 中使用spring-boot-starter-parent 作为父级。

为了禁用安全性,我在 application.yml 中添加了 2 个条目。

security:
  basic:
    enabled: false
management:
  security:
    enabled: false

它仍然没有禁用基本安全性。当我在本地 tomcat 中启动应用程序时,我在日志文件中看到了默认密码。

【问题讨论】:

【参考方案1】:

基本安全已禁用,但即使在将security.basic.enabled 设置为false 之后,Spring Boot 也会使用默认用户/密码配置身份验证管理器。 但基本安全被禁用。您可以重新配置 authenticationManager 以覆盖此行为,如下所示:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter 
    @Override
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception 
        auth
                .inMemoryAuthentication()
                    .withUser("me").password("secret").roles("ADMIN");
    
 

【讨论】:

【参考方案2】:

您可以通过在 application.yml 中设置用户名/密码来停止 spring boot 以记录默认密码 -

security:
  basic:
    enabled: false
  user:
    name: user
    password: ****

【讨论】:

【参考方案3】:

如果存在 Spring Security,您将需要添加自定义安全配置,以允许未经身份验证的访问端点。比如:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter 

//....

@Override
    public void configure(WebSecurity web) throws Exception 
        web.ignoring().antMatchers(HttpMethod.GET, "/actuator/**");
        super.configure(web);
    

【讨论】:

以上是关于Spring boot 和 Spring Actuator - 禁用安全性的主要内容,如果未能解决你的问题,请参考以下文章