如何重新启用对Spring Boot Health端点的匿名访问?

end*_*rec 7 java spring spring-security spring-boot spring-security-oauth2

可能我在这里做错了,我无法弄清楚是什么......

我在同一个应用程序中有一个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();
    }

}
Run Code Online (Sandbox Code Playgroud)

认证服务器配置:

@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");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试访问/健康时,我得到了一个HTTP/1.1 401 Unauthorized.

我怎样才能说服Spring Boot使/健康匿名访问?

Lyj*_*son 5

我有同样的问题并且有点挣扎。

protected void configure(HttpSecurity http) throws Exception {
    ...
    .authorizeRequests()
            .antMatchers("/actuator/**").permitAll()
}
Run Code Online (Sandbox Code Playgroud)

是不足够的。

还要覆盖此方法并添加以下内容并且它可以工作。

public void configure(WebSecurity web) throws Exception {
     web.ignoring().antMatchers("/actuator/**");
}
Run Code Online (Sandbox Code Playgroud)


end*_*rec 4

正如 M. Deinum 所说:

\n\n
\n

您指定映射的顺序也是查询它们的顺序。第一场比赛获胜...因为 /** 匹配\n 你的 /health 映射的所有内容都是无用的。将其移至 /**\n 映射上方以使其正常工作。\xe2\x80\x93 M. Deinum 8 月 20 日 17:56

\n
\n