我在 awx(ansible tower)网页中有一个 keycloak
客户端。
我只需要来自特定 keycloak
组的用户能够通过此客户端登录。
如何禁止所有其他用户(除了一个特定组)使用此 keycloak
客户端?
我在 awx(ansible tower)网页中有一个 keycloak
客户端。
我只需要来自特定 keycloak
组的用户能够通过此客户端登录。
如何禁止所有其他用户(除了一个特定组)使用此 keycloak
客户端?
nextcloud-access
或类似的角色),并在第4步中使用它。 - Karalga--2019-11-08编辑--
如评论中所述,必须将Client Protocol设置为openid-connect,并且必须将Access Type设置为confidential,才能使Authorization Enabled选项可见。
feature:authenticate
),则脚本会检查用户是否具有该角色,并在没有时显示错误页面(需要在主题中部署的新模板)。AuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");
function authenticate(context) {
var MANDATORY_ROLE = 'feature:authenticate';
var username = user ? user.username : "anonymous";
var client = session.getContext().getClient();
LOG.debug("Checking access to authentication for client '" + client.getName() + "' through mandatory role '" + MANDATORY_ROLE + "' for user '" + username + "'");
var mandatoryRole = client.getRole(MANDATORY_ROLE);
if (mandatoryRole === null) {
LOG.debug("No mandatory role '" + MANDATORY_ROLE + "' for client '" + client.getName() + "'");
return context.success();
}
if (user.hasRole(mandatoryRole)) {
LOG.info("Successful authentication for user '" + username + "' with mandatory role '" + MANDATORY_ROLE + "' for client '" + client.getName() + "'");
return context.success();
}
LOG.info("Denied authentication for user '" + username + "' without mandatory role '" + MANDATORY_ROLE + "' for client '" + client.getName() + "'");
return denyAccess(context, mandatoryRole);
}
function denyAccess(context, mandatoryRole) {
var formBuilder = context.form();
var client = session.getContext().getClient();
var description = !mandatoryRole.getAttribute('deniedMessage').isEmpty() ? mandatoryRole.getAttribute('deniedMessage') : [''];
var form = formBuilder
.setAttribute('clientUrl', client.getRootUrl())
.setAttribute('clientName', client.getName())
.setAttribute('description', description[0])
.createForm('denied-auth.ftl');
return context.failure(AuthenticationFlowError.INVALID_USER, form);
}
return context.failure(AuthenticationFlowError.CLIENT_DISABLED);
而不是使用自定义函数denyAccess
(会导致默认主题出现问题)。 - Skyr关于Allan的答案,他的方法是有效的(至少对我来说是;-)),但是我在部署方面遇到了一些困难。 这是我的做法:
-Dkeycloak.profile.feature.scripts=enabled
参数启动Keycloakfeature:authenticate
客户端角色分配给所有应受限制的客户端。不具备该角色的用户将无法访问应用程序。user.hasRole(realm.getRole("yourRoleName"))
)。我尝试了Allan的解决方案,并且在使用Keycloak 11.0.3时它运行良好,但它具有以下一些缺点。这是我的解决方案,用于认证脚本,如果用户不是至少属于给定组之一的成员,则不授予其访问权限。在这种情况下,会显示一个独特的错误消息。
AuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");
function authenticate(context) {
var allowed_groups = ['foo', 'bar'];
var username = user ? user.username : "anonymous";
var groups = user.getGroups();
var group_array = groups.toArray();
for (var i in group_array) {
var gn = group_array[i].getName();
if (allowed_groups.indexOf(gn) >= 0) {
LOG.info("Access granted for user '" + username + "' for being member of LDAP group '" + gn + "'");
return context.success();
}
}
LOG.info("Access denied for user '" + username + ". for not being member of any of the following LDAP groups: " + allowed_groups);
context.failure(AuthenticationFlowError.IDENTITY_PROVIDER_DISABLED, context.form().setError(
"User doesn't have the required LDAP group membership to view this page", null).createForm("error.ftl"));
return;
}
bin/standalone.sh|bat -Dkeycloak.profile.feature.scripts=enabled
@Allan的解决方案中,带有“身份验证”功能,我觉得很不错。