首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring OAuth + JWT -- /oauth/token

Spring OAuth + JWT -- /oauth/token
EN

Stack Overflow用户
提问于 2015-08-18 04:21:59
回答 1查看 4.5K关注 0票数 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?

EN

回答 1

Stack Overflow用户

发布于 2017-02-22 17:26:40

也许你应该使用spring提供的JwtAccessTokenConverter,然后进行适当的配置。下面是一个示例:

代码语言:javascript
复制
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;
}

配置如下:

代码语言:javascript
复制
 @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
            ;
 }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32059064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档