【问题标题】:Spring OAuth + JWT -- /oauth/tokenSpring OAuth + JWT -- /oauth/token
【发布时间】:2015-11-10 14:27:14
【问题描述】:

我正在尝试使用 https://github.com/spring-projects/spring-security-oauth 将我的 Spring 应用程序配置为使用 JWT。我已经为ConsumerTokenServices 公开了一个由JwtTokenStore 支持的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 spring-security jwt spring-security-oauth2


【解决方案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
            ;
 }
}

【讨论】:

    猜你喜欢
    • 2016-08-25
    • 2017-01-07
    • 2018-04-26
    • 2015-05-28
    • 2018-10-16
    • 2023-03-08
    • 2017-10-27
    • 2017-08-31
    • 2017-08-31
    相关资源
    最近更新 更多