如何禁用执行器安全性而不用 Spring Boot 2 完全禁用它

How to disable actuator security without disabling it totally with Spring Boot 2

我正在使用 Spring Boot Security with OAuth2。我不想禁用健康端点的安全性。

我可以完全禁用安全性或编写自己的 WebSecurityConfigurerAdapter 实现并禁用自动配置的实现。

但是如何修改 WebSecurityConfigurerAdapter (OAuth2SsoDefaultConfiguration) 的现有实现?

我试图在不禁用自动配置的情况下创建自己的配置,但由于 Order 冲突,这是不可能的。

错误信息如下:

Caused by: java.lang.IllegalStateException: @Order on WebSecurityConfigurers must be unique. 
Order of 100 was already used on SecurityConfiguration$$EnhancerBySpringCGLIB$05fc58@13f182b9,
 so it cannot be used on 
org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2SsoDefaultConfiguration$$EnhancerBySpringCGLIB$$dc290e2b@5ee0cf64 too.

此外,我试图为我自己的安全配置明确设置更高的顺序,但看起来自动配置的顺序覆盖了我的。

那么如何在不重新实现整个配置的情况下覆盖特定的安全规则?

我认为您可以让自己的实现扩展您使用的实现(OAuth2SsoDefaultConfiguration,如果我做对了),然后扩展配置方法以忽略您的健康端点。它看起来或多或少像这样

@Override
public void configure(final HttpSecurity http) throws Exception {
    http.regexMatchers("/health",)
        .permitAll()
}

顺便说一下这个 Also, I tried to explicitly set higher order for my own security configuration, but looks like autoconfigured one overrides mine. @Order 的工作方式,较低的数字具有较高的优先级,因此它可以解释为什么自动配置会覆盖您的。文档在这里:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/annotation/Order.html

以下是可能的检查。

解决方案 1: 确保您使用的是

org.springframework.core.annotation.Order

而不是

org.apache.logging.log4j.core.config.Order

由于 Spring 没有解析正确的注解,它假定两种配置的默认值都是 100。

解决方案 2

也许您已经使用 @EnableWebSecurity 注释对另一个 class 进行了注释。请注意,只有一个 class 可以实现此注释。

解决方案 3:参考这个

解决方案 4:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class DemoConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests().antMatchers("/health").permitAll();
        super.configure(http);
    }
}

您需要在

中实现以下方法

@SpringBootApplication class

 @SpringBootApplication
 @EnableResourceServer
 @EnableGlobalMethodSecurity(prePostEnabled = true)
 @Configuration
 public class BusinessLogicServiceApplication extends ResourceServerConfigurerAdapter {

 public static void main(String[] args) throws IOException {
    ConfigurableApplicationContext context =  
    SpringApplication.run(BusinessLogicServiceApplication.class, args);
    }

  @Override
  public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/health").permitAll().anyRequest().authenticated();

    }
}
@Configuration
@EnableOAuth2Sso
class MyConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/actuator/health")
                .permitAll()
            .anyRequest()
                .authenticated();
    }

}

确保您使用的是 @EnableOAuth2Sso 而不是 WebSecurityConfigurerAdapter class。这很重要,因为它将包含 OAuth2SsoCustomConfiguration,它基本上复制了 OAuth2SsoDefaultConfiguration#configure.

的功能

您可能还想显示完整的健康详情:

management:
  endpoint:
    health:
      show-details: always

您还可以使用 management.security.enabled: false 在你的application.propeeties(或.yaml)中。它将自动删除执行器暴露端点的任何安全性

management.security.enabled: false在spring boot中不再有效 2.我们需要采取ConfigurerAdapter的方式。下面是我使用 OAuth2 资源服务器时的代码。

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

/**
 * to disable security for acutator endpoints.
 *
 */
@Configuration
public class ActuatorSecurityConfigurer extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/actuator").permitAll();
    }
}

management.security.enabled: false

不适用于 spring 引导 2.x 版本