1. 程式人生 > >Spring boot 整合 cas Java client

Spring boot 整合 cas Java client

最近在做CAS相關的內容,在搭建了CAS伺服器之後,再搭建一個web client。可以通過簡單的web專案搭建方法搭建。CAS 官方給出了較為詳細的過濾器配置方法,甚至還給出了基於web.xml配置的示例

Spring boot這麼好用,不一起搞一下說不過去。那就搞一個。(這裡暫時先只給出client端的配置)

1、首先是pom.xml。

<?xml version="1.0" encoding="UTF-8"?>
<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.xx</groupId>
	<artifactId>cas-client-sb</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>cas-client-sb</name>
	<description>Demo project for CAS Client with Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		    <!-- https://mvnrepository.com/artifact/org.jasig.cas.client/cas-client-core -->
		<dependency>
		    <groupId>org.jasig.cas.client</groupId>
		    <artifactId>cas-client-core</artifactId>
		    <version>3.5.0</version>
		</dependency>
	        <!-- Support for SAML functionality is provided by this dependency -->
	        <!-- https://mvnrepository.com/artifact/org.jasig.cas.client/cas-client-support-saml -->
		<dependency>
		    <groupId>org.jasig.cas.client</groupId>
		    <artifactId>cas-client-support-saml</artifactId>
		    <version>3.5.0</version>
		</dependency>
			<!-- https://mvnrepository.com/artifact/org.jasig.cas.client/cas-client-integration-tomcat-v85 -->
		 <dependency>
		    <groupId>org.jasig.cas.client</groupId>
		    <artifactId>cas-client-integration-tomcat-v85</artifactId>
		    <version>3.5.0</version>
		</dependency>
				<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
		<dependency>
		    <groupId>org.springframework.security</groupId>
		    <artifactId>spring-security-core</artifactId>
		    <version>5.0.8.RELEASE</version>
		</dependency>
				<!-- https://mvnrepository.com/artifact/net.unicon.cas/cas-client-autoconfig-support -->
		<dependency>
		    <groupId>net.unicon.cas</groupId>
		    <artifactId>cas-client-autoconfig-support</artifactId>
		    <version>1.7.0-GA</version>
		</dependency>
		 <dependency>
		 	<groupId>org.springframework.boot</groupId>
		 	<artifactId>spring-boot-configuration-processor</artifactId>
		 	<optional>true</optional>
		 </dependency>
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2、然後是application.propertites。配置自己客戶端應用的地址和伺服器的地址。

#tomcat started on port(s) : 8080 (http)
server.port:8080

#伺服器的地址
cas.server-url-prefix=http://localhost:8900/cas
cas.server-login-url=http://localhost:8900/cas/login

#client 部署的地址
cas.client-host-url=http://localhost:8080
 
cas.validation-type=CAS

3、然後就是CasConfig.java ,這裡是重要的地方:過濾器的配置,起的作用與在web.xml配置一樣。

package com.xx.config;

import java.util.EventListener;
import java.util.HashMap;
import java.util.Map;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
* @author yuanyao
* @date   2018年9月13日 下午3:47:18
*/
public class CasConfig {
	//url的字首
    private static final String CAS_SERVER_URL_PREFIX = "http://localhost:8900";

    //本機的名稱
    private static final String SERVER_NAME = "http://localhost:8080";
    
    /**
     * 登入過濾器
     * @return
     */
	@Bean
	public FilterRegistrationBean filterSingleRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new SingleSignOutFilter());
		//設定匹配路徑
		registration.addUrlPatterns("/*");
		Map<String,String> initParameters = new HashMap<String,String>();
		initParameters.put("casServerUrlPrefix",CAS_SERVER_URL_PREFIX);
		//設定載入順序
		registration.setOrder(1);
		return registration;
	}
	
	/**
	 * 過濾驗證器
	 * @return
	 */
	@Bean
	public FilterRegistrationBean filterValidationRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
		//設定匹配路徑
		registration.addUrlPatterns("/*");
		Map<String,String> initParameters = new HashMap<String,String>();
		initParameters.put("casServerUrlPrefix", CAS_SERVER_URL_PREFIX);
		initParameters.put("serverName", SERVER_NAME);
		initParameters.put("useSession", "true");
		registration.setInitParameters(initParameters);
		//設定載入順序
		registration.setOrder(1);
		return registration;
	}
	
	/**
	 * 	授權過濾器
	 * @return
	 */
	@Bean
	public FilterRegistrationBean filterAuthenticationRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AuthenticationFilter());
        // 設定匹配的路徑
        registration.addUrlPatterns("/*");
        Map<String,String>  initParameters = new HashMap<String, String>();
        initParameters.put("casServerLoginUrl", CAS_SERVER_URL_PREFIX);
        initParameters.put("serverName", SERVER_NAME);
        initParameters.put("ignorePattern", ".*");
        //表示過濾所有
        //initParameters.put("ignoreUrlPatternType", "com.yellowcong.cas.auth.SimpleUrlPatternMatcherStrategy");

        registration.setInitParameters(initParameters);
        // 設定載入的順序
        registration.setOrder(1);
        return registration;
	}
	/**
     * wraper過濾器
     * @return
     */
    @Bean
    public FilterRegistrationBean filterWrapperRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new HttpServletRequestWrapperFilter());
        // 設定匹配的路徑
        registration.addUrlPatterns("/*");
        // 設定載入的順序
        registration.setOrder(1);
        return registration;
    }

    /**
     * 新增監聽器
     * @return
     */
    @Bean  
    public ServletListenerRegistrationBean<EventListener> singleSignOutListenerRegistration(){  
        ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<EventListener>();  
        registrationBean.setListener(new SingleSignOutHttpSessionListener());  
        registrationBean.setOrder(1);  
        return registrationBean;  
    }  


}

4、Springboot啟動類中啟用cas,類為:CasClientSbApplication.java

package com.xx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.logging.ClasspathLoggingApplicationListener;
import org.springframework.web.bind.annotation.GetMapping;

import net.unicon.cas.client.configuration.EnableCasClient;

@SpringBootApplication
@EnableCasClient
public class CasClientSbApplication {

	public static void main(String[] args) {
		SpringApplication.run(CasClientSbApplication.class, args);
	}
}

完了以後啟動client端與server端,然後在瀏覽器輸入http://localhost:8080/index.jsp,即會先跳轉到如下頁面

然後輸入使用者名稱和密碼,認證成功即可跳轉回請求index.html頁面