Spring security 在不丢失可配置端点的情况下重写spring安全执行器

Spring security 在不丢失可配置端点的情况下重写spring安全执行器,spring-security,spring-boot,spring-boot-actuator,Spring Security,Spring Boot,Spring Boot Actuator,我正在尝试保护项目中Spring Boot内部执行器的端点。但是,对于执行器,使用准备运行的弹簧安全配置: management: security: enabled: true role: ADMINISTRATOR management: security: enabled: true role: ADMINISTRATOR context-path: /management 这太容易了,我需要用我们的自定义安全性(这里是CASSSO)插入执行

我正在尝试保护
项目中
Spring Boot
内部
执行器的端点。但是,对于
执行器
,使用准备运行的
弹簧安全
配置:

management:
  security:
    enabled: true
    role: ADMINISTRATOR
management:
  security:
    enabled: true
    role: ADMINISTRATOR
  context-path: /management
这太容易了,我需要用我们的自定义安全性(这里是
CAS
SSO)插入
执行器

首先尝试为
执行器添加
上下文路径

management:
  security:
    enabled: true
    role: ADMINISTRATOR
management:
  security:
    enabled: true
    role: ADMINISTRATOR
  context-path: /management
并更新我的
websecurityConfigureAdapter
配置

@Override
protected void configure(HttpSecurity http) throws Exception {
    ...
    http.authorizeRequests()..antMatchers("/management/**").hasRole(Role.ADMINISTRATOR.toString());
    ...
} 
它可以工作,但我必须硬编码
执行器
上下文路径
,因此当我想更新
管理。上下文路径
时,我必须更新我的安全性

我知道可以检索
management.context路径的值,但是当值等于
时如何管理它

您可以回答我的
@Autowired
EndpointHandlerMapping
,并检索
执行器的列表
端点。。。最后,我将复制与
ManagementSecurityAutoConfiguration.ManagementWebSecurityConfigurerAdapter
相同的逻辑

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


结论

  • 是否有一种方法可以正确地覆盖(只是部分)执行器
    安全性
  • 我可能会错过什么,而我完全错了吗

  • 已经有一个悬而未决的问题。目前,戴夫·赛尔:

    我认为所有代码的复制粘贴实际上是最好的 现在的解决方案(并设置management.security.enabled=false以允许 我知道你想自己做)

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

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

    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
    
    }
    

    别忘了在你的
    @springboot应用程序中导入你的配置

    好的,我做了同样的事情,只是我没有使用
    Spring
    软件包,我只是复制了我需要的一段代码。我会接受这个答案,因为它证明并没有合适的方式来做我想做的事情。我会在github上跟踪这个问题,我希望有一天它会被修复