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

Apereo CAS 6.0.x如何将登录表单字段传递至解析属性及Principal对象

Apereo CAS 6.0.x 相关问题解答

问题1:如何将登录表单字段传递至解析后的属性?

要把自定义登录表单字段的值放到解析后的Principal属性里,你可以按这几步来操作:

  • 第一步:修改登录页面模板,添加自定义字段
    找到CAS的登录页面模板文件(一般在src/main/resources/templates/casLoginView.html),插入你需要的表单字段。比如添加一个叫employeeId的输入框:

    <div class="form-group mb-3">
        <label for="employeeId" class="form-label">员工ID</label>
        <input type="text" class="form-control" id="employeeId" name="employeeId" required>
    </div>
    
  • 第二步:配置CAS捕获并映射这个字段
    打开CAS的核心配置文件(application.properties或者application.yml),添加以下配置,让CAS识别这个字段并将其映射为属性:

    # 开启属性存储库
    cas.authn.attribute-repository.core.enabled=true
    # 映射表单字段到属性名(前后名称可一致,也可自定义)
    cas.authn.attribute-repository.core.attributes.employeeId=employeeId
    
  • 第三步:确保属性被包含到Principal中
    最后,配置CAS将这个属性加入到返回的Principal对象里:

    cas.authn.principal.attributes-to-include=employeeId
    

    这样用户登录后,employeeId的值就会出现在Principal的属性集合中,后续可以被客户端应用获取到。


问题2:添加非认证用的语言选择器字段并传递至客户端Principal

这个需求的关键是让CAS捕获语言选择值,但不参与认证流程,然后把它传递给客户端应用。具体步骤如下:

  • 第一步:在登录页面添加语言选择器
    同样修改登录页面模板,添加一个下拉选择框,示例代码:

    <div class="form-group mb-3">
        <label for="userLang" class="form-label">选择语言</label>
        <select class="form-control" id="userLang" name="userLang">
            <option value="zh-CN" selected>简体中文</option>
            <option value="en-US">English</option>
            <option value="ja-JP">日本語</option>
        </select>
    </div>
    
  • 第二步:告诉CAS忽略该字段的认证校验
    因为这个字段不需要参与用户认证,所以要配置CAS把它排除在认证参数之外,避免报错:

    cas.authn.accept.non-credential-parameters=userLang
    
  • 第三步:将字段值映射为Principal属性
    接下来配置CAS把userLang的值转化为Principal属性,并且允许传递给客户端:

    # 映射字段到属性
    cas.authn.attribute-repository.core.attributes.userLang=userLang
    # 指定要包含到Principal中的属性
    cas.authn.principal.attributes-to-include=userLang
    
  • 第四步:配置客户端服务允许接收该属性
    最后,在你的客户端服务注册配置(比如JSON格式的服务定义文件)中,设置属性释放策略,允许把userLang传递给客户端:

    {
      "@class": "org.apereo.cas.services.RegexRegisteredService",
      "serviceId": "^https://your-client-domain.com/.*",
      "name": "Your Client Application",
      "id": 10000001,
      "attributeReleasePolicy": {
        "@class": "org.apereo.cas.services.ReturnAllowedAttributeReleasePolicy",
        "allowedAttributes": ["userLang"]
      }
    }
    

这样操作后,用户登录时选择的语言值就会被传递到客户端应用的Principal对象里啦。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 22:02:38