简体   繁体   English

Spring OAuth2 服务器无法使用资源所有者凭据(密码)授予流程刷新令牌

[英]Spring OAuth2 server cannot refresh token with Resource owner credentials (password) grant flow

I have configured an OAuth2 authorisation server with spring security oauth, using jwt tokens:我已经使用 jwt 令牌配置了一个带有 spring security oauth 的 OAuth2 授权服务器:

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

...
    @Override
    public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource).passwordEncoder(passwordEncoder);
    }

    @Bean
    public ApprovalStore approvalStore() {
        return new JdbcApprovalStore(dataSource);
    }

    @Bean
    public TokenStore tokenStore() {
        var jwtTokenStore = new JwtTokenStore(tokenConverter());
        jwtTokenStore.setApprovalStore(approvalStore());
        return jwtTokenStore;
    }

    @Bean
    public JwtAccessTokenConverter tokenConverter() {
        var converter = new JwtAccessTokenConverter();
        var keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource(jwtKeyStore), jwtKeyPass.toCharArray());
        converter.setKeyPair(keyStoreKeyFactory.getKeyPair("jwtkey"));
        return converter;
    }

}

There is a client that has password and refresh_token grants.有一个具有passwordrefresh_token授权的客户端。 I can get access and refresh tokens with the following request:我可以通过以下请求获取访问和刷新令牌:

curl --request POST \
   --url 'http://localhost:8080/oauth/token?grant_type=password&scope=read' \
  --header 'authorization: Basic <xxxxxxx>' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data 'username=xxxxxxx&password=xxxxxxx'

Response:回复:

{
    "access_token": "<long access token>",
    "token_type": "bearer",
    "refresh_token": "<long refresh token>",
    "expires_in": 599,
    "scope": "read",
    "subject": "xxx",
    "jti": "xxx"
}

However, when I try to refresh the token, I get an error Invalid refresh token .但是,当我尝试刷新令牌时,出现错误Invalid refresh token Further debugging into Spring codes I see that on the first request, it doesn't insert a row into oauth_approvals table.进一步调试 Spring 代码我看到,在第一个请求中,它没有在oauth_approvals表中插入一行。 And on the second request (refreshing the token) it thinks that the user has not approved the scope (although I have autoapprove=true ).在第二个请求(刷新令牌)时,它认为用户没有批准范围(尽管我有autoapprove=true )。

This is not the case with implicit or authorization_code grant flow: in those cases it does insert a row into oauth_approvals table, and the token is refreshed successfully.这不是implicitauthorization_code授权流的情况:在这些情况下,它确实在oauth_approvals表中插入了一行,并且令牌被成功刷新。

Is this a bug in Spring OAuth or is there any workaround?这是 Spring OAuth 中的错误还是有任何解决方法?

After digging more into Spring's codes, I came to conclusion that this is indeed a bug there.在深入研究 Spring 的代码之后,我得出结论,这确实是一个错误。 So I extended JdbcApprovalStore and used that one instead.所以我扩展了JdbcApprovalStore并使用了那个。 Here is pseudo-code这是伪代码

public class JdbcApprovalStoreAutoApprove extends JdbcApprovalStore {
    ...
    @Override
    public List<Approval> getApprovals(String userName, String clientId) {
        if (client has auto approved scopes) {
            return those scopes;
        }
        return super.getApprovals(userName, clientId);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring安全性Oauth2资源所有者密码凭证授权 - Spring security Oauth2 Resource Owner Password Credentials Grant 使用 OAuth2 资源所有者密码授予类型在 Spring Cloud Gateway 中创建路由 - Create route in Spring Cloud Gateway with OAuth2 Resource Owner Password grant type Spring 安全 5.3.2 OAuth 2,资源所有者密码凭证流程 - 如何将额外的 HEADER 参数添加到授权服务器 uri - Spring Security 5.3.2 OAuth 2, Resource Owner Password Credentials Flow - How to add additional HEADER parameters to authorization server uri 在Spring Security OAuth2中使用用户名密码授予中的刷新令牌请求新的访问令牌 - Request new access token using refresh token in username-password grant in Spring Security OAuth2 在Android上使用资源所有者密码凭据实施OAuth2 - Implement OAuth2 with resource owner password credentials on Android 具有使用资源所有者密码凭证oauth流的多个客户端的中央身份验证服务器 - Central auth server with multiple clients using resource owner password credentials oauth flow 在Spring Oauth2密码授予中无法获取访问令牌 - Unable to get the access token in Spring Oauth2 password grant spring oauth2 客户端凭据流中的令牌交换 - Token exchange in spring oauth2 client credentials flow Spring OAuth2 服务器在授权代码流后没有响应刷新令牌 - Spring OAuth2 Server is not responding with refresh token after authorization code flow Spring OAuth2服务器通过密码授予访问权限被拒绝(用户为匿名用户) - spring oauth2 server grant by password Access is denied (user is anonymous)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM