我有基于领域的角色和基于客户端的角色。在我的密钥斗篷领域。 例如,真实角色 Teenager 和客户端角色 Student 。
现在,我想根据客户角色学生在我的spring应用程序中允许某些路由。
keycloak.security-constraints[1].authRoles[0]=Teenager
keycloak.security-constraints[1].securityCollections[0].patterns[0]=/teen
我知道,使用此配置,青少年路由仅适用于“少年”角色
现在要路线/ teen /学生
keycloak.security-constraints[1].authRoles[0]=Student
keycloak.security-constraints[1].securityCollections[0].patterns[0]=/teen/student
我可以通过上述方式进行操作,还是需要在.authRoles [0]之后链接其他密钥?
我已将此keycloak.use-resource-role-mappings=true
添加到我的属性文件中。
答案 0 :(得分:1)
首先,您需要将约束添加到不同的数组位置:
keycloak.security-constraints[0].authRoles[0]=Teenager
keycloak.security-constraints[0].securityCollections[0].patterns[0]=/teen
keycloak.security-constraints[1].authRoles[0]=Student
keycloak.security-constraints[1].securityCollections[0].patterns[0]=/teen/student
但是这仍然不起作用,因为您不能在属性文件中混合使用realm-roles和client.roles的约束。根据{{3}}:
如果设置为true,则适配器将在令牌内部查找应用程序 用户的级别角色映射。如果为假,它将查看领域 用户角色映射的级别。这是可选的。默认值为 错误。
因此它寻找客户端级别或领域级别。您将必须通过 代码进行操作。
您将客户端角色约束添加到属性文件中,例如:
keycloak.security-constraints[0].authRoles[0]=Student
keycloak.security-constraints[0].securityCollections[0].patterns[0]=/teen/student
keycloak.use-resource-role-mappings=true
,然后在代码中处理领域角色:
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests()
.antMatchers("/teen")
.hasRole("Teenager")
.anyRequest()
.authenticated();
...
}
您可能想看一下keycloak组的Keycloak documentation。