Spring OAuth + JWT -- /oauth/token

6
我正在尝试使用https://github.com/spring-projects/spring-security-oauth配置我的Spring应用程序以使用JWT。我公开了一个由JwtTokenStore支持的ConsumerTokenServices bean,但是访问/oauth/token并没有给我JWT。 $ curl localhost:8643/contextpath/oauth/token?grant_type=client_credentials -u user:password` {"access_token":"a78a6225-78d5-4cb8-9393-6c0b567a6f24","token_type":"bearer","expires_in":5684,"scope":"read write"}%
我知道正在使用TokenStore,因为访问check_token会产生错误,而之前没有出现过。 $ curl https://localhost:8643/context/oauth/check_token?token=a78a6225-78d5-4cb8-9393-6c0b567a6f24 {"error":"invalid_token","error_description":"Cannot convert access token to JSON"}%
如何使我的TokenEndpoint返回JWT?

你尝试使用Spring Boot 1.3制作示例应用程序了吗?它自带了OAuth2的自动支持,请参见 http://spring.io/blog/2015/11/16/spring-boot-1-3-0-released。我建议您尝试构建一个样例应用程序,然后查看 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/OAuth2AutoConfiguration.java 和 https://github.com/dynamind/spring-boot-security-oauth2-minimal。另一个不错的博客系列是 https://spring.io/blog/2015/02/03/sso-with-oauth2-angular-js-and-spring-security-part-v。 - ChrLipp
请查看https://github.com/dynamind/spring-boot-security-oauth2-minimal。 - Ali Dehghani
你能解决这个问题吗? - Amir
我最终没有使用Spring。 - Lee Avital
1个回答

1
也许你应该使用Spring提供的JwtAccessTokenConverter,然后进行适当的配置。以下是一个示例:
public class YourTokenEnhancer extends JwtAccessTokenConverter {

//you can autowire sth for you logic

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
                                 OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken customAccessToken = new DefaultOAuth2AccessToken(accessToken);

    OAuth2AccessToken enhancedToken = super.enhance(customAccessToken, authentication);
    return enhancedToken;
}

"而且配置如下:"
 @Configuration
 @EnableAuthorizationServer
 public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
 //other config...
 @Bean
 public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new YourTokenEnhancer();
    converter.setSigningKey("secret");
    return converter;
 }

 @Override
 public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

    endpoints.authenticationManager(authenticationManager)
            .tokenStore(redisTokenStore())
            .tokenServices(authorizationServerTokenServices())
            .accessTokenConverter(accessTokenConverter())//configure it here
            ;
 }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接