【问题标题】:how to include authenticated user's roles in JWT?如何在 JWT 中包含经过身份验证的用户角色?
【发布时间】:2020-09-21 02:38:27
【问题描述】:

我已将 CAS 配置为 OAuth2 服务器。登录成功后返回JWT,但是JWT中的roles字段总是为空的;

{
"sub": "dg",
...
"roles": [],
"nonce": "",
"client_id": "first-client",
"credentialType": "UsernamePasswordCredential",
...

}

当我登录时,如何在 JWT 中获取和放置经过身份验证的用户角色?

这是我的示例服务注册表;

{
  "@class" : "org.apereo.cas.support.oauth.services.OAuthRegisteredService",
  "serviceId" : "http://localhost:8085/.*",
  "name" : "CAS Spring Secured App",
  "description": "This is a Spring App that usses the CAS Server for its authentication",
  "id" : 1,
  "evaluationOrder" : 1,
  "bypassApprovalPrompt": true,
  "jwtAccessToken": true,
  "clientId": "first-client",
  "clientSecret": "noonewilleverguess",
  "supportedGrantTypes": [ "java.util.HashSet", [ "authorization_code" ] ],
  "supportedResponseTypes": [ "java.util.HashSet", [ "code" ] ]
}

感谢您的帮助。

【问题讨论】:

    标签: cas apereo


    【解决方案1】:

    我找到了解决方案。来自 CAS 博客 (https://apereo.github.io/2017/02/22/cas51-dbauthn-tutorial/),

    今天,CAS 无法直接检索属性作为身份验证的一部分,因此我们需要设置一个单独的属性存储库实例,一旦用户完全通过身份验证,CAS 将联系该实例。

    所以,我们需要使用属性存储库(它有很多类型,如 ldap、jdbc、stub ...https://apereo.github.io/cas/development/configuration/Configuration-Properties.html#stub

    我已经为属性库配置了 jdbc。 (postgresql 作为数据库)

    首先需要在build.gradle中添加两个依赖

    compile "org.apereo.cas:cas-server-support-jdbc:${casServerVersion}"
    compile "org.apereo.cas:cas-server-support-jdbc-drivers:${casServerVersion}"
    

    然后,创建您获取属性的数据库。例如,命名为 my_roles

    id (serial) | user_name (varchar(50)) | role_name (text[])
    ----------------------------------------------------------
        1       |       dg                | {'ROLE_READ', 'ROLE_WRITE'}
    

    然后,像这样配置属性库

    cas.authn.attribute-repository.jdbc[0].sql=SELECT * FROM my_roles WHERE {0}
    cas.authn.attribute-repository.jdbc[0].username=user_name
    
    cas.authn.attribute-repository.jdbc[0].user=postgres
    cas.authn.attribute-repository.jdbc[0].password=postgres
    cas.authn.attribute-repository.jdbc[0].url=jdbc:postgresql://localhost:5432/customer
    cas.authn.attribute-repository.jdbc[0].driverClass=org.postgresql.Driver
    cas.authn.attribute-repository.jdbc[0].dialect=org.hibernate.dialect.PostgreSQL95Dialect
    

    最后,不要忘记将发布策略添加到您的服务注册表中。

    {
      ...
      "attributeReleasePolicy" : {
        "@class" : "org.apereo.cas.services.ReturnAllowedAttributeReleasePolicy",
        "allowedAttributes" : [ "java.util.ArrayList", [ "role_name" ] ]
      }
    }
    

    所以,结果如下;

    {
      "sub": "dg",
       ...
      "role_name":
      [
        "ROLE_WRITE",
        "ROLE_READ"
      ],
      "aud": "http://localhost:8085/login/oauth2/code/login-client",
      "grant_type": "AUTHORIZATION_CODE",
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多