无法将访问令牌转换为JSON

问题描述

我在我的授权服务器Spring Cloud中使用Oauth2.0,将令牌存储在数据库中,使用jdbcTokenStore 而不是JwtTokenStore来正常工作,但是当我发出http请求时,邮递员返回此错误。 { “错误”:“ invalid_token”, “ error_description”:“无法将访问令牌转换为JSON” } 这是因为我没有实现方法 accesstokenconverter

我一直在寻找如何实现这种方法,但是我还没有走运。 你能帮我吗

package com.restaurants.serviciooauth.security;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;

import javax.sql.DataSource;


@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
 private  BCryptPasswordEncoder passwordEncoder;

    @Autowired
    AuthenticationManager authenticationManager;

    //configuration to jdbc
    @Autowired
    private DataSource dataSource;

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

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
       security.tokenKeyAccess("permitAll()")
       .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
     clients.inMemory().withClient("frontendapp")
             .secret(passwordEncoder.encode("12345"))
             .scopes("read","write")
     .authorizedGrantTypes("password","refresh_token")
     .accessTokenValiditySeconds(3600)
     .refreshTokenValiditySeconds(3600)
             .and()
             .withClient("androidapp")
             .secret(passwordEncoder.encode("12345"))
             .scopes("read","write")
             .authorizedGrantTypes("password","refresh_token")
             .accessTokenValiditySeconds(3600)
             .refreshTokenValiditySeconds(3600);
    }

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


}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)