【问题标题】:How to re-enable anonymous access to Spring Boot Health endpoint?如何重新启用对 Spring Boot Health 端点的匿名访问?
【发布时间】:2015-11-14 07:05:46
【问题描述】:

可能我在这里做错了什么,我只是不知道是什么......

我在同一个应用程序中有一个 Oauth2 身份验证服务器和一个资源服务器。

资源服务器配置:

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER-1)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    public static final String RESOURCE_ID = "resources";

    @Override
    public void configure(final ResourceServerSecurityConfigurer resources) {
        resources
                .resourceId(RESOURCE_ID);
    }

    @Override
    public void configure(final HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/**").access("#oauth2.hasScope('read')")
                .antMatchers(HttpMethod.POST, "/**").access("#oauth2.hasScope('write')")
                .antMatchers(HttpMethod.PUT, "/**").access("#oauth2.hasScope('write')")
                .antMatchers(HttpMethod.PATCH, "/**").access("#oauth2.hasScope('write')")
                .antMatchers(HttpMethod.DELETE, "/**").access("#oauth2.hasScope('write')")
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .antMatchers(HttpMethod.GET, "/health").permitAll();
    }

}

认证服务器配置:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userDetailsService)
                .passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().authenticated()
                .and().httpBasic().realmName("OAuth Server");
    }
}

当我尝试访问 /health 时,我得到了 HTTP/1.1 401 Unauthorized。

如何说服 Spring Boot 让 /health 匿名访问?

【问题讨论】:

  • 在 SecurityConfig 中,我认为您错过了添加: .antMatchers(HttpMethod.GET, "/health").permitAll();
  • 您指定映射的顺序也是查询它们的顺序。第一场比赛获胜......因为/** 匹配所有你的/health 映射是无用的。将其移至 /** 映射上方以使其正常运行。
  • @M.Deinum 谢谢,这解决了问题。如果您将此添加为答案,我很乐意接受。

标签: java spring spring-security spring-boot spring-security-oauth2


【解决方案1】:

我有同样的问题,有点挣扎。

protected void configure(HttpSecurity http) throws Exception {
    ...
    .authorizeRequests()
            .antMatchers("/actuator/**").permitAll()
}

还不够。

也覆盖此方法并添加以下内容即可。

public void configure(WebSecurity web) throws Exception {
     web.ignoring().antMatchers("/actuator/**");
}

【讨论】:

  • 一整天都在做。缺少了第二部分。谢谢
【解决方案2】:

正如 M. Deinum 所说:

您指定映射的顺序也是 他们被咨询。第一场比赛获胜......因为 /** 匹配 您的 /health 映射的所有内容都是无用的。将其移到 /** 上方 映射使其正常工作。 – M. Deinum 8 月 20 日 17:56

【讨论】:

    【解决方案3】:

    【讨论】:

    • 此解决方案将禁用所有执行器端点的安全性。问题是关于如何允许匿名访问健康端点。
    猜你喜欢
    • 2016-09-01
    • 1970-01-01
    • 2019-11-23
    • 2019-02-03
    • 2018-06-18
    • 2018-08-08
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多