單點登錄解決方案-CAS

單點登錄解決方案-CAS

1. 開源單點登錄系統CAS入門

什麼是單點登錄

  • 單點登錄(Single Sign On),簡稱爲 SSO,是目前比較流行的企業業務整合的解決方案之一。SSO的定義是在多個應用系統中,用戶只需要登錄一次就可以訪問所有相互信任的應用系統。
  • 我們目前的系統存在諸多子系統,而這些子系統是分別部署在不同的服務器中,那麼使用傳統方式的session是無法解決的,我們需要使用相關的單點登錄技術來解決。
    在這裏插入圖片描述

什麼是CAS

CAS 是 Yale 大學發起的一個開源項目,旨在爲 Web 應用系統提供一種可靠的單點登錄方法,CAS 在 2004 年 12 月正式成爲 JA-SIG 的一個項目。CAS 具有以下特點:

  1. 開源的企業級單點登錄解決方案。
  2. CAS Server 爲需要獨立部署的 Web 應用。
  3. CAS Client 支持非常多的客戶端(這裏指單點登錄系統中的各個 Web 應用),包括 Java, .Net, PHP, Perl, Apache, uPortal, Ruby 等。

在這裏插入圖片描述
SSO單點登錄訪問流程主要有以下步驟:
4. 訪問服務:SSO客戶端發送請求訪問應用系統提供的服務資源。
5. 定向認證:SSO客戶端會重定向用戶請求到SSO服務器。
6. 用戶認證:用戶身份認證。
7. 發放票據:SSO服務器會產生一個隨機的Service Ticket。
8. 驗證票據:SSO服務器驗證票據Service Ticket的合法性,驗證通過後,允許客戶端訪問服務。
9. 傳輸用戶信息:SSO服務器驗證票據通過後,傳輸用戶認證結果信息給客戶端。

CAS服務端部署

1. 下載CAS

Cas服務端其實就是一個war包。在\CAS\source\cas-server-4.0.0-release\cas-server-4.0.0\modules\下的cas-server-webapp-4.0.0.war,

  • Csa下載

鏈接:https://pan.baidu.com/s/1MELIcgCJtDNL80azatC9CQ
提取碼:bnnt
複製這段內容後打開百度網盤手機App,操作更方便哦

2. 部署
  • 將cas-server-webapp-4.0.0.war,複製到一個新的tomcat下的webapps下,更名爲cas.war(爲了在訪問的時候更方便)
  • 啓動tomcat,在瀏覽器輸入http://localhost:8080/cas即可訪問,默認的登錄名和密碼爲 casuser /Mellon,點擊登錄即可登錄成功
    在這裏插入圖片描述
3. 端口的修改

當我們不希望使用8080端口進行訪問時,我們可以進行修改

  1. 修改tomcat端口:在tomcat目錄下的conf下的server.xml裏面進行修改,這裏我改爲9100
    在這裏插入圖片描述
  2. 修改CAS的配置文件:修改cas下的WEB-INF下的cas.properties文件。這裏我也改爲9100(與tomcat保持一致)
    在這裏插入圖片描述
  3. 重啓tomcat,使用9100端口訪問(修改成功)
    在這裏插入圖片描述
4. 去除HTTPS認證

CAS默認使用的是HTTPS協議,如果使用HTTPS協議需要SSL安全證書(需向特定的機構申請和購買) 。如果對安全要求不高或是在開發測試階段,可使用HTTP協議。我們這裏講解通過修改配置,讓CAS使用HTTP協議。

  1. 修改cas下的WEB-INF下的deployerConfigContext.xml打開後在下列位置添加p:requireSecure=“false”(下圖是我添加以後的圖片)
    在這裏插入圖片描述

requireSecure屬性表示是否啓用安全認證,即HTTPS,默認是true,

  1. 修改cas的/WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
    找到下面配置,將true改爲false
    在這裏插入圖片描述

p:cookieSecure=“true”,同理爲HTTPS驗證相關,TRUE爲採用HTTPS驗證,FALSE爲不採用https驗證。

p:cookieMaxAge="-1",是COOKIE的最大生命週期,-1爲無生命週期,即只在當前打開的窗口有效,關閉或重新打開其它窗口,仍會要求驗證。可以根據需要修改爲大於0的數字,比如3600等,意思是在3600秒內,打開任意窗口,都不需要驗證。

  1. 修改cas的WEB-INF/spring-configuration/warnCookieGenerator.xml
    找到下面配置,進行更改
    在這裏插入圖片描述
5. 通過配置文件添加一個用戶
  • 在cas\WEB-INF下的deployerConfigContext.xml中模仿下圖配置
    在這裏插入圖片描述

CAS客戶端入門案例(cas原生方法實現)

1. 單點登錄
  1. 創建兩個工程,來試驗單點登錄(即一次登錄,到處運行,)
  2. 在pom.xml中引入依賴,項目一配置Tomcat端口爲9001,項目二Tomcat端口爲9002
