对于 4.2.1 版本来说,要使用restful接口,需要先启动restful api功能。
启动 restful api模块
在build.gradle 文件中添加如下一行:1
compile "org.jasig.cas:cas-server-support-rest:${project.version}"
${project.version} 是你的cas版本,这里填4.2.1
填好之后,重新编译
1 | gradle :cas-server-webapp:build |
编译完成后,将生成的war包上传至tomcat的webapps目录下。
注册service
重新启动tomcat后,就可以使用restful api了。但是你会发现,cas给你 返回的结果是没有注册的service
这里我们使用json文件注册的方式,更多方式参见官网
在/webapps/cas/WEB-INF下,编辑deployerConfigContext.xml文件,添加一行:1
<alias name="jsonServiceRegistryDao" alias="serviceRegistryDao" />
编辑 cas.properties文件,添加如下一行:1
service.registry.config.location=classpath:services
然后在cas/WEB-INF/classes/services目录下创建一个json文件:1
2
3
4
5
6
7{
"@class" : "org.jasig.cas.services.RegexRegisteredService",
"serviceId" : "http://test.abc.com",
"name" : "testId",
"id" : 1,
"evaluationOrder" : 0
}
这里的serviceId就是你注册的service
获取TGT
1 | POST /cas/v1/tickets |
返回的TGT结果1
2201 Created
[('content-length', '435'), ('server', 'Apache-Coyote/1.1'), ('location', 'https://portal.saas.odoo-server.corp.esmart365.com:8443/cas/v1/tickets/TGT-1-rubn9xwZxEyS3GzkyKxWfVtIjwgrVVdPIqEgbdnUmFoF2a3HiM-portal.saas.odoo-server.corp.esmart365.com'), ('cache-control', 'no-store'), ('date', 'Tue, 26 Dec 2017 09:13:50 GMT'), ('content-type', 'text/html;charset=UTF-8')]
通过TGT获取ST
1 | POST /cas/v1/tickets/{YOUR TGT}?service={YOUR SERIVICE} |
返回结果:1
2
3200 OK
[('content-length', '68'), ('content-disposition', 'inline;filename=f.txt'), ('server', 'Apache-Coyote/1.1'), ('cache-control', 'no-store'), ('date', 'Tue, 26 Dec 2017 09:14:09 GMT'), ('content-type', 'application/x-msdownload;charset=UTF-8')]
ST-1-nKAFecKy5ByA5beteUFF-portal.saas.odoo-server.corp.esmart365.com
验证ST
1 | GET /cas/p3/serviceValidate?service={YOUR SERVICE}&ticket={YOUR ST} |
返回XML结果:1
2
3
4
5
6
7
8
9
10<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>admin</cas:user>
<cas:attributes>
<cas:longTermAuthenticationRequestTokenUsed>false</cas:longTermAuthenticationRequestTokenUsed>
<cas:isFromNewLogin>true</cas:isFromNewLogin>
<cas:authenticationDate>2017-12-26T17:13:50.146+08:00</cas:authenticationDate>
</cas:attributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
问题:Ticket ‘XXXX’ not recognized 未能够识别出目标 票根
- host与你服务器的域名不匹配
解决方案:修改cas.properties中的host字段为你当前的域名。 - 超时时间 默认为10s
解决方案:WEB-INF/cas.properties 中将 st.timeToKillInSeconds=1800 设置为半小时。