【问题标题】:Spring OAuth2 server cannot refresh token with Resource owner credentials (password) grant flowSpring OAuth2 服务器无法使用资源所有者凭据(密码)授权流程刷新令牌
【发布时间】:2019-05-26 05:24:40
【问题描述】:

我已经使用 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;
    }

}

有一个客户端拥有passwordrefresh_token 授权。我可以通过以下请求获取访问和刷新令牌:

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'

回复:

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

但是,当我尝试刷新令牌时,我收到错误 Invalid refresh token。进一步调试 Spring 代码我看到在第一个请求中,它不会在 oauth_approvals 表中插入一行。在第二个请求(刷新令牌)中,它认为用户尚未批准范围(尽管我有 autoapprove=true)。

implicitauthorization_code 授权流程不是这种情况:在这些情况下,它会在oauth_approvals 表中插入一行,并且成功刷新了令牌。

这是 Spring OAuth 中的错误还是有任何解决方法?

【问题讨论】:

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


    【解决方案1】:

    在深入研究 Spring 的代码后,我得出结论,这确实是一个错误。所以我扩展了JdbcApprovalStore 并使用了那个。这是伪代码

    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);
        }
    

    【讨论】:

    • 您应该将该错误报告给 Spring Security 团队以对其进行验证和修复。
    猜你喜欢
    • 2017-10-08
    • 2013-11-23
    • 2014-09-02
    • 2018-05-25
    • 2021-07-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多