<dependencies>
		<!-- cas -->  
		<dependency>  
		    <groupId>org.jasig.cas.client</groupId>  
		    <artifactId>cas-client-core</artifactId>  
		    <version>3.3.3</version>  
		</dependency>  		
		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>  
			<scope>provided</scope>
		</dependency>
	</dependencies>  

  <build>  
	  <plugins>
	      <plugin>  
	          <groupId>org.apache.maven.plugins</groupId>  
	          <artifactId>maven-compiler-plugin</artifactId>  
	          <version>2.3.2</version>  
	          <configuration>  
	              <source>1.7</source>  
	              <target>1.7</target>  
	          </configuration>  
	      </plugin>  
	      <plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 指定端口 -->
					<port>9002</port>
					<!-- 請求路徑 -->
					<path>/</path>
				</configuration>
	  	  </plugin>
	  </plugins>  
    </build>
  1. 修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">	
	
	<!-- ======================== 單點登錄開始 ======================== -->  
    <!-- 用於單點退出,該過濾器用於實現單點登出功能,可選配置 -->  
    <listener>  
        <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  
    </listener>  
  
    <!-- 該過濾器用於實現單點登出功能,可選配置。 -->  
    <filter>  
        <filter-name>CAS Single Sign Out Filter</filter-name>  
        <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>CAS Single Sign Out Filter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
  
    <!-- 該過濾器負責用戶的認證工作,必須啓用它 -->  
    <filter>  
        <filter-name>CASFilter</filter-name>  
        <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  
        <init-param>  
            <param-name>casServerLoginUrl</param-name>  
            <param-value>http://localhost:9100/cas/login</param-value>  
            <!--這裏的server是服務端的IP -->  
        </init-param>  
        <init-param>  
            <param-name>serverName</param-name>  
            <param-value>http://localhost:9002</param-value>
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>CASFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
  
    <!-- 該過濾器負責對Ticket的校驗工作,必須啓用它 -->  
    <filter>  
        <filter-name>CAS Validation Filter</filter-name>  
        <filter-class>  
            org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>  
        <init-param>  
            <param-name>casServerUrlPrefix</param-name>  
            <param-value>http://localhost:9100/cas</param-value>  
        </init-param>  
        <init-param>  
            <param-name>serverName</param-name>  
            <param-value>http://localhost:9002</param-value>
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>CAS Validation Filter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
  
    <!-- 該過濾器負責實現HttpServletRequest請求的包裹, 比如允許開發者通過HttpServletRequest的getRemoteUser()方法獲得SSO登錄用戶的登錄名,可選配置。 -->  
    <filter>  
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
        <filter-class>  
            org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
  
    <!-- 該過濾器使得開發者可以通過org.jasig.cas.client.util.AssertionHolder來獲取用戶的登錄名。 比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->  
    <filter>  
        <filter-name>CAS Assertion Thread Local Filter</filter-name>  
        <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>CAS Assertion Thread Local Filter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
  
    <!-- ======================== 單點登錄結束 ======================== -->  
</web-app>

在這裏插入圖片描述
4. 分別在兩個項目創建一個index.jsp,分別顯示歡迎來到品優購一和歡迎來到品優購二
在這裏插入圖片描述
5. 啓動cas服務端,啓動兩個客戶端,在瀏覽器輸入localhost:9001訪問,會發現自動跳轉到了9100的服務端,在服務端登陸以後,會自動跳轉到剛纔的頁面,這是再次打開一個瀏覽器,輸入localhost:9002訪問,發現不用登陸就可以直接訪問,這就是單點登錄,在一個模塊登錄,登錄信息會發送到其他模塊,訪問其他模塊時就不需要登陸了
在這裏插入圖片描述

2. 單點退出登錄
  • 地址欄輸入http://localhost:9100/cas/logout鏈接,回車即可退出登錄
    在這裏插入圖片描述
  • 這時再請求localhost:9001就需要重新進行登錄
3.單點登錄退出後跳轉到某個頁面
  • 修改cas下的cas-servlet.xml
    在這裏插入圖片描述
  • 重啓服務,在兩個客戶端的index.jsp裏面添加一個超鏈接
    在這裏插入圖片描述

點擊該鏈接就會退出登錄跳轉到百度的首頁

4. 配置使用數據庫的賬號密碼登錄
  1. 在cas\WEB-INF下的deployerConfigContext.xml中添加下面三個bean
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
			  p:driverClass="com.mysql.jdbc.Driver"  
			  p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/pinyougoudb?characterEncoding=utf8"  
			  p:user="root"  
			  p:password="123456" /> 
<bean id="passwordEncoder" 
class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"  
		c:encodingAlgorithm="MD5"  
		p:characterEncoding="UTF-8" />  
<bean id="dbAuthHandler"  
		  class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"  
		  p:dataSource-ref="dataSource"  
		  p:sql="select password from tb_user where username = ?"  
		  p:passwordEncoder-ref="passwordEncoder"/>  

