Shiro 1.2 開始提供了 Jasig CAS 單點登錄的支持,單點登錄主要用于多系統(tǒng)集成,即在多個系統(tǒng)中,用戶只需要到一個中央服務器登錄一次即可訪問這些系統(tǒng)中的任何一個,無須多次登錄。此處我們使用 Jasig CAS v4.0.0-RC3 版本:
https://github.com/Jasig/cas/tree/v4.0.0-RC3
Jasig CAS 單點登錄系統(tǒng)分為服務器端和客戶端,服務器端提供單點登錄,多個客戶端(子系統(tǒng))將跳轉到該服務器進行登錄驗證,大體流程如下:
http://localhost:9080/client/
,此時會跳到單點登錄服務器 https://localhost:8443/server/login?service=https://localhost:9443/client/cas
;https://localhost:9443/client/cas?ticket=ST-1-eh2cIo92F9syvoMs5DOg-cas01.example.org
,且?guī)е粋€ ticket;本章使用了和《第十四章 SSL》一樣的數字證書。
我們使用了 Jasig CAS 服務器 v4.0.0-RC3 版本,可以到其官方的 github 下載:https://github.com/Jasig/cas/tree/v4.0.0-RC3
下載,然后將其 cas-server-webapp 模塊封裝到 shiro-example-chapter15-server 模塊中,具體請參考源碼。
1、數字證書使用和《第十四章 SSL》一樣的數字證書,即將 localhost.keystore 拷貝到 shiro-example-chapter15-server 模塊根目錄下;
2、在 pom.xml 中添加 Jetty Maven 插件,并添加 SSL 支持:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.8.v20121106</version>
<configuration>
<webAppConfig>
<contextPath>/${project.build.finalName}</contextPath>
</webAppConfig>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
</connector>
<connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector">
<port>8443</port>
<keystore>${project.basedir}/localhost.keystore</keystore>
<password>123456</password>
<keyPassword>123456</keyPassword>
</connector>
</connectors>
</configuration>
</plugin>
3、修改 src/main/webapp/WEB-INF/deployerConfigContext.xml
,找到 primaryAuthenticationHandler,然后添加一個賬戶:
<entry key="zhang" value="123"/>
其也支持如 JDBC 查詢,可以自己定制;具體請參考文檔。
4、mvn jetty:run 啟動服務器測試即可:
訪問 https://localhost:8443/chapter15-server/login
將彈出如下登錄頁面:
輸入用戶名 / 密碼,如 zhang/123,將顯示登錄成功頁面:
到此服務器端的簡單配置就完成了。
1、首先使用 localhost.keystore 導出數字證書(公鑰)到 D:\localhost.cer
keytool -export -alias localhost -file D:\localhost.cer -keystore D:\localhost.keystore
2、因為 CAS client 需要使用該證書進行驗證,需要將證書導入到 JDK 中:
cd D:\jdk1.7.0_21\jre\lib\security
keytool -import -alias localhost -file D:\localhost.cer -noprompt -trustcacerts -storetype jks -keystore cacerts -storepass 123456
如果導入失敗,可以先把 security 目錄下的 cacerts 刪掉;
3、按照服務器端的 Jetty Maven 插件的配置方式配置 Jetty 插件;
4、在 shiro-example-chapter15-client 模塊中導入 shiro-cas 依賴,具體請參考其 pom.xml;
5、自定義 CasRealm:
public class MyCasRealm extends CasRealm {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String)principals.getPrimaryPrincipal();
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
authorizationInfo.setRoles(userService.findRoles(username));
authorizationInfo.setStringPermissions(userService.findPermissions(username));
return authorizationInfo;
}
}
CasRealm 根據 CAS 服務器端返回的用戶身份獲取相應的角色 / 權限信息。
6、spring-shiro-web.xml 配置:
<bean id="casRealm" class="com.github.zhangkaitao.shiro.chapter13.realm.MyCasRealm">
<property name="userService" ref="userService"/>
……
<property name="casServerUrlPrefix" value="https://localhost:8443/chapter14-server"/>
<property name="casService" value="https://localhost:9443/chapter14-client/cas"/>
</bean>
casServerUrlPrefix:是 CAS Server 服務器端地址;
casService:是當前應用 CAS 服務 URL,即用于接收并處理登錄成功后的 Ticket 的;
如果角色 / 權限信息是由服務器端提供的話,我們可以直接使用 CasRealm:
<bean id="casRealm" class="org.apache.shiro.cas.CasRealm">
……
<property name="defaultRoles" value="admin,user"/>
<property name="defaultPermissions" value="user:create,user:update"/>
<property name="roleAttributeNames" value="roles"/>
<property name="permissionAttributeNames" value="permissions"/>
<property name="casServerUrlPrefix" value="https://localhost:8443/chapter14-server"/>
<property name="casService" value="https://localhost:9443/chapter14-client/cas"/>
</bean>
defaultRoles/ defaultPermissions:默認添加給所有 CAS 登錄成功用戶的角色和權限信息; roleAttributeNames/ permissionAttributeNames:角色屬性 / 權限屬性名稱,如果用戶的角色 / 權限信息是從服務器端返回的(即返回的 CAS Principal 中除了 Principal 之外還有如一些 Attributes),此時可以使用 roleAttributeNames/ permissionAttributeNames 得到 Attributes 中的角色 / 權限數據;請自行查詢 CAS 獲取用戶更多信息。
<bean id="casFilter" class="org.apache.shiro.cas.CasFilter">
<property name="failureUrl" value="/casFailure.jsp"/>
</bean>
CasFilter 類似于 FormAuthenticationFilter,只不過其驗證服務器端返回的 CAS Service Ticket。
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="https://localhost:8443/chapter14-server/login?service=https://localhost:9443/chapter14-client/cas"/>
<property name="successUrl" value="/"/>
<property name="filters">
<util:map>
<entry key="cas" value-ref="casFilter"/>
</util:map>
</property>
<property name="filterChainDefinitions">
<value>
/casFailure.jsp = anon
/cas = cas
/logout = logout
/** = user
</value>
</property>
</bean>
loginUrl:https://localhost:8443/chapter15-server/login
表示服務端端登錄地址,登錄成功后跳轉到?service 參數對于的地址進行客戶端驗證及登錄;
“/cas=cas”:即 /cas 地址是服務器端回調地址,使用 CasFilter 獲取 Ticket 進行登錄。
7、測試,輸入 http://localhost:9080/chapter15-client
地址進行測試即可,可以使用如 Chrome 開這 debug 觀察網絡請求的變化。
如果遇到以下異常,一般是證書導入錯誤造成的,請嘗試重新導入,如果還是不行,有可能是運行應用的 JDK 和安裝數字證書的 JDK 不是同一個造成的:
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
... 67 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
... 73 more
更多建議: