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

10
我正在使用Spring Boot Security并使用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$$9505fc58@13f182b9,
 so it cannot be used on 
org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2SsoDefaultConfiguration$$EnhancerBySpringCGLIB$$dc290e2b@5ee0cf64 too.

此外,我尝试显式地为自己的安全配置设置更高的顺序,但似乎自动配置的安全配置覆盖了我的配置。

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


请参阅Spring Boot 参考文档。这并不是一个简单的过程。 - dur
9个回答

9
您需要在您的@SpringBootApplication类中实现以下方法。
 @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();

    }
}

谢谢,我尝试过ResourceServerConfigurerAdapter但是在请求构建器方面出了些问题。 - solomkinmv

8
@Configuration
@EnableOAuth2Sso
class MyConfiguration extends WebSecurityConfigurerAdapter {

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

}

确保您使用@EnableOAuth2SsoWebSecurityConfigurerAdapter类上。这很重要,因为它将包括OAuth2SsoCustomConfiguration,其基本上复制了OAuth2SsoDefaultConfiguration#configure的功能。
您可能还想显示完整的健康详细信息:
management:
  endpoint:
    health:
      show-details: always

4
以下是可能的检查项。 解决方案1: 确保使用

org.springframework.core.annotation.Order

而不是

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

由于Spring没有解析正确的注释,它假设两种配置的默认值都为100。 解决方案2: 可能您已经使用@EnableWebSecurity注释了另一个类。请注意,只有一个类可以实现此注释。 解决方案3:请参阅https://dev59.com/zlwX5IYBdhLWcg3wrBB6#44076087 解决方案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);
    }
}

1

我认为你可以扩展你使用的实现(如果我没记错的话是OAuth2SsoDefaultConfiguration),然后扩展configure方法来忽略你的健康端点。它看起来会像这样

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

顺便提一下,关于这个问题:我尝试显式地为自己的安全配置设置更高的顺序,但似乎自动配置的优先级比我的高。 @Order 的工作方式是,数字越小,优先级越高,这就解释了为什么自动配置会覆盖你的配置。文档在这里:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/annotation/Order.html


1
一个快速的更新,因为我正在使用最新的Spring Boot 2.7.11。似乎继承WebSecurityConfigurerAdapter现在已经被弃用。
相反,我只需这样做:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;


@EnableWebSecurity
@Configuration
public class ActuatorSecurityFilter {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/actuator").permitAll();
        return http.build();
    }
}

这个不起作用。我尝试了,出现错误“请考虑在您的配置中定义一个类型为'org.springframework.security.config.annotation.web.builders.HttpSecurity'的bean。” - Venu
这个不起作用我尝试了,出现了错误请在您的配置中定义一个类型为'org.springframework.security.config.annotation.web.builders.HttpSecurity'的bean。 - undefined
确实有效。相同的解决方案在下面还列出了3次。你还有另一个问题。也许你没有启用Spring Security,导致Spring无法将HttpSecurity注入到你的配置中。尝试在你的类上添加@EnableWebSecurity。 - Patrice Gagnon
确实有效。相同的解决方案在下面列出了3次。你还有另一个问题。也许你没有启用Spring Security,导致Spring无法在你的配置中注入HttpSecurity。尝试在你的类上添加@EnableWebSecurity。 - undefined
是的,它起作用了。但是我需要将注解更改为@EnableWebfluxSecurity,然后事情开始运行。不过,我想知道是否有其他方法可以通过application.properties实现相同的效果,而无需添加额外的类。 - Venu
是的,它起作用了。但是我需要将注解更改为@EnableWebfluxSecurity,然后事情开始运作了。然而,我想知道是否有其他方法通过application.properties来实现相同的功能,而不需要添加额外的类。 - undefined

0

management.security.enabled: false

不适用于Spring Boot 2.x版本。


0

在Spring Boot 2中,management.security.enabled: false已经不再适用。我们需要采用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();
    }
}

0

关于 Kotlin

@Configuration
class SecurityConfiguration : WebSecurityConfigurerAdapter() {
    override fun configure(httpSecurity: HttpSecurity) {
        httpSecurity.authorizeRequests().antMatchers("/actuator").permitAll()
    }
}

-3
你也可以在你的 application.properties (或 .yaml) 中使用以下代码: management.security.enabled: false 这将自动移除 actuator 暴露端点的任何安全限制。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接