CAS5.3X 之配置默认Service实现

前言

今儿老A问我 :“怎么在没有service的情况下,给CAS配置默认的service,跳转到固定的url啊?”
我:“额,文档不是写了嘛?在springboot的配置文件application.properties里有个配置属性,就下边这个”

官网文档链接:
https://apereo.github.io/cas/5.0.x/installation/Configuration-Properties.html

# Defines a default URL to which CAS may redirect if there is no service
# provided in the authentication request.
cas.view.defaultRedirectUrl=https://www.github.com

过了一会儿…
老A:“哎,不对呀,你过来看看,这怎么还提示我CAS无法使用
呢,页面也没跳转呀”
错误提示:

Error: Exception thrown executing org.apereo.cas.web.flow.login.GenericSuccessViewAction@50118e2e in state ‘viewGenericLoginSuccess’ of flow ‘login’ – action execution attributes were ‘map[[empty]]’

截图:在这里插入图片描述
我:“哟,中奖了!来来来,分析分析…”

CAS配置默认service

经过前面的对话,咱都了解了老A的问题,也去看了CAS的文档,知道了有个配置是用来定义一个默认URL,如果没有服务的情况下,CAS可以重定向到该URL。
当然,还要注意一句话:“provided in the authentication request. ”,意思是说你这个URL也不能乱写,必须要通过serviceid的正则验证才行
通过入门文档,咱都知道,在resources/services下新建的主题json文件,基本都长下面这样:

静态Service配置

{
   
   
  "@class": "org.apereo.cas.services.RegexRegisteredService",
  "serviceId": "^(http)://localhost.*",
  "name": "本地服务",
  "id": 100001,
  "description": "这是一个本地允许的服务,通过localhost访问都允许通过",
  "evaluationOrder": 1
}

注意:
json文件名字规则为 n a m e − {name}- name{id}.json, id必须为json文件内容id一致
json文件解释:

  • @class:必须为org.apereo.cas.services.RegisteredService的实现类,对其他属性进行一个json反射对象,常用的有RegexRegisteredService,匹配策略为id的正则表达式
  • serviceId:唯一的服务id
  • name: 服务名称,会显示在默认登录页
  • id:全局唯一标志
  • description:服务描述,会显示在默认登录页
  • evaluationOrder:确定已注册服务的相对评估顺序。当两个服务URL表达式覆盖相同的服务时,此标志尤其重要;评估顺序决定首先评估哪个注册,并作为内部排序因素。 (越小越优先)
    除了以上说的还有很多配置策略以及节点,具体还要看官方文档,配置不同的RegisteredService也会有稍微不一样,当然咱也可以写自己的,这是后话,没啥不行的。
    按老A的描述,他这里也做了修改,长下面这样,确实把啥url都给放过了,没话说。
{
   
   
  "@class" : "org.apereo.cas.services.RegexRegisteredService",
  "serviceId" : "^(https|imaps|http)://.*",
  "name" : "Landing One",
  "id" : 1000,
  "description" : "landingone 项目访问过来,跳转到demo主题",
  "evaluationOrder" : 10,
  "theme": "landingone"
}

Spring Web Flow

咱们都知道CAS目前是集成了Spring Web Flow,在CAS中Spring Web Flow的配置文件最初始的就有login-webflow.xml面主要配置了登录的流程。这个如果用图来表示的话那应该是一个状态图,一些节点会有一些判断然后会有不同的分支。
不知道大伙有没有看过历史版本哈,那玩意真的是,节点挺多的,一眼望不到底,还得翻两页。
哎,咱们用的这个CAS 5.3.1 版本,贼简单,就长下面这样:

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/webflow"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
                          http://www.springframework.org/schema/webflow/spring-webflow.xsd">

    <action-state id="initializeLoginForm">
        <evaluate expression="initializeLoginAction" />
        <transition on="success" to="viewLoginForm"/>
    </action-state>

    <view-state id="viewLoginForm" view="casLoginView" model="credential">
        <binder>
            <binding property="username" required="true"/>
            <binding property="password" required=
