Spring引导oauth2管理httpbasic认证

ger*_*ard 5 java security spring spring-boot spring-security-oauth2

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

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

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

security.user.name=admin
security.user.password=password
Run Code Online (Sandbox Code Playgroud)

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

我的尝试都没有奏效,它一直在告诉我

<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>
Run Code Online (Sandbox Code Playgroud)

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

ger*_*ard 0

好的,使用以下 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();
    }
}
Run Code Online (Sandbox Code Playgroud)