1 <!-- https://mvnrepository.com/artifact/org.jasig.cas.client/cas-client-core --> 2 <dependency> 3 <groupId>org.jasig.cas.client</groupId> 4 <artifactId>cas-client-core</artifactId> 5 <version>3.2.1</version> 6 </dependency>
不用maven的話能夠用上面的地址去手動下載jar包。web
1 <!-- 該過濾器配置負責對Ticket的校驗工做--> 2 <filter> 3 <filter-name>CAS Validation Filter</filter-name> 4 <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class> 5 <init-param> 6 <param-name>casServerUrlPrefix</param-name> 7 <param-value>http://www.mycas.com:8088/cas-server-webapp-3.5.2</param-value> 8 </init-param> 9 <init-param> 10 <param-name>serverName</param-name> 11 <param-value>http://127.0.0.1:80/</param-value> 12 </init-param> 13 <init-param> 14 <param-name>renew</param-name> 15 <param-value>false</param-value> 16 </init-param> 17 <init-param> 18 <param-name>gateway</param-name> 19 <param-value>false</param-value> 20 </init-param> 21 <init-param> 22 <param-name>encoding</param-name> 23 <param-value>UTF-8</param-value> 24 </init-param> 25 </filter> 26 <filter-mapping> 27 <filter-name>CAS Validation Filter</filter-name> 28 <url-pattern>/*</url-pattern> 29 </filter-mapping>
2.2 AuthenticationFilter(必需)
該過濾器負責用戶的認證工做,casServerLoginUrl:cas服務端登陸地址(注意這裏比上面的多了'/login'),serverName:同上服務器
1 <!--該過濾器負責用戶的認證工做--> 2 <filter> 3 <filter-name>CASFilter</filter-name> 4 <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class> 5 <init-param> 6 <param-name>casServerLoginUrl</param-name> 7 <param-value>http://www.mycas.com:8088/cas-server-webapp-3.5.2/login</param-value> 8 </init-param> 9 <init-param> 10 <param-name>serverName</param-name> 11 <param-value>http://127.0.0.1:80/</param-value> 12 </init-param> 13 </filter> 14 <filter-mapping> 15 <filter-name>CASFilter</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping>
2.3 HttpServletRequestWrapperFilter (可選)與 AssertionThreadLocalFilter(可選)app
1 <!--HttpServletRequestWrapperFilter--> 2 <filter> 3 <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> 4 <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping> 10 11 <!--AssertionThreadLocalFilter--> 12 <filter> 13 <filter-name>CAS Assertion Thread Local Filter</filter-name> 14 <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class> 15 </filter> 16 <filter-mapping> 17 <filter-name>CAS Assertion Thread Local Filter</filter-name> 18 <url-pattern>/*</url-pattern> 19 </filter-mapping>
這2個過濾器的功能是同樣的,因此放到一塊兒來說,當咱們登陸成功後,須要獲取登陸的用戶信息(只能獲取到用戶名),就須要配置上面2個過濾器的其中一個。獲取方式以下:
HttpServletRequestWrapperFilter :webapp
1 HttpServletRequest request2 = (HttpServletRequest) request; 2 // 從Cas服務器獲取登陸帳戶的用戶名(2種方式) 3 String username1 = request2.getUserPrincipal().toString(); 4 String username2 = request2.getRemoteUser();
AssertionThreadLocalFilter:maven
1 Assertion assertion = AssertionHolder.getAssertion(); 2 String username3 = assertion.getPrincipal().getName();