欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot 整合 cas Java client

程序员文章站 2022-05-06 07:54:03
...

最近在做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 Barry
* @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,即会先跳转到如下页面

Spring boot 整合 cas Java client

然后输入用户名和密码,认证成功即可跳转回请求index.html页面

上一篇: EL表达式

下一篇: 线程池