10

我正在尝试保护项目Actuators内部的端点Spring Boot。但是,改为使用准备运行的Spring Security配置Actuators

management:
  security:
    enabled: true
    role: ADMINISTRATOR

这太容易了,我需要插入Actuators我们的自定义安全性(这里是CASSSO)。

首先尝试它是context-path添加Actuators

management:
  security:
    enabled: true
    role: ADMINISTRATOR
  context-path: /management

并更新我的WebSecurityConfigurerAdapter配置

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

它可以工作,但我必须硬编码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. 我可以错过一些东西,我完全错了吗?
4

1 回答 1

1

Github上已经有一个待处理的问题。目前 Dave Syer建议

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

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

将您的配置类放在项目中的 package 下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

}

不要忘记@Import在您的@SpringBootApplication.

于 2015-11-20T08:53:09.870 回答