在這裏插入圖片描述
2. 在上面的一個bean中進行修改
在這裏插入圖片描述
3. 引入jar包,因爲在這裏我們用到了mysql以及c3p0連接池,所以我們要引入這些jar包以及一個cas服務端jdbc的支持包
在這裏插入圖片描述

  1. 重新啓動,使用數據庫中的賬號密碼測試
5. 更改頁面(參考)

在這裏插入圖片描述

2. CAS客戶端與SpringSecurity集成

1. pom.xml
  1. 引入spring-security的依賴
  2. 引入spring-security整合cas的依賴
  3. 引入cas原生的依賴

需要將log4j的依賴排除,因爲在spring-security裏面有log4j的依賴了,cas還會引入一個log4j,,這會造成版本衝突

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lld.demo</groupId>
  <artifactId>cas_spring_security_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
		<spring.version>4.2.4.RELEASE</spring.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>4.1.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-config</artifactId>
			<version>4.1.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		
		<dependency>  
			   <groupId>org.springframework.security</groupId>  
			   <artifactId>spring-security-cas</artifactId>  
			   <version>4.1.0.RELEASE</version>  
		</dependency>     
		<dependency>  
		        <groupId>org.jasig.cas.client</groupId>  
		        <artifactId>cas-client-core</artifactId>  
		        <version>3.3.3</version>  
		        <exclusions>  
		            <exclusion>  
		                <groupId>org.slf4j</groupId>  
		                <artifactId>log4j-over-slf4j</artifactId>  
		            </exclusion>  
		        </exclusions>  
		</dependency> 
		

	</dependencies>
	<build>
	  <plugins>		
	      <!-- java編譯插件 -->
		  <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
		  </plugin>      
	      <plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 指定端口 -->
					<port>9003</port>
					<!-- 請求路徑 -->
					<path>/</path>
				</configuration>
	  	  </plugin>
	   </plugins>  
    </build>
</project>
2. spring-security.xml文件(沒聽懂325集 )
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
	
	<!--   entry-point-ref  入口點引用 -->
	<http use-expressions="false" entry-point-ref="casProcessingFilterEntryPoint">  
        <intercept-url pattern="/**" access="ROLE_USER"/>   
        <csrf disabled="true"/>  
        <!-- custom-filter爲過濾器, position 表示將過濾器放在指定的位置上,before表示放在指定位置之前  ,after表示放在指定的位置之後  -->           
        <custom-filter ref="casAuthenticationFilter"  position="CAS_FILTER" />      
        <custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER"/>  
        <custom-filter ref="singleLogoutFilter" before="CAS_FILTER"/>  
    </http>
    
  	<!-- CAS入口點 開始 -->
    <beans:bean id="casProcessingFilterEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">  
        <!-- 單點登錄服務器登錄URL -->  
        <beans:property name="loginUrl" value="http://localhost:9100/cas/login"/>  
        <beans:property name="serviceProperties" ref="serviceProperties"/>  
    </beans:bean>      
    <beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">  
        <!--service 配置自身工程的根地址+/login/cas   -->  
        <beans:property name="service" value="http://localhost:9003/login/cas"/>
    </beans:bean>  
    <!-- CAS入口點 結束 -->

    
    <!-- 認證過濾器 開始 -->
    <beans:bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">  
        <beans:property name="authenticationManager" ref="authenticationManager"/>  
    </beans:bean>  
		<!-- 認證管理器 -->
	<authentication-manager alias="authenticationManager">
		<authentication-provider  ref="casAuthenticationProvider">
		</authentication-provider>
	</authentication-manager>
		<!-- 認證提供者 -->
	<beans:bean id="casAuthenticationProvider"     class="org.springframework.security.cas.authentication.CasAuthenticationProvider">  
        <beans:property name="authenticationUserDetailsService">  
            <beans:bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">  
                <beans:constructor-arg ref="userDetailsService" />  
            </beans:bean>  
        </beans:property>  
        <beans:property name="serviceProperties" ref="serviceProperties"/>  
        <!-- ticketValidator 爲票據驗證器 -->
        <beans:property name="ticketValidator">  
            <beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">  
                <beans:constructor-arg index="0" value="http://localhost:9100/cas"/>  
            </beans:bean>  
        </beans:property>  
        <beans:property name="key" value="an_id_for_this_auth_provider_only"/> 
    </beans:bean>        
   		 <!-- 認證類 -->
	<beans:bean id="userDetailsService" class="cn.itcast.demo.service.UserDetailServiceImpl"/>  
	
	<!-- 認證過濾器 結束 -->
	
	
	<!-- 單點登出  開始  -->     
    <beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>          
    <beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">  
        <beans:constructor-arg value="http://localhost:9100/cas/logout?service=http://www.baidu.com"/>  
        <beans:constructor-arg>  
            <beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>  
        </beans:constructor-arg>  
        <beans:property name="filterProcessesUrl" value="/logout/cas"/>  
    </beans:bean>  
    <!-- 單點登出  結束 -->  
	
</beans:beans>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章