Spring REST API 相同路径 /api/ 需要使用 Okta Oauth2.0 或基本身份验证进行身份验证

问题描述 投票:0回答:0

我有一个场景,当使用 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);
            } 
}
spring spring-boot oauth-2.0 basic-authentication spring-boot-security
© www.soinside.com 2019 - 2024. All rights reserved.