授权后获取用户角色

Get user role after authorization

为了安全起见,我想使用基于 OAuth2 的 Angular 9 实施 Spring 引导项目。我不清楚的问题是在用户身份验证后获取用户角色的最佳方式是什么。我找到了 2 种方法:

  1. 在使用 OAuth2 对用户进行身份验证后正确添加到响应负载中:

    {
     "access_token": "wdwdw",
       "token_type": "bearer",
       "refresh_token": "wdwdwdwd",
       "expires_in": 4,
       "scope": "read",
       "role": [
         "ROLE_ADMIN"
       ],
       "jti": "wfwfe"
     }
    
  2. 第二种方法是使用第二次 API 调用 oauth/check_token 以获得角色:

$ curl localhost:8080/oauth/check_token/?token=fc9e4ad4-d6e8-4f57-b67e-c0285dcdeb58

{
  "scope": [
    "read",
    "write"
  ],
  "active": true,
  "exp": 1544940147,
  "authorities": [
    "ROLE_USER"
  ],
  "client_id": "ger-client-id"
}

我很感兴趣这两种方式中哪一种在安全性和最佳实践方面更好。

如果您需要根据用户权限控制路由和模块加载,我将采用第一种方式,这样您就不需要为查找权限而触发新请求。

我会更喜欢第一种方式,无论安全凭证将作为对象属性传递,您都可以使用它。