CAS單點登陸學習(二):客戶端配置

  1. 下載jar包
    由於cas的源碼修改變更很大,因此客戶端引入的jar包根據服務端的war包而定。以前搭建的cas服務端用的版本是3.5.2,通過測試,可使用cas-client-core的3.2.1版本。使用maven添加依賴
    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

  2. web.xml配置
    引入jar包後,只要在客戶端的web.xml裏配置相應的過濾器便可。根據添加的過濾器的位置順序一一介紹:

    2.1 Cas20ProxyReceivingTicketValidationFilter(必需)
      該過濾器負責對ticket的校驗工做,這個過濾器要放在第一個位置。casServerUrlPrefix:cas服務器訪問地址,serverName:客戶端域名和端口。這裏也能夠配置成ip,可是最好仍是配置成域名。
      還有就是cas服務端我取消了https,這裏路徑注意一下。
     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();
  3. 到這裏客戶端的配置就完成了~~
相關文章
相關標籤/搜索