对于我的项目,我的 Keycloak 中存在已正确设置身份提供商链接用户 ID 的用户。\n其中一些用户没有为我的项目的客户端设置角色。\n这些用户已登录(因为他们拥有有效的身份提供商链接用户 ID)。 Google 帐户)到我的应用程序,然后应用程序必须管理这样一个事实:他们不应该访问该应用程序(因为他们没有角色)。\n我想要的是告诉 keycloak 在用户没有权限的情况下不要重定向到我的应用程序\n我已经为用户名密码表单完成了此操作(使用脚本,请参阅下面的脚本代码),但我无法使用身份提供程序重定向器成功执行此操作,之后的脚本似乎未执行(重定向似乎发生在身份提供者重定向器中)。
\n\n预先感谢您的任何帮助,
\n\n编辑:适用于用户名密码表单的脚本:
\n\n/*\n * Template for JavaScript based authenticators.\n * See org.keycloak.authentication.authenticators.browser.ScriptBasedAuthenticatorFactory\n */\n\n// import enum for error lookup\nAuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");\nUserCredentialModel = Java.type("org.keycloak.models.UserCredentialModel");\nErrors = Java.type("org.keycloak.events.Errors");\nOAuth2ErrorRepresentation = Java.type("org.keycloak.representations.idm.OAuth2ErrorRepresentation");\nMediaType = Java.type("javax.ws.rs.core.MediaType");\nResponse = Java.type("javax.ws.rs.core.Response");\n\nFormMessage = Java.type(\'org.keycloak.models.utils.FormMessage\');\n/**\n * An example authenticate function.\n *\n * The following variables are available for convenience:\n * user - current user {@see org.keycloak.models.UserModel}\n * realm - current realm {@see org.keycloak.models.RealmModel}\n * session - current KeycloakSession {@see org.keycloak.models.KeycloakSession}\n * httpRequest - current HttpRequest {@see org.jboss.resteasy.spi.HttpRequest}\n * script - current script {@see org.keycloak.models.ScriptModel}\n * authenticationSession - current authentication session {@see org.keycloak.sessions.AuthenticationSessionModel}\n * LOG - current logger {@see org.jboss.logging.Logger}\n *\n * You one can extract current http request headers via:\n * httpRequest.getHttpHeaders().getHeaderString("Forwarded")\n *\n * @param context {@see org.keycloak.authentication.AuthenticationFlowContext}\n */\nfunction authenticate(context) {\n\n var username = user ? user.username : "anonymous";\n LOG.info(script.name + " trace auth for: " + username + " " + session.getContext().getClient().getClientId());\n\n var client = session.getContext().getClient();\n var rolesClient = user.getClientRoleMappings(client);\n\n if (rolesClient.isEmpty()) {\n context.forkWithErrorMessage(new FormMessage(\'label\', \'Utilisateur non autoris\xc3\xa9\'));\n return;\n }\n context.success();\n}\nRun Code Online (Sandbox Code Playgroud)\n
我的项目也遇到了同样的问题。我设法通过身份提供者上的“登录后流程”解决了这个问题。
我必须做的步骤是:
我希望这也能解决你的问题
| 归档时间: |
|
| 查看次数: |
4486 次 |
| 最近记录: |