CAS协议:
1.没有验证的情况下访问由CAS控制的服务,返回登录验证界面:
http://application.examples.com/protected/index.jsp → HTTP 302
→ https://server.cas.com/login?service=http://application.examples.com/shiro-cas
2.在某一服务中取得验证后,使用一次性的服务票据ticket去访问CAS控制的服务,ticket作为用户的短暂凭证:
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所控制服务资源:
http://application.examples.com/shiro-cas?ticket=ST-4545454542121-cas → HTTP 302
→ http://application.examples.com/protected/index.jsp
shiro与CAS的集成,需要shiro1.2以上版本
maven的pom.xml配置文件:
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-cas</artifactId>
<version>version</version>
</dependency>
shiro.ini配置CASfilter
[main]
casFilter = org.apache.shiro.cas.CasFilter
casFilter.failureUrl = /error.jsp //ticket无效时调用
[urls]
/shiro-cas = casFilter //要在请求服务和回应服务端声明url来接收ticket,如http://application.examples.com/shiro-cas
shiro.ini配置CasRelam
[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服务地址,如https://server.cas.com;
casService:服务ticket发送、接收地址,如http://application.examples.com/shiro-cas;
validationProcol:可以是SAML(安全声明标记语言)或者CAS(默认是CAS),决定用户验证信息以何种方式来进行传输(CAS3.1版本以后,才可用SAML);
如果选用SAML需要在pom.xml中配置更多的附件:
<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认证成功分配给用户的角色;
permissionAttributeNames:获取CAS认证成功后分配给用户的权限。
为了实现记住我功能,需要实现CasSubjectFactory:
[main]
casSubjectFactory = org.apache.shiro.cas.CasSubjectFactory
securityManager.subjectFactory = $casSubjectFactory
配置过滤规则:
[urls]
/protected/** = roles[ROLE_USER]
/** = anon
配置登陆验证界面,当用户未获认证时,返回登录验证界面:
[main]
roles.loginUrl = https://server.cas.com/login?service=http://application.examples.com/shiro-cas