Spring 引导管理安全性与端口集的工作方式不同



我正在尝试配置一个具有执行器支持的 Spring 启动应用程序(1.2.3,但在 1.2.4.BUILD-SNAPSHOT 版本中也会失败)。我想使用 Actuator 安全配置来控制对管理端点的访问,以及我们自己对应用程序其余部分的身份验证。

这是我的安全配置:

@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
    @Autowired
    private CustomAuthenticationProvider customAuthProvider;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.authenticationProvider(customAuthProvider);
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
            .authorizeRequests()
            .regexMatchers(API_DOC_REGEX).permitAll()
            .regexMatchers(String.format(PATH_REGEX, PUBLIC_ACCESS)).permitAll()
            .regexMatchers(String.format(PATH_REGEX, INTERNAL_ACCESS)).access("isAuthenticated() && authentication.hasOrigin('INTERNAL')")
            .regexMatchers(String.format(PATH_REGEX, EXTERNAL_AUTHENTICATED_ACCESS)).authenticated()
            .antMatchers("/**").denyAll()
            .and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
            .and()
            .addFilterAfter(customAuthProcessingFilter(), BasicAuthenticationFilter.class)
            .csrf().disable();
    }
}
当我不设置管理端口时,

这可以正常工作,但是当我设置管理端口时,管理 URL 会返回 401 响应。如果我注释掉.antMatchers("/**").denyAll()行,那么一切都不需要身份验证。因此,当我设置自定义端口时,它似乎正在使用我的应用程序对执行器端点的安全配置,但我不确定为什么。

在自定义端口上运行时,如何让它使用自己的安全性?

扩展 @M. Deinum 的评论,为管理内容添加另一个适配器(即使它已经有一个)似乎已经修复了它。这是我最终上过的课:

@Order(0)
@Configuration
public class ManagementSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Autowired
    ManagementServerProperties managementProperties;
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
                .requestMatchers()
                .requestMatchers(new RequestMatcher()
                {
                    @Override
                    public boolean matches(HttpServletRequest request)
                    {
                        return managementProperties.getContextPath().equals(request.getContextPath());
                    }
                })
                .and()
                .authorizeRequests()
                .anyRequest().hasRole("ADMIN")
                .and()
                .httpBasic();
    }
}

最新更新