【问题标题】:OAuth2 different token expiration time per client每个客户端的 OAuth2 不同令牌过期时间
【发布时间】:2022-01-15 07:07:23
【问题描述】:

我正在使用spring-security-oauth2 来实现我的 OAuth2 授权服务器。 spring-security-oauth2 即将消失,我知道我需要将其替换为 spring-authorization-server

问题: 是否可以为不同的客户端设置不同的令牌过期时间(这里的客户端代表客户端 ID/客户端密钥对)?

如果是,您能否分享 spring-authorization-server 周围的文档/示例代码?

如果不是,是 spring-authorization-server 的限制还是 OAuth2 规范不允许?

(澄清一下,我并不是说它在 spring-security-oauth2 中是可能的,如果我也想知道的话)

【问题讨论】:

    标签: java spring-security oauth-2.0 spring-security-oauth2


    【解决方案1】:

    是的,您可以为每个客户端设置不同的到期时间。您将使用每个RegisteredClienttokenSettings,如下例所示:

    RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
                    .clientId("messaging-client")
                    .clientSecret("{noop}secret")
                    .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                    .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                    .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                    .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
                    .redirectUri("http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc")
                    .redirectUri("http://127.0.0.1:8080/authorized")
                    .scope(OidcScopes.OPENID)
                    .scope("message.read")
                    .scope("message.write")
                    .tokenSettings(TokenSettings.builder()
                            .accessTokenTimeToLive(Duration.ofMinutes(5))
                            .refreshTokenTimeToLive(Duration.ofHours(2))
                            .build())
                    .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
                    .build();
    

    请参阅sample config 了解完整上下文。

    【讨论】:

    • 您能否澄清注册客户端的客户端 ID/秘密与将用于客户端凭据授予流程的客户端 ID/秘密之间的区别?我一直在寻找一个没有运气的区别。
    • 对不起@Scott,我不确定我是否理解。我不认为有区别。
    • 谢谢,史蒂夫。我很困惑,因为我没有意识到注册的客户端和客户端凭据是相同的。我认为这部分是因为我还没有看到公共 API 的未经身份验证的注册客户端的示例,该 API 仅使用授权代码和刷新授权流。
    • 明白了。仅供参考,我们实际上正在研究公共客户样本,并于 2022 年 3 月 10 日就该主题发送webinar。同时,here's an example RegisteredClient 用于公共客户。请注意,在这种情况下不会发出刷新令牌。
    猜你喜欢
    • 2015-11-10
    • 1970-01-01
    • 2021-06-05
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 2013-11-02
    • 2020-06-22
    相关资源
    最近更新 更多