为什么我的令牌被拒绝?什么是资源ID? “无效令牌不包含资源ID(oauth2-resource)". [英] Why is my token being rejected? What is a resource ID? "Invalid token does not contain resource id (oauth2-resource)"

查看:3079
本文介绍了为什么我的令牌被拒绝?什么是资源ID? “无效令牌不包含资源ID(oauth2-resource)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为春季项目配置OAuth2.我正在使用共享的UAA(从Cloud Foundry实施oauth )实例我的工作场所提供了(因此,我不尝试创建授权服务器,并且授权服务器与资源服务器是分开的).前端是一个单页应用程序,它使用隐式授予直接从授权服务器获取令牌.我有SPA设置,它在每个Web API调用上将Authorization: Bearer <TOKEN>标头添加到微服务.

I'm trying to configure OAuth2 for a spring project. I'm using a shared UAA (oauth implementation from cloud foundry) instance my work place provides (so I'm not trying to create an authorization server and the authorization server is separate from the resource server). The frontend is a single-page-application and it gets token directly from the authorization server using the implicit grant. I have the SPA setup where it adds the Authorization: Bearer <TOKEN> header on each web API call to microservices.

我的问题现在是微服务.

My issue is now with the microservices.

我正在尝试使用此共享授权服务器对微服务进行身份验证.在这里,我可能会有一个误解,请确保我目前的理解是,这些微服务起着资源服务器的作用,因为它们托管SPA用来获取数据的端点.

I'm trying to use this shared authorization server to authenticate the microservices. I might have a misunderstanding here, buy my current understanding is that these microservices play the role of the resource server because they host the endpoints the SPA uses to get data.

所以我试图像这样配置微服务:

So I tried to configure a microservice like so:

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
        .authorizeRequests()
        .antMatchers("/api/**").authenticated();
    }

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setVerifierKey("-----BEGIN PUBLIC KEY-----<key omitted>-----END PUBLIC KEY-----");
        return converter;
    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        return defaultTokenServices;
    }


    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
         resources.tokenServices(tokenServices());
    }
}

现在,每当我用Authorization: Bearer <TOKEN>击中/api/**时,都会出现此错误的403:

Now whenever I hit a /api/** with the Authorization: Bearer <TOKEN>, I get a 403 with this error:

{
    "error": "access_denied",
    "error_description": "Invalid token does not contain resource id (oauth2-resource)"
}


所以这是我的问题:

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