在不丢失可配置端点的情况下覆盖弹簧安全执行器

Kak*_*ait 10 spring-security spring-boot spring-boot-actuator

我想,以确保端点ActuatorsSpring Boot的项目.但是,使用准备运行Spring Security配置Actuators:

management:
  security:
    enabled: true
    role: ADMINISTRATOR
Run Code Online (Sandbox Code Playgroud)

这太容易我需要Actuators使用我们的自定义安全性(此处为CASSSO).

第一次尝试是增加context-pathActuators:

management:
  security:
    enabled: true
    role: ADMINISTRATOR
  context-path: /management
Run Code Online (Sandbox Code Playgroud)

并更新我的WebSecurityConfigurerAdapter配置

@Override
protected void configure(HttpSecurity http) throws Exception {
    ...
    http.authorizeRequests()..antMatchers("/management/**").hasRole(Role.ADMINISTRATOR.toString());
    ...
} 
Run Code Online (Sandbox Code Playgroud)

它工作但我必须硬编码Actuators context-path,所以当我想要更新时,management.context-path我必须更新我的安全性.

我知道可以检索价值management.context-path但是当价值等于时如何管理它""

你可以回答我@Autowired EndpointHandlerMapping并检索Actuators端点列表......最后我将复制过去相同的逻辑ManagementSecurityAutoConfiguration.ManagementWebSecurityConfigurerAdapter.

此外ManagementSecurityAutoConfiguration.ManagementWebSecurityConfigurerAdapter @ConditionalOnMissingBean是指向自身,但是ManagementSecurityAutoConfiguration.ManagementWebSecurityConfigurerAdapter内部静态受保护的类,所以不能在不传递参数的情况下禁用它management.security.enabled=false,这可能很奇怪,因为你的配置说management.security.enabled=false但实际上端点是安全的......


结论

  1. 有没有办法覆盖(只是一部分)正确的Actuators安全性
  2. 我可能会错过一些东西而且我完全错了吗?

Kam*_*kol 1

Github上已经有一个悬而未决的问题。目前 Dave Syer建议

我认为复制粘贴其中的所有代码实际上是目前最好的解决方案(并设置 management.security.enabled=false 让 Boot 知道您想自己执行此操作)。

我还没有测试是否会抛出运行时异常,但我认为您可以重用ManagementWebSecurityConfigurerAdapter并节省大量复制粘贴操作。至少编译器不会抱怨。

将您的配置类放在org.springframework.boot.actuate.autoconfigure项目中的包下并从ManagementWebSecurityAutoConfiguration.ManagementWebSecurityConfigurerAdapter. 不要错过 中的所有注释ManagementWebSecurityConfigurerAdapter。这是这里唯一的复制粘贴操作,因为类注释不能被子类继承。

package org.springframework.boot.actuate.autoconfigure;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

@Configuration
@ConditionalOnProperty(prefix = "management.security", name = "enabled", matchIfMissing = true)
@Order(ManagementServerProperties.BASIC_AUTH_ORDER)
public class SsoManagementWebSecurityConfigurerAdapter extends ManagementWebSecurityAutoConfiguration.ManagementWebSecurityConfigurerAdapter {

    //TODO your SSO configuration

}
Run Code Online (Sandbox Code Playgroud)

不要忘记@Import您的@SpringBootApplication.