具有OAuth2的REST服务:找不到令牌的访问令牌 [英] Rest service with oauth2: Failed to find access token for token

查看:44
本文介绍了具有OAuth2的REST服务:找不到令牌的访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建Spring REST服务,该服务由我自己的OAuth2资源服务器验证。我创建了资源服务器:

@Configuration
@EnableResourceServer
protected static class ResourceServer extends ResourceServerConfigurerAdapter {

    @Autowired
    private TokenStore tokenStore;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenStore(tokenStore).resourceId("mobileapp");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/api/shop /**").authenticated().and()
                .authorizeRequests().antMatchers("/auth/**").anonymous();
    }

}

和授权服务器:

@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager auth;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Bean
    public JdbcTokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }

    @Bean
    protected AuthorizationCodeServices authorizationCodeServices() {
        return new JdbcAuthorizationCodeServices(dataSource);
    }

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

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .authorizationCodeServices(authorizationCodeServices())
                .authenticationManager(auth)
                .tokenStore(tokenStore())
                .approvalStoreDisabled();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource)
                .passwordEncoder(passwordEncoder);
                .withClient("mobile")
                .authorizedGrantTypes("password", "refresh_token")
                .authorities("ROLE_CLIENT")
                .scopes("read", "write", "trust")
                .autoApprove(true)
               .resourceIds("mobileapp")
                .secret("123456");
    }

当我尝试使用cURL从服务器接收访问令牌时:

Curl-X后VU移动:123456http://localhost:8080/oauth/token-H "接受:应用程序/json"-d "password=test123&;username=admin@gmail.com&;grant_type=password&;scope=read&;client_secret=123456&;client_id=mobile"

我收到此错误作为响应消息:

{"错误":"SERVER_ERROR","error_description":"java.io.NotSerializableException: Org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"}

在Tomcat日志中也有

O.s.s.o.p.token.store.JdbcTokenStore-找不到的访问令牌 令牌

编辑: 密码编码器的Bean定义:

@Bean
public BCryptPasswordEncoder passwordEncoder() {
    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
    return bCryptPasswordEncoder;
}

此Bean是在类中创建的,在类中声明了OAuth2Config和ResourceServer。

我检查了代码,发现表Spring使用的是哪个表,而表是空的。我的问题是:是应该自动生成还是我的代码有问题?

提前感谢您的帮助。

推荐答案

重写JdbcTokenStore类并将此函数替换为。

public OAuth2AccessToken readAccessToken(String tokenValue) {
    OAuth2AccessToken accessToken = null;

    try {
        accessToken = new DefaultOAuth2AccessToken(tokenValue);
    }
    catch (EmptyResultDataAccessException e) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Failed to find access token for token "+tokenValue);
        }
    }
    catch (IllegalArgumentException e) {
        LOG.warn("Failed to deserialize access token for " +tokenValue,e);
        removeAccessToken(tokenValue);
    }

    return accessToken;
}

您找不到访问令牌的问题已解决。请在OAuth2Config中使用此类。

这篇关于具有OAuth2的REST服务:找不到令牌的访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