【问题标题】:ImplicitAudienceValidatingTokenServices missing from new okta-spring-boot 2.1.1新的 okta-spring-boot 2.1.1 中缺少 ImplicitAudienceValidatingTokenServices
【发布时间】:2021-12-08 15:51:14
【问题描述】:

我已将我们的 Spring Boot 应用程序从使用 okta-spring-boot-starter v0.61 升级到 v2.1.1

微服务现在总是抛出:

p.a.OAuth2AuthenticationProcessingFilter : Authentication request failed: error="invalid_token", error_description="Invalid access token:  ... TOKEN OMITTED

新的 (v2.1.1) OAuth2AuthenticationManager 与 org.springframework.security.oauth2.provider.token.DefaultTokenServices 的实例相关联,该实例具有空的 hashmap 并导致异常(因为 OAuth2AuthenticationManager从 tokenServices.loadAuthentication(token) 获取 null 返回)

OLD (v0.61) 实现使用 com.okta.spring.oauth.implicit.ImplicitAudienceValidatingTokenServices

的实例

这是在 ResourceServerConfig 内的旧代码中自动创建的

ImplicitAudienceValidatingTokenServices 现在从 okta-spring-boot 中消失了,ResourceConfig 也是如此。我不清楚如何在 Okta Spring Boot Starter v2.1.1 中启用相同的行为。

关于我缺少哪些配置/属性来恢复旧行为的任何想法?我不相信 Okta 甚至支持“远程令牌验证”(不确定准确的技术短语)。本地验证仍然适用于我们的目的。

【问题讨论】:

    标签: spring-boot spring-security-oauth2 okta


    【解决方案1】:

    这些版本之间发生了很大变化。其中最大的是 Spring Security 的 OAuth 支持(Okta lib 位于其之上)。确保您的类路径 spring-security-oauth 上没有旧的 Spring Sec 库。 related migration guide 也可能对您有所帮助(取决于您在做什么)。

    从这里有两件事需要配置:

    1. 您的配置属性(应该保持不变okta.oauth2.*
    2. 配置资源服务器。

    对于最后一个,剩下的就是 Spring Security API 来配置资源服务器:

    import com.okta.spring.boot.oauth.Okta;
    import org.springframework.http.HttpMethod;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    
    @EnableWebSecurity
    public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http.authorizeRequests()
                // all requests are authenticated
                .anyRequest().authenticated()
                .and()
                .oauth2ResourceServer().jwt(); // replace .jwt() with .opaqueToken() for Opaque Token case
    
            // Send a 401 message to the browser (w/o this, you'll see a blank page)
            // this is optional, and only needed if people are accessing your API manually through a browser
            Okta.configureResourceServer401ResponseBody(http);
        }
    }
    

    您可以在此处查看完整示例:https://github.com/okta/samples-java-spring/blob/master/resource-server/src/main/java/com/okta/spring/example/ResourceServerExampleApplication.java(只需添加您的属性)

    Okta 特定位已自动连接和配置,例如,Okta 特定 JWT 验证(尚无官方 JWT 访问令牌规范)。

    另外,对于“远程令牌验证”,它在 Spring Sec API 中被称为“不透明”,请参阅代码块中.opaueToken() 的注释。

    【讨论】:

    • 谢谢@Brian。我能够用你的提示解决问题。我将在下面添加更详细的回复。
    【解决方案2】:

    如前所述,我能够解决问题。

    通过上面的示例,我还找到了另一个示例。第一个更具体到一般 Spring:https://github.com/spring-projects/spring-security-samples/blob/main/servlet/spring-boot/java/oauth2/resource-server/hello-security/src/main/java/example/OAuth2ResourceServerSecurityConfiguration.java

    由于我使用的是 okta-spring-boot,所以我使用了 Brian 的示例:https://github.com/okta/samples-java-spring/blob/master/resource-server/src/main/java/com/okta/spring/example/ResourceServerExampleApplication.java

    我做的第一件事是为 Spring Boot 添加两个新属性。这令人困惑,因为我相信相同属性的名称在不同版本的 Spring 中至少有一次非常微妙的变化。有一些不同的例子会出现在谷歌上,并且不会立即被注意到。这些对于 Spring Boot 2.5.4 和 Spring Core 5.3.9 是正确的:

    spring.security.oauth2.resourceserver.jwt.issuer-uri: https://your domain.okta.com/oauth2/your_auth_server_id(可能需要 /default,具体取决于您的应用程序的设置方式)

    spring.security.oauth2.resourceserver.jwt.jwk-set-uri: url_above/v1/keys

    我必须删除 @EnableResourceServer 并添加(基于上面的 Okta 示例)注释 @EnableGlobalMethodSecurity(prePostEnabled = true, secureEnabled = true)

    在我的例子中,我们有一个 ResourceServerConfigurerAdapter 的实现,我更新它以扩展 WebSecurityConfigurerAdapter 并使用 API 创建资源服务器:.and().oauth2ResourceServer().jwt();

    我还需要做进一步的测试并想了解 EnableGlobalMethodSecurity 上的选项,但目前我的 REST 端点将再次正确接受 Bearer 令牌。

    【讨论】:

      猜你喜欢
      • 2020-03-13
      • 2019-05-04
      • 2018-05-21
      • 2023-03-31
      • 2016-02-14
      • 2016-06-02
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多