CAS客户端(Client)接入手册
作者: 刘凯
邮箱: zhizhufan@foxmail.com
钉钉账号: 9cw-qkodvrspf
CAS Server 信息
- CAS Server地址: http://106.13.37.193:8480/cas/login
单点登录
登录流程
-
浏览器访问 http://106.13.37.193:8480/cas/login?service=http://your-app.com/login-success
请将http://your-app.com/login-success 替换成你的系统地址
-
然后输入认证信息进行认证
-
认证成功后,CAS Server会生成一个票据(Ticket)重定向回你的应用,并附带票据作为查询参数。
例如:http://your-app.com/login-success?ticket=ST-0123456789
Ticket是一次性的,并且10秒就会失效
-
在你的应用中,通过获取请求参数中的票据信息来进行验证。你的应用要发送一个如下的 HTTP的GET 请求到 CAS Server,http://106.13.37.193:8480/cas/p3/serviceValidate?ticket=ST-0123456789&service=http://your-app.com/login-success 因为Ticket是一次性的,所以这个请求发送过Ticket就会失效,CAS Service返回一个如下的XML, 读取并解析
cas:user
属性获取当前登录人的账号
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:authenticationSuccess>
<cas:user>admin</cas:user>
<cas:attributes>
<cas:credentialType>UsernamePasswordCaptchaCredential</cas:credentialType>
<cas:isFromNewLogin>true</cas:isFromNewLogin>
<cas:authenticationDate>2023-09-21T15:51:14.275+08:00[Asia/Shanghai]</cas:authenticationDate>
<cas:authenticationMethod>QueryDatabaseAuthenticationHandler</cas:authenticationMethod>
<cas:successfulAuthenticationHandlers>UsernamePasswordCaptchaAuthenticationHandler
</cas:successfulAuthenticationHandlers>
<cas:name>超管</cas:name>
<cas:longTermAuthenticationRequestTokenUsed>false</cas:longTermAuthenticationRequestTokenUsed>
</cas:attributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
单点退出
退出流程
- 首先需要接入的系统提供一个退出的HTTP接口,要求是POST请求。比如http://your-app.com/logout/cas
- 在浏览器中请求http://106.13.37.193:8480/cas/logout?service=http://your-app.com/login-success ,
请将http://your-app.com/login-success 替换成你的系统地址 - 在上一步跳转到Cas Server之后,Cas Server会向已经登录过的系统发送一个POST请求,请求如下
POST http://your-app.com/logout/cas
Content-Type: application/x-www-form-urlencoded
logoutRequest=<samlp:LogoutRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="LR-1-cKY5Ta40H8o8Pc3g8dYKhKHL" Version="2.0" IssueInstant="2024-04-18T16:21:49Z"><saml:NameID xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">@NOT_USED@</saml:NameID><samlp:SessionIndex>ST-1-iFCUl9SuvVDmsxGfDT-pUr9uHsQK-Mac</samlp:SessionIndex></samlp:LogoutRequest>
需要接入的系统解析上面的logoutRequest参数,提取出<samlp:SessionIndex>ST-1-iFCUl9SuvVDmsxGfDT-pUr9uHsQK-Mac</samlp:SessionIndex>
里面的内容。然后做退出操作。