invalid_token:无法将访问令牌转换为JSON

时间:2019-04-25 10:04:48

标签: spring-boot oauth-2.0 jwt

刷新令牌时出现错误(grant_type = refresh_token)。似乎用户没有长时间使用该应用程序,并且访问令牌和刷新令牌都已过期。当应用现在尝试刷新令牌时,它会收到错误

{
"error": "invalid_token",
"error_description": "Cannot convert access token to JSON" 
  }

我在这个问题上看到了很多帖子,但是我仍然遇到这个错误。我尝试使用setVerifierKey。但是没有运气。这是代码:

@Configuration
   @EnableWebSecurity
   @EnableGlobalMethodSecurity(prePostEnabled = true)
   public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Value("${security.signing-key}")
private String signingKey;

@Value("${security.encoding-strength}")
private Integer encodingStrength;

@Value("${security.security-realm}")
private String securityRealm;

@Autowired
private UserDetailsService userDetailsService;

@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
    return super.authenticationManager();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService);
          //  .passwordEncoder(new ShaPasswordEncoder(encodingStrength));
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .httpBasic()
            .realmName(securityRealm)
            .and()
            .csrf()
            .disable();

}

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setSigningKey(signingKey);
    return converter;
}

@Bean
public TokenStore tokenStore() {
    return new JwtTokenStore(accessTokenConverter());
}

@Bean
@Primary //Making this primary to avoid any accidental duplication with another token service instance of the same name
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}
}



//AuthorizationServerConfig

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Value("${security.jwt.client-id}")
private String clientId;

@Value("${security.jwt.client-secret}")
private String clientSecret;

@Value("${security.jwt.grant-type}")
private String grantType;

@Value("${security.jwt.scope-read}")
private String scopeRead;

@Value("${security.jwt.scope-write}")
private String scopeWrite = "write";

@Value("${security.jwt.resource-ids}")
private String resourceIds;

@Autowired
private TokenStore tokenStore;

@Autowired
private JwtAccessTokenConverter accessTokenConverter;

@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;

@Override
public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
    configurer
            .inMemory()
            .withClient(clientId)
            .secret(clientSecret)
            .authorizedGrantTypes("client_credentials", "password", "refresh_token", "authorization_code")
            .scopes(scopeRead, scopeWrite)
           // .accessTokenValiditySeconds(60)
           // .refreshTokenValiditySeconds(2000)
            .resourceIds(resourceIds);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
    enhancerChain.setTokenEnhancers(Arrays.asList(accessTokenConverter));
    endpoints.tokenStore(tokenStore)
            .accessTokenConverter(accessTokenConverter)
            .tokenEnhancer(enhancerChain).userDetailsService(userDetailsService)
            .authenticationManager(authenticationManager);
}

}

我希望令牌会被刷新,但是出现上述错误。我的配置属性是:

  • security.oauth2.resource.filter-order = 3
  • security.signing-key = ZMaasazkSjmaasw
  • security.encoding-strength = 256
  • security.security-realm = Spring Boot JWT示例领域
  • security.jwt.grant-type = password security.jwt.scope-read = read
  • security.jwt.scope-write = write
  • security.jwt.resource-ids = testjwtresourceid

任何帮助表示赞赏!

0 个答案:

没有答案