虽然我使用了授权服务器生成的正确令牌,但我得到了invalid_token

时间:2017-01-08 19:31:11

标签: spring spring-boot spring-security spring-security-oauth2

使用jdbc令牌存储的授权服务器

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends
        AuthorizationServerConfigurerAdapter {

@Autowired
private DataSource dataSource;

private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

@Autowired
private AuthenticationManager authenticationManager;

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

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

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

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

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.jdbc(dataSource);
}

资源服务器

private static final String RESOURCE_ID = "test";

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends
        ResourceServerConfigurerAdapter {


    @Autowired
    private DataSource dataSource;

    @Autowired
    private TokenStore tokenStore;

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

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

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated();
    }

    }
}

ResourceController

@RequestMapping(value = "/transaction", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces =MediaType.APPLICATION_JSON_VALUE)


    public Map<String, Object> haloApi(@RequestBody(required = false) Map<String, String> input){Map<String, Object> data = new HashMap<>();
    data.put("kwame", new Date());
    if(input != null){
        String name = input.get("test");
        if(name != null && !name.isEmpty()){
            data.put("nice", "Hello "+name);
        }
    }

    return data;
}

当我通过cURL向Authorizer Server发送帖子请求时,我收到了一个令牌,但不幸的是,当我在资源服务器中使用它时,它总是返回 invalid token作为回应。

我错过了什么?

请参阅下面我对 Authorizer 资源服务器的示例请求,并提供建议。

授权服务器:

$ curl -X POST -H "Accept: application/json" -d "grant_type=client_credentials" -u "daniel:123456" "http://localhost:5600/oauth/token" 

示例回复:

{
    "access_token": "cddc1b75-87d9-4a2f-9d66-210eae85b0f9",
    "token_type": "bearer",
    "expires_in": 149,
    "scope": "read write"
}

当我立即使用令牌时,如下面的代码段所示,

curl -X POST http://localhost:5700/checkout/transaction -v -H 'Content-Type: application/json' -H 'Authorization: Bearer ac72b34f-437d-4134-8760-16f1ca3f0483' -d '{"test": "test"}'

我不断收到以下回复:

{
    "error": "invalid_token",
    "error_description": "ac72b34f-437d-4134-8760-16f1ca3f0483"
}

以下是我的日志:

2017-01-08 18:45:36.375 DEBUG 11012 --- [io-22000-exec-1] o.s.s.oauth2.client.OAuth2RestTemplate   : GET request for "ip&port/oauth/check_token" resulted in 401 (null); invoking error handler

这里,我的资源服务器application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxxxxx
spring.datasource.username=xxxxxxxxx
spring.datasource.password=xxxxxxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
server.port=5700
security.oauth2.resource.user-info-uri=localhost:5600/oauth/check_token
logging.level.org.springframework.security=DEBUG

2 个答案:

答案 0 :(得分:1)

谢谢大家......我终于发现了这个问题!

我在application.properties中介绍了

<强> security.oauth2.resource.user-INFO-URI = http://localhost:5600/oauth/check_token

因此我只需删除该行即可解决问题。

特别感谢Cleto的关注和贡献。

答案 1 :(得分:0)

您在令牌之前和之后使用引号,请在没有引号的情况下执行请求-H "Authorization: Bearer ac72b34f-437d-4134-8760-16f1ca3f0483"

请确保您的令牌在请求时仍然有效。 (请参阅oauth_client_details表上的access_token_validity列)

编辑:

我创建了一个空白项目,其中包含与您相同的SAME授权和资源服务器,并且我能够使用以下命令发出请求:

  

curl -X POST localhost:**** / transaction -v -H   “Content-Type:application / json”-H“授权:Bearer   4c7591de-a4bc-4896-afc4-ed1fb7199c06“-d”{\“test \”:\“test \”}“

请在每个参数(-H,-d)上使用双引号,并使用。\ / p>转义数据的双引号。

当我测试时,我意识到你没有设置资源的resource_id:

resources.tokenStore(tokenStore).resourceId(RESOURCE_ID);

设置ID后,请确保您的数据库具有正确的oauth_client_details,并且可以访问该资源。

如果您检查一切看起来没问题但是它无效,请调试spring API并查看错误。