Spring boot 整合 cas Java client

最近在做CAS相关的内容,在搭建了CAS服务器之后,再搭建一个web client。可以通过简单的web项目搭建方法搭建。CAS 官方给出了较为详细的过滤器配置方法,甚至还给出了基于web.xml配置的示例。

Spring boot这么好用,不一起搞一下说不过去。那就搞一个。(这里暂时先只给出client端的配置)

1、首先是pom.xml。



	4.0.0

	com.xx
	cas-client-sb
	0.0.1-SNAPSHOT
	war

	cas-client-sb
	Demo project for CAS Client with Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.4.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		    
		
		    org.jasig.cas.client
		    cas-client-core
		    3.5.0
		
		 
		 	org.springframework.boot
		 	spring-boot-configuration-processor
		 	true
		 
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



2、然后就是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
*/
@Configure
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 initParameters = new HashMap();
		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 initParameters = new HashMap();
		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  initParameters = new HashMap();
        initParameters.put("casServerLoginUrl", CAS_SERVER_URL_PREFIX);
        initParameters.put("serverName", SERVER_NAME);
        initParameters.put("ignorePattern", ".*");
        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 singleSignOutListenerRegistration(){  
        ServletListenerRegistrationBean registrationBean = new ServletListenerRegistrationBean();  
        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
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_第1张图片

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

你可能感兴趣的:(java)