中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

CAS實現SSO單點登錄

2018-10-11    來源:importnew

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

環(huán)境

cas-server-4.1.8,cas-client-3.4.0,Java-8,Maven-3,Tomcat-7.0.72

CAS Server 安裝

點此進入?CAS?下載列表,選擇下載?cas-4.1.8.zip。

解壓縮 cas-4.1.8.zip 并進入 cas-server-webapp 目錄,在當前目錄打開 cmd 并執(zhí)行安裝命令。

mvn -e -ff clean install -Dmaven.test.skip=true

經親身測試(自己拉的電信12M網絡),該安裝過程非常漫長,主要因為鏡像原因導致依賴包下載非常慢,此過程需靜心等待;蛑苯酉螺d我已經打好包的?cas.war?文件(注:該文件的依賴包版本有稍做修改,此不影響正常使用)。

安裝完成后,在 cas-server-webapp/target 目錄下可以看到 cas.war 文件,該文件便是 cas server 應用服務的 war 包。

cas server 安全認證是基于 https 的,這里使用 JDK 自帶的 keytool 工具生成數字證書,生產環(huán)境系統(tǒng)的應用需要到證書提供商處購買證書。證書的生成及 Tomcat 的配置可參考文章:keytool 生成數字證書 – tomcat https 配置?。

首先確保 Tomcat 的 https 可以正常訪問,將 cas.war 文件拷貝到 apache-tomcat-7.0.72/webapps 下進行發(fā)布,啟動 Tomcat,訪問?https://www.fanlychie.com:8443/cas。

上圖是用火狐瀏覽器打開的鏈接,選擇高級 -> 添加例外 -> 確認安全例外。

用戶名和密碼在 apache-tomcat-7.0.72/webapps/cas/WEB-INF/deployerConfigContext.xml 配置文件中,找到并打開該文件,大概在 105 行

<bean id="primaryAuthenticationHandler"
	class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">
	<property name="users">
		<map>
			<entry key="casuser" value="Mellon" />
		</map>
	</property>
</bean>

可以看到默認的用戶名是 casuser,密碼是 Mellon。

看到上圖的頁面,表明 cas server 已經部署成功。

CAS Server 配置基于數據庫用戶認證

回到 cas-4.1.8.zip 解壓縮的目錄,并進入 cas-server-support-jdbc 目錄,在當前目錄打開 cmd 并執(zhí)行安裝命令

mvn -e -ff clean install -Dmaven.test.skip=true

安裝完成后在 target 目錄得到 cas-server-support-jdbc-4.1.8.jar 文件。

將該文件拷貝到 apache-tomcat-7.0.72/webapps/cas/WEB-INF/lib 目錄下,并向此目錄添加 c3p0-0.9.1.2.jar,mysql-connector-java-5.1.17.jar 兩個文件。嫌麻煩的話,點此下載這三個 jar 包的壓縮包文件。

再次打開 apache-tomcat-7.0.72/webapps/cas/WEB-INF/deployerConfigContext.xml 文件,大概在第 54 行。

<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">
    <constructor-arg>
        <map>
            <!--
               | IMPORTANT
               | Every handler requires a unique name.
               | If more than one instance of the same handler class is configured, you must explicitly
               | set its name to something other than its default name (typically the simple class name).
               -->
            <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />
            <!-- 注銷此項
            <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
            -->
            <!-- 添加此項 -->
            <entry key-ref="myAuthenticationHandler" value-ref="primaryPrincipalResolver" />
        </map>
    </constructor-arg>
    <!-- Uncomment the metadata populator to capture the password.
    <property name="authenticationMetaDataPopulators">
       <util:list>
           <bean class="org.jasig.cas.authentication.CacheCredentialsMetaDataPopulator"/>
       </util:list>
    </property>
    -->
    <!--
       | Defines the security policy around authentication. Some alternative policies that ship with CAS:
       |
       | * NotPreventedAuthenticationPolicy - all credential must either pass or fail authentication
       | * AllAuthenticationPolicy - all presented credential must be authenticated successfully
       | * RequiredHandlerAuthenticationPolicy - specifies a handler that must authenticate its credential to pass
       -->
    <property name="authenticationPolicy">
        <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" />
    </property>
</bean>

