Keycloak Keycloack:注销后,仍然可以使用访问令牌

Keycloak Keycloack:注销后,仍然可以使用访问令牌,keycloak,openid,openresty,Keycloak,Openid,Openresty,我有一个nginx/openresty客户端到keycloack服务器,以便使用openid进行授权。 我使用允许访问代理背后的服务 用户可以在以下位置访问其个人资料: https:///auth/realms//account 然后通过 https:///auth/realms//protocol/openid-connect/logout 问题是,即使在注销后,用户仍然可以访问服务器后面的服务,基本上,他从KeyClope获得的令牌似乎仍然有效或是其他什么。。。。这也是其他用户观察到的一种行

我有一个nginx/openresty客户端到keycloack服务器,以便使用openid进行授权。 我使用允许访问代理背后的服务

用户可以在以下位置访问其个人资料:
https:///auth/realms//account
然后通过
https:///auth/realms//protocol/openid-connect/logout

问题是,即使在注销后,用户仍然可以访问服务器后面的服务,基本上,他从KeyClope获得的令牌似乎仍然有效或是其他什么。。。。这也是其他用户观察到的一种行为,例如参见ch271828n的评论

如何确保注销后用户在重新登录之前无法再访问?

我必须检查,但我认为我已经了解了注销行为:Lua resty openidc建立会话,并且在检测到特定url访问时终止会话(它由
opts.logout\u path控制,我们需要将其设置为服务路径中的地址,例如..../service/logout)

本质上,需要点击两个url,一个用于KeyClock注销,另一个用于openresty会话注销https:///auth/realms//protocol/openid-connect/logout
在我们访问
https:///service/logout

因此在正确设置所有内容之后,我们要做的就是注销点击
https:///service/logout
。这将破坏会话并使我们注销

我认为我们需要设置
opts.revoke\u tokens\u on\u logout
true
,还要注意,从我的实验中,出于某种原因,在注销后设置
重定向\u\u uri
可能会导致用户由于重定向而无法注销

下面是nginx.conf实现此功能所需的示例

location /myservice/ {

    access_by_lua_block {
        local opts = {
            redirect_uri_path = "/myservice/auth",
            discovery = "https://<keycloak-server>/auth/realms/<my-realm>/.well-known/openid-configuration",
            client_id = "<my-client-id>",
            client_secret = "<the-clients-secret>",
            logout_path = "/service/logout",
            revoke_tokens_on_logout = true,
            session_contents = {id_token=true} -- this is essential for safari!
        }
        -- call introspect for OAuth 2.0 Bearer Access Token validation
        local res, err = require("resty.openidc").authenticate(opts)

        if err then
            ngx.status = 403
            ngx.say(err)
            ngx.exit(ngx.HTTP_FORBIDDEN)
        end
    }

    # I disbled caching so the browser won't cache the site.
    expires           0;
    add_header        Cache-Control private;

    proxy_pass http://my-service-server.cloud:port/some/path/;
    proxy_set_header Host $http_host;

    proxy_http_version 1.1;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
location/myservice/{
通过_lua_块访问_{
本地选项={
重定向_uri_path=“/myservice/auth”,
发现=”https:///auth/realms//.well-known/openid-configuration",
客户_id=“”,
客户_secret=“”,
注销路径=“/service/logout”,
注销时撤销令牌=true,
session_contents={id_token=true}——这对于safari来说是必不可少的!
}
--OAuth 2.0承载访问令牌验证的调用内省
本地res,err=require(“resty.openidc”).authenticate(opts)
如果有错误,那么
ngx.status=403
ngx.say(呃)
ngx.exit(禁止ngx.HTTP_)
结束
}
#我取消了缓存,因此浏览器不会缓存站点。
过期0;
添加_头缓存控制私有;
代理通行证http://my-service-server.cloud:port/some/path/;
代理设置头主机$http\U主机;
proxy_http_版本1.1;
代理_重定向关闭;
代理缓冲关闭;
代理设置头升级$http\U升级;
代理设置头连接“升级”;
}

也许这篇文章只是为了澄清:我们确实让refreshToken过期,但accessToken在“访问令牌寿命”期间仍然有效。下次用户试图通过refresh Token续订访问令牌时,KeyClope返回400个错误请求,应该捕获哪些内容并作为401个未授权响应发送。