有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是否可以通过KeyClope中的RESTAPI检查用户是否有活动/有效的会话?

是否可以通过RESTAPI检查用户是否有活动/有效的会话

我正在使用JavaREST管理客户端。我看到了由返回的UserSessionRepresentation

List<UserSessionRepresentation> usr = Keycloak.realm("realmId").users().get("userId").getUserSessions();

但是UserSessionRepresentation没有我想要的信息。 有可能吗


共 (3) 个答案

  1. # 1 楼答案

    此处引用的特定API端点(interfaceimplementation返回该用户的活动/有效会话。您可以匹配会话id(或其他各种内容)。如果会话无效,则会将其标记为删除,而不会返回该端点。看来这就是你想要的

    但也要意识到,如果您正在寻找其他会话(如脱机会话)。如果需要,请检查链接源以获取更多信息或其他端点

  2. # 2 楼答案

    您可以使用以下地址检查会话:

    http://keycloakAddressAndPort/auth/realms/develop/protocol/openid-connect/userinfo
    

    如果会话不正常,您将看到此响应(http状态代码401):

    {
        "error": "invalid_request",
        "error_description": "User session not found or doesn't have client attached on it"
    }
    

    如果没有问题,您会看到类似的情况(http状态代码200):

    {
        "sub": "c0c25095-63e7-471d-a39e-f3b157c91fd7",
        "email_verified": true,
        "name": "Amir Azizkhani",
        "preferred_username": "a.azizkhani@...",
        "given_name": "Amir",
        "family_name": "Azizkhani",
        "email": "a.azizkhani@...."
    }
    

    完整的邮递员json:

    {
        "info": {
            "_postman_id": "77ce65c3-948a-4b3d-a97b-b11cd00c593b",
            "name": "Spring Keycloak",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
        },
        "item": [
        
            {
                "name": "openid-connect/userinfo",
                "request": {
                    "auth": {
                        "type": "bearer",
                        "bearer": [
                            {
                                "key": "token",
                                "value": "eyJhbGciOiJSUzI1NiIsInR5cC...",
                                "type": "string"
                            }
                        ]
                    },
                    "method": "GET",
                    "header": [
                        {
                            "key": "accept",
                            "value": "application/json"
                        }
                    ],
                    "url": {
                        "raw": "http://192.168.131.33:8080/auth/realms/develop/protocol/openid-connect/userinfo",
                        "protocol": "http",
                        "host": [
                            "192",
                            "168",
                            "131",
                            "33"
                        ],
                        "port": "8080",
                        "path": [
                            "auth",
                            "realms",
                            "develop",
                            "protocol",
                            "openid-connect",
                            "userinfo"
                        ]
                    },
                    "description": "Dont forget to set Bearer value obtains from token request"
                },
                "response": []
            }
        ]
    }
    
  3. # 3 楼答案

    我想您希望that one获得活动客户端会话的列表:

    GET /admin/realms/{realm}/client-session-stats
    

    描述

    The key is the client id, the (returned) value is the number of sessions that currently are active with that client. Only clients that actually have a session associated with them will be in this map.