小编典典

Spring Boot OAuth2管理HTTP基本认证

spring-boot

我有一个使用oauth2进行身份验证的spring boot应用程序。oauth2机制正在运行,客户端可以进行身份​​验证并接收其访问令牌。

我想通过httpbasic身份验证来保护执行器端点,即不要求用户先使用oauth2进行身份验证,然后再访问执行器端点。到目前为止,我所做的是在属性文件中设置以下内容:

management.context-path=/admin/actuators
management.security.enabled=true
management.security.role=ADMIN

security.user.name=admin
security.user.password=password

我尝试了各种方法来使用ResourceServerConfigurerAdapter和WebSecurityConfigurerAdapter设置配置。

我的尝试都无济于事,它不断告诉我

<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>

使OAUTH2和管理端点正常工作的正确方法是什么?


阅读 342

收藏
2020-05-30

共1个答案

小编典典

好的,使用下面的java配置使其工作。

任何人都可以访问端点/ admin / actuators / health,并且所有其他/ admin / actuators /
*端点均已通过身份验证。

@Configuration
@Order(1)
protected static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) throws Exception {

        http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/admin/actuators/health").permitAll()
            .and()
                .antMatcher("/admin/actuators/**")
                .authorizeRequests()
                .anyRequest()
                .hasRole("ADMIN")
                .and()
                .httpBasic();
    }
}
2020-05-30