按以上配置注銷掉第二個 entry 并添加一個 entry。接著在后面添加兩個 bean 配置。

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/cas_test_db?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8" />
    <property name="user" value="root" />
    <property name="password" value="root" />
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="initialPoolSize" value="10" />
    <property name="maxIdleTime" value="1800" />
    <property name="maxPoolSize" value="60" />
    <property name="acquireIncrement" value="5" />
    <property name="acquireRetryAttempts" value="60" />
    <property name="acquireRetryDelay" value="2000" />
    <property name="breakAfterAcquireFailure" value="false" />
    <property name="autoCommitOnClose" value="false" />
    <property name="checkoutTimeout" value="30000" />
    <property name="idleConnectionTestPeriod" value="900" />
</bean>
<bean id="myAuthenticationHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" 
    p:dataSource-ref="dataSource" 
    p:sql="SELECT passwd FROM user WHERE name = ?" />

其中 cas_test_db 數據庫中的 user 建表語句為

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `passwd` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
)

重啟 Tomcat,訪問?https://www.fanlychie.com:8443/cas,用數據庫中的 name/passwd 作為用戶名和密碼登錄系統(tǒng),若登錄成功,表明配置已成功。

CAS Client 客戶端使用和配置

使用 maven 創(chuàng)建兩個 web 項目 cas-client1,cas-client2。點此下載?demo?文件。

cas-client1 項目 pom.xml 配置

<dependencies>
	<dependency>
		<groupId>org.jasig.cas.client</groupId>
		<artifactId>cas-client-core</artifactId>
		<version>3.4.0</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.12</version>
	</dependency>
</dependencies>
<build>
	<plugins>
		<plugin>
			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<version>2.2</version>
			<configuration>
				<path>/</path>
				<port>8881</port>
				<httpsPort>8081</httpsPort>
				<uriEncoding>UTF-8</uriEncoding>
				<protocol>org.apache.coyote.http11.Http11NioProtocol</protocol>
				<clientAuth>false</clientAuth>
				<keystoreFile>C:\Users\fanlychie\.keystore\selfissue.jks</keystoreFile>
				<keystorePass>123654</keystorePass>
				<keystoreType>JKS</keystoreType>
				<url>http://localhost:8081/manager/html</url>
			</configuration>
		</plugin>
	</plugins>
</build>

首先必須確保項目 https 協議可以正常訪問,否則 cas server 無法認證。

選中項目 -> Run As -> Maven build… -> tomcat7:run

訪問?https://www.fanlychie.com:8081,若能訪問到,表明 Tomcat 已準備好。

cas-client1 項目 web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<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>
		<init-param>
			<param-name>casServerUrlPrefix</param-name>
			<!-- 此處配置的是 cas server 地址 -->
			<param-value>https://www.fanlychie.com:8443/cas</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>CAS Authentication Filter</filter-name>
		<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
		<init-param>
			<param-name>casServerLoginUrl</param-name>
			<!-- 此處配置的是 cas server 登錄地址 -->
			<param-value>https://www.fanlychie.com:8443/cas/login</param-value>
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<!-- 此處配置的是當前項目地址, 且必須使用 https 服務, 否則 cas server 無法認證 -->
			<param-value>https://www.fanlychie.com:8081</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CAS Authentication Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<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>
			<!-- 此處配置的是 cas server 地址 -->
			<param-value>https://www.fanlychie.com:8443/cas</param-value>
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<!-- 此處配置的是當前項目地址, 且必須使用 https 服務, 否則 cas server 無法認證 -->
			<param-value>https://www.fanlychie.com:8081</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CAS Validation Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<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>
	<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>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

以上是 cas client 標準配置,具體信息可參考?https://github.com/apereo/java-cas-client。

cas-client2 配置基本與 cas-client1 配置相同,詳情可見 demo,同時啟動這兩個項目

  • cas-client1 -?https://www.fanlychie.com:8081
  • cas-client2 -?https://www.fanlychie.com:8082

訪問其中的一個項目?https://www.fanlychie.com:8081,會自動跳到

https://www.fanlychie.com:8443/cas/login?service=https%3A%2F%2Fwww.fanlychie.com%3A8081%2F。

由于還沒有登錄過 CAS 認證系統(tǒng),CAS 認證系統(tǒng)攔截到你的訪問,進入到認證系統(tǒng)登錄界面,當登錄成功后,CAS 服務會跳轉向到你剛剛訪問的地址。

當你訪問?https://www.fanlychie.com:8082,此時是不需要登錄了的。

至此,CAS 實現 SSO 單點登錄系統(tǒng)搭建結束。

標簽: Mysql 安全 數據庫 網絡

版權申明:本站文章部分自網絡,如有侵權,請聯系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯系。

上一篇:2018年,這些UI設計趨勢正在流行

下一篇:深入理解Java中的底層阻塞原理及實現