授权后获取用户角色

时间:2020-07-22 23:12:35

标签: angular spring-security-oauth2 angular9

为了安全起见,我想使用基于OAuth2的Angular 9实现Spring Boot Project。对我来说尚不明确的问题是,在用户身份验证之后获得用户角色的最佳方法是什么。我发现了两种方法:

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

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

$ 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"
}

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

2 个答案:

答案 0 :(得分:1)

如果您需要根据用户权限控制路由和模块的加载,我将采用第一种方法,因此您无需为查找权限而触发新请求。

答案 1 :(得分:0)

无论安全证书将作为对象属性通过什么,您都可以使用它。