Keycloak:JWT 令牌中列出的重复角色名称

问题描述 投票:0回答:1

尝试访问授权服务器并获取JWT token。当我使用

jq
解码令牌时,我注意到“角色”部分下的角色是重复的。如何删除重复的角色?

{
    "aud": [
        "security-admin-console",
        "Test-management"
    ],
    "resource_access": {
        "realm-management": {
            "roles": [
                "view-Test",
                "Test-admin",
                "query-Test",
                "manage-Test"
            ]
        },
        "Test": {
            "roles": [
                "Test-user"
            ]
        },
        "account": {
            "roles": [
                "test-role"
            ]
        },
        "Data": {
            "roles": [
                "data-user"
            ]
        }
    },
    "scope": "openid profile email",
    "sid": "",
    "email_verified": false,
    "org": "Test",
    "roles": [
        "view-Test",
        "Test-admin",
        "query-Test",
        "manage-Test",
        "Test-user",
        "test-role",
        "data-user",
        "view-Test",
        "Test-admin",
        "query-Test",
        "manage-Test",
        "Test-user",
        "test-role",
        "data-user",
        "view-Test",
        "Test-admin",
        "query-Test",
        "manage-Test",
        "Test-user",
        "test-role",
        "data-user"
    ],
    "groups": [
        "Administrators",
        "Users"
    ],
    "preferred_username": "TestUser"
}

Expected:

{
    "aud": [
        "security-admin-console",
        "Test-management"
    ],
    "resource_access": {
        "realm-management": {
            "roles": [
                "view-Test",
                "Test-admin",
                "query-Test",
                "manage-Test"
            ]
        },
        "Test": {
            "roles": [
                "Test-user"
            ]
        },
        "account": {
            "roles": [
                "test-role"
            ]
        },
        "Data": {
            "roles": [
                "data-user"
            ]
        }
    },
    "scope": "openid profile email",
    "sid": "",
    "email_verified": false,
    "org": "Test",
    "roles": [
        "view-Test",
        "Test-admin",
        "query-Test",
        "manage-Test",
        "Test-user",
        "test-role",
        "data-user"
    ],
    "groups": [
        "Administrators",
        "Users"
    ],
    "preferred_username": "TestUser"
}
jwt keycloak
1个回答
0
投票

测试智威汤逊

token.jwt

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsic2VjdXJpdHktYWRtaW4tY29uc29sZSIsIlRlc3QtbWFuYWdlbWVudCJdLCJyZXNvdXJjZV9hY2Nlc3MiOnsicmVhbG0tbWFuYWdlbWVudCI6eyJyb2xlcyI6WyJ2aWV3LVRlc3QiLCJUZXN0LWFkbWluIiwicXVlcnktVGVzdCIsIm1hbmFnZS1UZXN0Il19LCJUZXN0Ijp7InJvbGVzIjpbIlRlc3QtdXNlciJdfSwiYWNjb3VudCI6eyJyb2xlcyI6WyJ0ZXN0LXJvbGUiXX0sIkRhdGEiOnsicm9sZXMiOlsiZGF0YS11c2VyIl19fSwic2NvcGUiOiJvcGVuaWQgcHJvZmlsZSBlbWFpbCIsInNpZCI6IiIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwib3JnIjoiVGVzdCIsInJvbGVzIjpbInZpZXctVGVzdCIsIlRlc3QtYWRtaW4iLCJxdWVyeS1UZXN0IiwibWFuYWdlLVRlc3QiLCJUZXN0LXVzZXIiLCJ0ZXN0LXJvbGUiLCJkYXRhLXVzZXIiLCJ2aWV3LVRlc3QiLCJUZXN0LWFkbWluIiwicXVlcnktVGVzdCIsIm1hbmFnZS1UZXN0IiwiVGVzdC11c2VyIiwidGVzdC1yb2xlIiwiZGF0YS11c2VyIiwidmlldy1UZXN0IiwiVGVzdC1hZG1pbiIsInF1ZXJ5LVRlc3QiLCJtYW5hZ2UtVGVzdCIsIlRlc3QtdXNlciIsInRlc3Qtcm9sZSIsImRhdGEtdXNlciJdLCJncm91cHMiOlsiQWRtaW5pc3RyYXRvcnMiLCJVc2VycyJdLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJUZXN0VXNlciIsImlhdCI6MTczMzg1MzA0Mn0.yTHHN3Ypm2bQXkgvVKL_z0GiouyeICnus-UmszFgn2c

从您的

jq-data.json
赞扬中保存
jq

cat token.jwt | awk -F. '{print $2}' | sed 's/-/+/g; s/_/\//g; s/=$//g' | base64 -d | jq > jq-data.json

jq-data.json

{
    "aud": [
        "security-admin-console",
        "Test-management"
    ],
    "resource_access": {
        "realm-management": {
            "roles": [
                "view-Test",
                "Test-admin",
                "query-Test",
                "manage-Test"
            ]
        },
        "Test": {
            "roles": [
                "Test-user"
            ]
        },
        "account": {
            "roles": [
                "test-role"
            ]
        },
        "Data": {
            "roles": [
                "data-user"
            ]
        }
    },
    "scope": "openid profile email",
    "sid": "",
    "email_verified": false,
    "org": "Test",
    "roles": [
        "view-Test",
        "Test-admin",
        "query-Test",
        "manage-Test",
        "Test-user",
        "test-role",
        "data-user",
        "view-Test",
        "Test-admin",
        "query-Test",
        "manage-Test",
        "Test-user",
        "test-role",
        "data-user",
        "view-Test",
        "Test-admin",
        "query-Test",
        "manage-Test",
        "Test-user",
        "test-role",
        "data-user"
    ],
    "groups": [
        "Administrators",
        "Users"
    ],
    "preferred_username": "TestUser"
}

运行此 jq 命令以删除重复角色

jq '.roles |= (unique | sort)' jq-data.json > updated-jq-data.json

updated-jq-data.json

{
  "aud": [
    "security-admin-console",
    "Test-management"
  ],
  "resource_access": {
    "realm-management": {
      "roles": [
        "view-Test",
        "Test-admin",
        "query-Test",
        "manage-Test"
      ]
    },
    "Test": {
      "roles": [
        "Test-user"
      ]
    },
    "account": {
      "roles": [
        "test-role"
      ]
    },
    "Data": {
      "roles": [
        "data-user"
      ]
    }
  },
  "scope": "openid profile email",
  "sid": "",
  "email_verified": false,
  "org": "Test",
  "roles": [
    "Test-admin",
    "Test-user",
    "data-user",
    "manage-Test",
    "query-Test",
    "test-role",
    "view-Test"
  ],
  "groups": [
    "Administrators",
    "Users"
  ],
  "preferred_username": "TestUser",
  "iat": 1733853042
}
© www.soinside.com 2019 - 2024. All rights reserved.