Java JWT无法将访问令牌转换为JSON

Java JWT无法将访问令牌转换为JSON,java,spring-security,Java,Spring Security,我知道有人问过这个问题。然而,在这个问题上没有一行代码。我将分享我的代码,以便它可以帮助我和其他可能面临相同问题的人 下面是代码 @Configuration @EnableAuthorizationServer public class OAuth2Config extends AuthorizationServerConfigurerAdapter { @Autowired private PasswordEncoder passwordEncoder; @Autowired priva

我知道有人问过这个问题。然而,在这个问题上没有一行代码。我将分享我的代码,以便它可以帮助我和其他可能面临相同问题的人

下面是代码

@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

@Autowired
private PasswordEncoder passwordEncoder;

@Autowired
private UserDetailsService userDetailsService;

@Autowired
@Qualifier(value = "authenticationManager")
private AuthenticationManager authenticationManager;

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security.allowFormAuthenticationForClients().passwordEncoder(passwordEncoder);
}

/*
 * Não remover, Configura os Endpoints para o oAuth2
 */
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
    tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));
    endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore()).tokenEnhancer(tokenEnhancer())
            .userDetailsService(userDetailsService);
}

@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}

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

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    final KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("mytest.jks"),
            "mypass".toCharArray());
    converter.setKeyPair(keyStoreKeyFactory.getKeyPair("mytest"));
    return converter;
}

@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    defaultTokenServices.setReuseRefreshToken(false);
    return defaultTokenServices;
}
}

}

因此,我使用以下命令创建了一个带有辅助键的JWT:

keytool-genkeypair-别名mytest -keyalg RSA -密钥传递mypass -keystoremytest.jks -storepass我的通行证

由于我的授权服务器与资源服务器位于同一位置,因此我将.jks和public.txt(包含公钥)添加到项目中

已正确生成令牌。。。。以下是一个例子:

EYJHBGCIOIJUZI1NIISINR5CCI6IKPXVCJ9.EYJ1C2VYX25HBWUIOIYB290QDEWZDQWMTK4LWE0OTUTNDJMNC05NDLWYWOTQ1NZFMNDBMOCISIMWUDF9PZCI6IJVCMDL6YXRPB24IYB290QDEWFHQQGIFQ.MHURNG2V6M9RXTYxODEOPVUKK0N9IVUK0N9IVNJAUL0VP0

但是,发送此令牌以从服务器获取任何资源时,我出现以下错误:

{ “错误”:“无效的\u令牌”, “错误描述”:“无法将访问令牌转换为JSON” }

那么,缺少什么呢

编辑

服务器记录:

> Caused by: java.security.SignatureException: Signature length not correct: 
got 32 but was expecting 256
at sun.security.rsa.RSASignature.engineVerify(RSASignature.java:189)
at java.security.Signature$Delegate.engineVerify(Signature.java:1219)
at java.security.Signature.verify(Signature.java:652)
at org.springframework.security.jwt.crypto.sign.RsaVerifier.verify(RsaVerifier.java:54)
... 57 more
编辑2


我不知道为什么否决了这个问题。如果有什么可以补充的,请留下评论,我会尽力改进问题。

您最好将密钥库的详细信息放入
应用程序.yml
(或
应用程序.properties
)中,如下所示:

encrypt:
  key-store:
    location: mytest.jks
    alias: mytest
    password: mypass
    secret: mypass
然后您可以在
OAuth2Config
ResourceServer
中简化代码:

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    return converter;
}
encrypt:
  key-store:
    location: mytest.jks
    alias: mytest
    password: mypass
    secret: mypass
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    return converter;
}