后端认证鉴权单点登录【免费下载链接】casApereo CAS - Identity Single Sign On for all earthlings and beyond.项目地址https://gitcode.com/gh_mirrors/ca/cas点击查看免费下载属性仓库过滤是 Apereo CAS 中面向“服务Registered Service”粒度的属性释放控制手段通过principalAttributesRepository.attributeRepositoryIds你可以精确指定在属性释放阶段只联系哪些 Person Directory 属性源如某个 JSON 仓库而忽略其它已解析属性或将其合并进来。读完本文你将掌握attributeRepositoryIds、ignoreResolvedAttributes、mergingStrategy三个核心属性的语义与组合方式能够在服务注册文件中写出“延迟到释放时才查询指定属性源”的实战配置并从源码层面理解其底层过滤与缓存机制。背景属性解析与属性释放的分工在 CAS 中属性解析Attribute Resolution与属性释放Attribute Release是两个不同的阶段。属性解析由 Person Directory 系列组件驱动它从 LDAP、JDBC、REST、JSON、Groovy 等若干属性源中检索、缓存、合并属性最终构造出携带属性集合的已认证Principal。属性释放则发生在服务票据校验service ticket validation时由该服务对应的attributeReleasePolicy决定向调用方开放哪些属性。默认情况下已解析的属性会被缓存到 SSO 会话结束为止详见 Attribute Release Caching。这意味着认证阶段从属性源拿到一次属性后即使底层数据发生变化SSO 会话存续期间再次校验服务票据时释放的仍是旧值。属性仓库过滤机制正是在这一背景下提供的一种“按服务定制”的能力不是让所有属性源都在认证阶段被查询而是把某些属性源推迟到释放阶段、且只针对指定服务、按指定策略查询。Person Directory 的属性解析引擎也可以配置为只查询一部分属性源从而把属性检索任务“延迟defer”到释放阶段。三个核心属性过滤与合并的控制开关所有 Principal 属性仓库实现都共享一组配置属性它们定义在抽象基类 AbstractPrincipalAttributesRepository 中并在服务注册 JSON 的principalAttributesRepository块内声明属性名说明默认值源码attributeRepositoryIds释放阶段要联系的属性仓库标识符集合类型为SetString空集合表示不额外查询仓库ignoreResolvedAttributes是否忽略认证阶段principal resolution已经解析到Principal上的属性falsemergingStrategy合并已解析属性与仓库检索结果时采用的策略取值MULTIVALUED、ADD、REPLACE、NONEMULTIVALUED从源码可以确认这些默认值AbstractPrincipalAttributesRepository 中mergingStrategy初始化为MULTIVALUEDattributeRepositoryIds初始化为空LinkedHashSetignoreResolvedAttributes初始化为false。getPrincipalAttributes()方法集中体现了ignoreResolvedAttributes的影响源码 L107-L113为true时直接返回空HashMap即“全部丢弃此前解析出的属性”为false时才把Principal上已有的属性转换后参与后续合并。areAttributeRepositoryIdsDefined()则决定是否真正去联系属性源源码 L89-L92只有当attributeRepositoryIds非空时CAS 才会调用retrievePersonAttributesFromAttributeRepository()去查询仓库否则只返回Principal已有属性。场景一缓存模式下仅从指定仓库重新拉取属性下面这段配置来自 Attribute Repository Filtering假设已定义了一个标识符为MyJsonRepository的 JSON 属性仓库源。本配置会丢弃所有此前已解析的属性在释放阶段重新联系MyJsonRepository获取属性并缓存 30 分钟{ class : org.apereo.cas.services.CasRegisteredService, serviceId : ^(https|imaps)://.*, name : HTTPS and IMAPS, id : 1, attributeReleasePolicy : { class : org.apereo.cas.services.ReturnAllAttributeReleasePolicy, principalAttributesRepository : { class : org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository, timeUnit : MINUTES, expiration : 30, ignoreResolvedAttributes: true, attributeRepositoryIds: [java.util.HashSet, [ MyJsonRepository ]], mergingStrategy : MULTIVALUED } } }逐项解读class为CachingPrincipalAttributesRepository启用缓存型仓库。其执行逻辑在 CachingPrincipalAttributesRepository.getAttributes() 中先查缓存若命中且非空则直接返回缓存属性不再联系仓库未命中时才继续执行后续检索。timeUnit与expiration缓存时长。此处为 30 分钟timeUnit取值遵循java.util.concurrent.TimeUnit语义如SECONDS、MINUTES、HOURS。超过过期时间后下次释放会重新咨询底层属性源。ignoreResolvedAttributes: true忽略认证阶段已解析的属性释放时完全以仓库结果为准。attributeRepositoryIds: [java.util.HashSet, [ MyJsonRepository ]]这是 CAS 服务注册 JSON 中Set的标准序列化写法——先给出集合实现类java.util.HashSet再给出元素数组。它声明“释放时只联系MyJsonRepository这一个属性源”。mergingStrategy: MULTIVALUED合并策略。由于ignoreResolvedAttributes已为true主属性为空此策略主要影响仓库内多值属性的表现形式同名属性合并为多值列表。缓存策略的生效时机需要注意CachingPrincipalAttributesRepository的缓存策略只在属性释放服务票据校验时被咨询。如果自定义 webflow 等组件希望基于Principal获得刷新后的属性必须自行查询底层目录源而不能依赖Principal上携带的属性参见 Attribute Release Caching 中的说明。场景二关闭缓存合并已解析属性与指定仓库结果下面这段配置同样来自关联文档使用DefaultPrincipalAttributesRepository非缓存型——服务层面关闭了缓存{ class : org.apereo.cas.services.CasRegisteredService, serviceId : ^(https|imaps)://.*, name : HTTPS and IMAPS, id : 1, attributeReleasePolicy : { class : org.apereo.cas.services.ReturnAllAttributeReleasePolicy, principalAttributesRepository : { class : org.apereo.cas.authentication.principal.DefaultPrincipalAttributesRepository, ignoreResolvedAttributes: false, attributeRepositoryIds: [java.util.HashSet, [ MyJsonRepository ]], mergingStrategy : MULTIVALUED } } }与场景一的关键差异class换为DefaultPrincipalAttributesRepository即 DefaultPrincipalAttributesRepository它“原样返回收到的属性”不做任何缓存。其getAttributes()逻辑源码 L31-L47与缓存版几乎一致先取主属性若定义了attributeRepositoryIds则检索仓库并按合并策略合并。ignoreResolvedAttributes: false保留认证阶段已解析到Principal上的属性与MyJsonRepository的检索结果合并。期望的前置条件文档明确指出这里的预期是MyJsonRepository在认证阶段的 principal resolution 过程中被排除仅在该服务的释放阶段才被联系。也就是说属性仓库过滤常与“认证阶段按需排除属性源”配合使用实现真正的延迟检索deferred attribute retrieval。Person Directory 的解析引擎可配置为只查询选中的属性源子集从而把检索任务推迟到释放阶段参见 Attribute Resolution 中的说明。合并策略详解合并发生在“Principal 已解析属性”与“仓库检索结果”之间。四种策略的行为对比如下源自 Attribute Resolution 与 Attribute Release Caching 中的表格策略行为MULTIVALUED同名属性合并为多值列表。例如主属性phone123-456-7890与源属性phone[111-222-3333, 000-999-8888]合并为phone[123-456-7890, 111-222-3333, 000-999-8888]ADD仅把源中主属性不存在的属性补充进来已存在的属性值保持不变REPLACE源属性总是覆盖主属性中同名属性的值NONE不合并只使用认证期间检索到的属性例如以主属性{emaileric.dalquistexample.com, phone123-456-7890}、源属性{phone[111-222-3333, 000-999-8888], office3233}为例MULTIVALUED结果为{emaileric.dalquistexample.com, phone[123-456-7890, 111-222-3333, 000-999-8888], office3233}ADD结果为{emaileric.dalquistexample.com, phone123-456-7890, office3233}REPLACE结果为{emaileric.dalquistexample.com, phone[111-222-3333, 000-999-8888], office3233}从源码看合并策略由CoreAuthenticationUtils.getAttributeMerger(mergeStrategy)选择实现并统一作用于“主属性 仓库属性”两个集合参见 AbstractPrincipalAttributesRepository 中合并调用的位置。当定义了多个属性仓库源时各源按配置顺序执行合并策略同样决定冲突解决结果。底层实现标识符如何被用来过滤属性源attributeRepositoryIds的过滤能力最终由两个组件协同完成PrincipalAttributeRepositoryFetcher位于 core/cas-server-core-authentication-api/src/main/java/org/apereo/cas/authentication/attribute/PrincipalAttributeRepositoryFetcher.java负责构造查询并调用 Person Directory 的PersonAttributeDao。其retrieve()方法源码 L46-L74会把当前principal的 ID、属性、username以及可选的service一并放入查询条件当返回多条 person 记录时CAS 只取第一条并给出告警日志。PrincipalAttributeRepositoryFilter位于 core/cas-server-core-authentication-api/src/main/java/org/apereo/cas/authentication/attribute/PrincipalAttributeRepositoryFilter.java实现PersonAttributeDaoFilter在choosePersonAttributeDao()中源码 L24-L42逐个比对仓库的id与attributeRepositoryIds集合。比对是大小写不敏感的同时支持通配符若集合中不含任何标识符则不会选择任何仓库若包含PersonAttributeDao.WILDCARD则选择全部仓库PrincipalAttributeRepositoryFetcher.fromAllAttributeRepositories()正是通过添加该通配符来禁用过滤。在 AbstractPrincipalAttributesRepository.retrievePersonAttributesFromAttributeRepository() 中CAS 通过 Spring 上下文获取PrincipalResolver.BEAN_NAME_ATTRIBUTE_REPOSITORY对应的PersonAttributeDaoBean并以attributeRepositoryIds作为activeAttributeRepositoryIdentifiers构建 fetcher——这便构成了“只查指定仓库”的完整调用链。实战建议与注意事项标识符必须与属性源定义一致attributeRepositoryIds里的字符串需要与 Person Directory 中属性仓库源定义的标识符完全对应比对时大小写不敏感但建议保持书写一致避免排查困难。JSON 仓库源的定义与命名方式可参考 Attribute Resolution 中关于各属性源的说明。延迟检索的前提要让“释放阶段才查仓库”真正生效需要同时保证该属性源在认证阶段不被查询。请结合 Person Directory 的源选择/排除配置使用否则同一源在认证阶段已被解析过滤机制的意义会大打折扣。缓存过期才会刷新使用CachingPrincipalAttributesRepository时只要缓存未过期释放阶段返回的是缓存值而非仓库实时值若业务要求每次校验都拿最新数据应改用DefaultPrincipalAttributesRepository或缩短过期时间。多源场景注意顺序配置多个属性源时它们按定义顺序执行mergingStrategy决定同名属性冲突的最终形态。排除属性的表达ignoreResolvedAttributes控制的是“认证阶段解析到 Principal 上的属性”是否参与释放合并与attributeRepositoryIds控制的“释放阶段联系哪些源”是两个正交维度可自由组合出四种典型行为。总结属性仓库过滤Attribute Repository Filtering是 CAS 在“服务级属性释放”上的精细控制手段核心就是principalAttributesRepository中的三个开关attributeRepositoryIds决定释放时联系哪些属性源ignoreResolvedAttributes决定是否丢弃已解析属性mergingStrategy决定合并冲突的解决方式。配合缓存型与非缓存型两种仓库实现你可以在同一个 CAS 部署中为不同服务定制完全不同的属性获取策略——例如某个服务总是实时从 JSON 源拉取最新属性而其它服务沿用 SSO 会话期间的缓存结果。源码中PrincipalAttributeRepositoryFetcher与PrincipalAttributeRepositoryFilter的协作清晰展示了标识符匹配、通配符支持与大小写不敏感比对等实现细节为深度定制提供了可靠依据。关联文档Attribute Repository Filtering相关文档Attribute Release Caching、Attribute Resolution核心实现AbstractPrincipalAttributesRepository、CachingPrincipalAttributesRepository、DefaultPrincipalAttributesRepository过滤链路PrincipalAttributeRepositoryFetcher、PrincipalAttributeRepositoryFilter赞分享后端认证鉴权单点登录【免费下载链接】casApereo CAS - Identity Single Sign On for all earthlings and beyond.项目地址https://gitcode.com/gh_mirrors/ca/cas点击查看免费下载相关推荐CAS 属性释放策略详解ReturnStatic 静态属性释放Attribute Release Policy - Return StaticCAS 属性释放策略详解ReturnStatic 静态属性释放Attribute Release Policy Return Static 本指南围绕 A后端认证鉴权单点登录Apereo CAS 属性释放策略 ReturnMapped服务级属性重命名与动态映射实战指南Apereo CAS 属性释放策略 ReturnMapped服务级属性重命名与动态映射实战指南 本文面向需要为不同应用定制属性名称的 CAS 集成场景详细讲后端认证鉴权单点登录Apereo CAS 属性释放用户同意Attribute Release Consent机制详解激活策略、属性选择与存储实现Apereo CAS 属性释放用户同意Attribute Release Consent机制详解激活策略、属性选择与存储实现 CAS 属性释放同意Att后端认证鉴权单点登录创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考