### CAS 5.3 客户端配置教程 #### 配置概述 Apereo CAS 是一种广泛使用的单点登录解决方案,支持多种客户端集成方式。对于 CAS 5.3 的客户端配置,主要涉及以下几个方面:项目初始化、依赖管理以及服务注册。 --- #### 1. 初始化 CAS 客户端项目 在开始配置之前,需要创建一个新的 Maven 或 Gradle 项目作为客户端应用的基础环境。以下是基于 Maven 的示例: ```xml <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- CAS Client Core Dependency --> <dependency> <groupId>org.jasig.cas.client</groupId> <artifactId>cas-client-core</artifactId> <version>3.6.1</version> </dependency> </dependencies> ``` 上述代码片段展示了如何引入 `cas-client-core` 依赖[^3],这是实现 CAS 协议的核心库。 --- #### 2. 修改 Web 应用程序的配置文件 为了使客户端能够与 CAS Server 正常交互,需调整应用程序中的过滤器设置。通常情况下,在 Java EE 中可以通过 `web.xml` 文件完成此操作;而在现代框架(如 Spring Boot)中,则通过编程方式进行配置。 ##### 基于 XML 的配置方法 如果使用传统的 Servlet API,可以在 `web.xml` 中定义以下内容: ```xml <filter> <filter-name>CAS Single Sign Out Filter</filter-name> <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class> </filter> <filter-mapping> <filter-name>CAS Single Sign Out Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Authentication Filter --> <listener> <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class> </listener> ``` 该部分实现了单点登出功能并监听会话状态变化。 ##### 基于 Spring Boot 的自动配置 当采用 Spring Boot 构建时,推荐利用 Java Config 替代静态 XML 描述符的方式。下面是一个简单的例子: ```java import org.jasig.cas.client.authentication.AuthenticationFilter; import org.jasig.cas.client.session.SingleSignOutFilter; @Configuration public class CasClientConfig { @Bean public SingleSignOutFilter singleSignOutFilter() { final SingleSignOutFilter filter = new SingleSignOutFilter(); filter.setCasServerUrlPrefix("https://your-cas-server-url/cas"); return filter; } @Bean public AuthenticationFilter authenticationFilter() throws Exception { final AuthenticationFilter authFilter = new AuthenticationFilter(); authFilter.setCasServerLoginUrl("https://your-cas-server-url/cas/login"); authFilter.setServerName("http://localhost:8080"); // Your client app URL return authFilter; } } ``` 这段代码动态设置了 CAS 登录页面链接和服务名称等参数。 --- #### 3. 调整 CAS Server 的服务注册表 为了让 CAS Server 接受来自特定客户端的应用请求,必须更新其服务注册表。默认路径位于 Tomcat 的部署目录下 `\WEB-INF\classes\services` 文件夹内。 假设目标客户端允许 HTTP 和 HTTPS 请求,则编辑对应的 JSON 文件(例如 `HTTPSandIMAPS-10000001.json`),将其内容更改为如下形式: ```json { "@class": "org.apereo.cas.services.RegexRegisteredService", "serviceId": "^(https|http|imaps)://.*", "name": "HTTP and IMAP Service", "id": 10000001, "description": "Allows both secure (HTTPS) and insecure (HTTP) connections." } ``` 此外,还需确认是否存在额外的安全约束条件影响 TGC Cookie 行为。此时可在全局属性文件 `\WEB-INF\classes\application.properties` 添加以下条目以放宽限制[^4]: ```properties cas.tgc.secure=false cas.serviceRegistry.initFromJson=true ``` 这两项分别控制 Ticket Granting Cookies 是否强制加密传输以及是否启用 JSON 格式的初始加载机制。 --- #### 4. 测试连接 完成所有必要的更改之后,启动 CAS Server 及测试客户端实例,并验证两者之间的通信流程是否顺畅无误。具体步骤可能因实际场景而异,但一般包括访问保护资源触发重定向至认证入口点的过程。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值