【问题标题】:Getting Insufficient scope for this resource using Spring Boot and OAuth2使用 Spring Boot 和 OAuth2 获取此资源的范围不足
【发布时间】:2016-08-15 02:27:47
【问题描述】:

我正在尝试使用 Spring Boot 1.2.2 为 REST API 运行一个小型概念验证,并使用 OAuth2 进行保护。为此,我创建了两个项目,一个用于身份验证服务器,另一个用于 REST 服务。

运行项目时,一切正常。我从身份验证服务器获取了一个令牌,然后使用该令牌,我设法调用了该服务并获得了预期的结果。

当我尝试验证服务中的范围时会出现此问题。只要我添加该行:

@PreAuthorize("#oauth2.hasScope('webshop')")

为我服务,在代码中:

@RestController
public class ProductService {

    @RequestMapping("/product/{productId}")
    @PreAuthorize("#oauth2.hasScope('webshop')")
    public Product getProduct(@PathVariable int productId) {

        return new Product(productId, "name", 123);
    }
}

我收到此回复:

{
   "error": "insufficient_scope",
   "error_description": "Insufficient scope for this resource",
   "scope": "webshop"
}

认证服务器配置为:

@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

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

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
            .withClient("acme")
            .secret("acmesecret")
            .authorizedGrantTypes("authorization_code", "refresh_token", "implicit", "password", "client_credentials")
            .scopes("webshop");
    }
}

这是获取令牌的实际调用:

juan@ubuntu:~/ms/core/product-service$ curl -s acme:acmesecret@localhost:9999/uaa/oauth/token -d grant_type=client_credentials -d scope=webshop
{
  "access_token": "ac3a9768-4ebb-44e3-a49f-21e2f117d0b4",
  "token_type": "bearer",
  "expires_in": 43199,
  "scope": "webshop"
}

然后,调用服务:

juan@ubuntu:~/ms/core/product-service$ TOKEN=ac3a9768-4ebb-44e3-a49f-21e2f117d0b4
juan@ubuntu:~/ms/core/product-service$ curl 'http://localhost:8080/product/32' -H "Authorization: Bearer $TOKEN" -s
{
  "error": "insufficient_scope",
  "error_description": "Insufficient scope for this resource",
  "scope": "webshop"
}

我觉得我只是因为缺乏概念而错过了一些简单的东西。如果客户端是在内存中创建的,应该允许做“网店”,并且成功生成了token,为什么会产生错误?

提前感谢您指出一些方向或帮助。

【问题讨论】:

    标签: api rest security spring-security-oauth2


    【解决方案1】:

    这是由于 UserInfoTokenServices 类未能从请求中提取范围,因此未将它们添加到新的 OAuth2Request。

    具体是这个方法

    private OAuth2Authentication extractAuthentication(Map<String, Object> map) {
        Object principal = getPrincipal(map);
        List<GrantedAuthority> authorities = this.authoritiesExtractor
                .extractAuthorities(map);
        OAuth2Request request = new OAuth2Request(null, this.clientId, null, true, null,
                null, null, null, null);
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
                principal, "N/A", authorities);
        token.setDetails(map);
        return new OAuth2Authentication(request, token);
    }
    

    忽略从 oauth2Request 映射条目中提取范围映射,然后将范围集合的 null 传递给 Oauth2Request 范围参数的构造函数。

    这意味着即使您可能已授权用户访问特定范围,但当通过 security.oauth2.resource.userInfoUri 端点查询 authserver 时,在调用代码中会忽略授权范围,因此 PreAuthorize 检查失败.

    这对我来说似乎是一个错误;我提出了an issue on github。

    【讨论】:

    • Spring 的人说我的问题是重复的,但是在 Github 上的链接问题上,有很多关于这个问题以及如何解决它的讨论,所以要获得完整的答案,请通过我上面的问题去那里.
    • 你解决了吗?
    • 那是 5 年前,我不记得了 :-) 你最好的办法是查看与我提出的问题相关的 [GitHub 问题][github.com/spring-projects/spring-boot/issues/5096]。跨度>
    猜你喜欢
    • 2017-02-18
    • 1970-01-01
    • 2020-11-30
    • 2021-02-05
    • 2019-05-01
    • 2023-03-12
    • 2015-05-05
    • 2017-04-13
    • 2021-02-07
    相关资源
    最近更新 更多