You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CAS 5.2.3对接LDAP获取用户所属组配置及Java侧取值方法咨询

CAS 5.2.3 LDAP用户组查询配置及Java获取方案

一、修正LDAP属性仓库配置

你目前的参考配置存在两处错误,需调整后才能正常拉取用户组:

  • 原userFilter配置错误:member属性存储的是用户完整DN,不需要额外拼接cn前缀,也不需要多余的&符号
  • 缺少baseDn配置:未指定搜索根节点为ou=groups,且不需要dnFormat配置,该配置适用于按DN直接查询单条记录的场景,不适用组搜索

完整生效配置

cas.authn.attributeRepository.ldap[2].ldapUrl=ldap://localhost:9080
cas.authn.attributeRepository.ldap[2].useSsl=false
cas.authn.attributeRepository.ldap[2].useStartTls=false
cas.authn.attributeRepository.ldap[2].connectTimeout=5000
cas.authn.attributeRepository.ldap[2].bindDn=cn=Manager,dc=domain,dc=ru
cas.authn.attributeRepository.ldap[2].bindCredential=Manager1
# 指定组搜索根节点
cas.authn.attributeRepository.ldap[2].baseDn=ou=groups,dc=domain,dc=ru
# 匹配member属性等于当前认证用户的完整DN
cas.authn.attributeRepository.ldap[2].userFilter=(member={user})
cas.authn.attributeRepository.ldap[2].subtreeSearch=true
# 将LDAP组的cn属性映射为CAS内置属性groups,多值自动封装为字符串列表
cas.authn.attributeRepository.ldap[2].attributes.cn=groups

配置生效后,CAS在用户认证成功后会自动执行组查询,将用户所属所有组的组名以List<String>的格式存入groups属性中。

二、Java类中获取组列表的方法

如果是在CAS服务端的自定义扩展逻辑(如认证后处理器、自定义验证逻辑)中获取,直接通过Principal对象取值即可:

import org.apereo.cas.authentication.principal.Principal;
import java.util.List;

// 示例自定义逻辑类
public class CustomUserGroupHandler {
    public void updateUserGroupInfo(Principal principal) {
        // 直接获取groups属性,已为字符串列表格式
        List<String> userGroups = (List<String>) principal.getAttributes().get("groups");
        // 执行你的自定义更新逻辑
        for (String group : userGroups) {
            // 业务处理逻辑
        }
    }
}

如果是接入CAS的业务系统Java客户端,在CAS票据验证成功后,从返回的属性映射集中直接取groups字段即可,返回格式同样为字符串列表。

内容的提问来源于stack exchange,提问作者Ivan Green

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 17:21:00