春季安全 OAuth2 JWT 刷新令牌返回"Authentication failed: invalid_token Cannot convert access token to JSON"



我已经使用JWT和初始身份验证实现了spring-security-oauth2,并且对资源的请求正常工作,令牌增强器也正常工作。当我尝试使用刷新令牌获得新的JWT时,我会得到错误"cannot convert access token to JSON"

public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    // ....
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .tokenStore(tokenStore())
                .authenticationManager(authenticationManager)
                .accessTokenConverter(accessTokenConverter())
                .reuseRefreshTokens(false)
                .userDetailsService(userDetailsService);
    }
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }
    @Bean
    JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new CustomTokenEnhancer();
        converter.setSigningKey(jwtSigningKey);
        converter.setVerifierKey(jwtSigningKey);
        return converter;
    }
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        Base64Utility base64 = new Base64Utility();
        clients.inMemory()
                .withClient(ApplicationConstants.CLIENT)
                .resourceIds(securityConstants.audience)
                .secret(...)
                .scopes(AuthorizationConstants.READ)
                .authorizedGrantTypes("password", "refresh_token")
                .accessTokenValiditySeconds(securityConstants.getAccessTokenValiditySeconds())
                .refreshTokenValiditySeconds(securityConstants.getRefreshTokenValiditySeconds());
    }
}

返回的Access_Token是普通的three.part.tokenrefresh_token是这样:22cc0513-8a19-42bb-9bd4-631c6758a273

我使用此JavaScript代码尝试刷新它:

function refreshToken() {
    var client = jwtForm.client.value;
    var clientSecret = getClientSecret();
    var data = "grant_type=refresh_token&refresh_token=" + jwt.refresh_token;
    var xhr = new XMLHttpRequest();
    xhr.open("POST", authServer + "/oauth/token");
    xhr.setRequestHeader ("Authorization", "Basic " + btoa(client + ":" + atob(clientSecret)));
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function () {
        if(xhr.readyState == 4) {
            processResponse(xhr);
        }
    };
    xhr.send(data);
}

有什么想法?

问题在我的自定义jwtaccesstokenconverter中。如果您不打算创建自己的刷新令牌,则必须在增强方法中调用Super.senchance。一旦我打电话给超级。

相关内容