# 17. CAS
# 17. CAS
shiro-cas 模塊是用來保護一個 [Jasig CAS](http://www.jasig.org/cas) 單點登錄服務器。它使一個 Shiro-enabled 程序變成 CAS 客戶端
## Basic understanding of the CAS protocol CAS協議的基本理解
1. 如果你想訪問一個應用程序由 CAS 保護,并且如果你不驗證在這個應用程序中的客戶端,你重定向通過 CAS 客戶端 到 CAS 服務器登錄頁面。 在 CAS 登錄 url 定義了應用程序用戶希望登錄服務參數。
<http://application.examples.com/protected/index.jsp> → HTTP 302 → <https://server.cas.com/login?service=http://application.examples.com/shiro-cas>
2. 你填寫的登錄名和密碼和驗證CAS服務器,然后將用戶重定向到應用程序(服務的 url)和 url 的服務票證。 服務票證是短暫的一次性令牌可贖回在CAS服務器用戶標識符(和可選地,用戶屬性)。
<https://server.cas.com/login?service=http://application.examples.com/shiro-cas> → HTTP 302 → <http://application.examples.com/shiro-cas?ticket=ST-4545454542121-cas>
3. 應用程序直接問 CAS 服務器如果服務票是有效和 CAS 服務器響應通過身份驗證的用戶的身份。 一般來說,CAS 端轉發用戶最初叫保護頁面。
<http://application.examples.com/shiro-cas?ticket=ST-4545454542121-cas> → HTTP 302 → <http://application.examples.com/protected/index.jsp>
## How to configure shiro to work with CAS server ? 配置
### Dependency 依賴
```
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-cas</artifactId>
<version>version</version>
</dependency>
```
( 版本 > = 1.2.0)。
### CasFilter
你必須定義服務應用程序的 ur l(在 CAS 服務器也必須聲明)。 該url將被用來接收 CAS 服務票證。 例如: <http://application.examples.com/shiro-cas>
在 shiro 配置,您必須定義 CasFilter :
```
[main]
casFilter = org.apache.shiro.cas.CasFilter
casFilter.failureUrl = /error.jsp
```
(失敗的 url 時,服務票驗證失敗)。
和url可用:
```
[urls]
/shiro-cas = casFilter
```
這樣,當用戶通過 CAS服務器使用有效的服務票證(身份驗證)后, 被重定向到應用程序服務 url (/shiro-cas),這個過濾器接收服務票證,并創建一個 CasToken 可以被 CasRealm 使用。
### CasRealm
CasRealm 使用 CasFilter 創建的 CasToken 通過 CAS 對CAS服務器服務票證 查驗,從而對用戶進行身份驗證
在shiro配置,您必須添加 CasRealm :
```
[main]
casRealm = org.apache.shiro.cas.CasRealm
casRealm.defaultRoles = ROLE_USER
#casRealm.defaultPermissions
#casRealm.roleAttributeNames
#casRealm.permissionAttributeNames
#casRealm.validationProtocol = SAML
casRealm.casServerUrlPrefix = https://server.cas.com/
casRealm.casService = http://application.examples.com/shiro-cas
```
casServerUrlPrefix 是 CAS 服務器的 url(例如: [https://server.cas.com](https://server.cas.com/) )。
casService 是應用程序服務 url,url 指向應用程序收到 CAS 服務票證(例如: <http://application.examples.com/shiro-cas> )。
validationProcol可以是 SAML或 CAS(默認):屬性 和 remember me信息 只推到 SAML 驗證協議(具體定制除外)。 這取決于 CAS 服務器的版本:SAML 協議可以使用 CAS 服務器版本 >= 3.1。
*如果選擇 SAML 驗證,需要添加更多依賴*
```
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.opensaml</groupId>
<artifactId>opensaml</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>1.4.3</version>
</dependency>
```
defaultRoles 是默認的角色給了 CAS 認證成功后通過身份驗證的用戶。
defaultPermissions 是默認的權限給了 CAS 認證成功后通過身份驗證的用戶。
roleAttributeNames 定義屬性的名稱來自 CAS 響應定義角色給了身份驗證的用戶(角色由 comas 進行分隔 )。
permissionAttributeNames 定義屬性的名稱來自 CAS 響應它定義權限給身份驗證的用戶(權限由 comas 進行分隔 )。
### CasSubjectFactory
在 CAS 服務器,你可以“記住我”的支持。 這些信息是通過 SAML 驗證或CAS 定制的驗證。 反映在 Shiro CAS-remember 我地位,你必須定義一個特定的 CasSubjectFactory 在你的Shiro配置:
```
[main]
casSubjectFactory = org.apache.shiro.cas.CasSubjectFactory
securityManager.subjectFactory = $casSubjectFactory
```
### Security of the application應用程序的安全
最后,您必須定義您的應用程序的安全。
在 Shiro 配置,您必須保護 url 與角色(例如):
```
[urls]
/protected/** = roles[ROLE_USER]
/** = anon
```
和登錄 url 如果用戶沒有經過身份驗證的 CAS 服務器上定義與應用程序服務網址:
```
[main]
roles.loginUrl = https://server.cas.com/login?service=http://application.examples.com/shiro-cas
```
這樣,如果你不驗證和嘗試訪問 /保護/ url,您被重定向到CAS服務器進行身份驗證。
### Complete configuration sample 完整的配置樣例
```
[main]
casFilter = org.apache.shiro.cas.CasFilter
casFilter.failureUrl = /error.jsp
casRealm = org.apache.shiro.cas.CasRealm
casRealm.defaultRoles = ROLE_USER
casRealm.casServerUrlPrefix = https://server.cas.com/
casRealm.casService = http://application.examples.com/shiro-cas
casSubjectFactory = org.apache.shiro.cas.CasSubjectFactory
securityManager.subjectFactory = $casSubjectFactory
roles.loginUrl = https://server.cas.com/login?service=http://application.examples.com/shiro-cas
[urls]
/shiro-cas = casFilter
/protected/** = roles[ROLE_USER]
/** = anon
```
## History 歷史
Version 1.2.0 : shiro-cas 模塊第一個發布版本.
- Introduction
- 1. Introduction 介紹
- 2. Tutorial 教程
- 3. Architecture 架構
- 4. Configuration 配置
- 5. Authentication 認證
- 6. Authorization 授權
- 6.1. Permissions 權限
- 7. Realms
- 8. Session Management
- 9. Cryptography 密碼
- 10. Web
- 10.1. Configuration 配置
- 10.2. 基于路徑的 url 安全
- 10.3. Default Filters 默認過濾器
- 10.4. Session Management
- 10.5. JSP Tag Library
- 11. Caching 緩存
- 12. Concurrency & Multithreading 并發與多線程
- 13. Testing 測試
- 14. Custom Subjects 自定義 Subject
- 15. Spring Framework
- 16. Guice
- 17. CAS
- 18. Command Line Hasher
- 19. Terminology 術語
- 20. 10 Minute Tutorial 十分鐘教程
- 21. Beginner's Webapp Tutorial 初學者web應用教程
- 22. Application Security With Apache Shiro 用Shiro保護你的應用安全
- 23. CacheManager 緩存管理
- 24. Apache Shiro Cryptography Features 加密功能