我有一个场景,当使用 Okta Oauth2.0 令牌请求时,我需要授予对 /api/* REST API 的访问权限。由于某些用例,由于相同 REST api /api/*
的一些限制,我需要提供基本身份验证场景一:
用户/系统将使用 clientid 和 secret 生成 okta oauth2.0 令牌,并将该 Bearer 令牌发送到 http://localhost:8080/api 进行令牌验证以获取访问权限。
场景二:
user2/system2 将使用用户名/密码作为基本身份验证访问相同的 rest api 路径 http://localhost:8080/api
下面的代码适用于场景 1,但是如何在同一个 spring boot 安全配置项目中集成 oauth2.0 或 basic?请尽可能提供完整的工作代码。
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Order(1)
@Configuration
public static class OauthOktaConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**")
.requestMatcher(new BearerRequestMatcher())
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)
.sessionManagement((sessionManagement) ->
sessionManagement
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
);
Okta.configureResourceServer401ResponseBody(http);
}
}