spring boot整合CAS配置详解

在下不才,以下是我花了好几天的时间才整合出来的在spring boot里面的CAS配置整合

为了帮助没搞定的人,毕竟自己踩了很多坑,一步一步爬过来的,有什么不足之处可以给建议  谢谢(小部分代码是整合他人的)

1.不多废话,直接上最重要的代码,以下代码整合cas的重要过程

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.AssertionThreadLocalFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import org.jasig.cas.client.validation.Cas20ServiceTicketValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.cas.ServiceProperties;
import org.springframework.security.cas.authentication.CasAuthenticationProvider;
import org.springframework.security.cas.userdetails.GrantedAuthorityFromAssertionAttributesUserDetailsService;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; 

import java.util.List; 

@Configuration
public class CasConfig { 

  @Autowired
  SpringCasAutoconfig autoconfig; 

  private static boolean casEnabled = true; 

  public CasConfig() {
  } 

  @Bean
  public SpringCasAutoconfig getSpringCasAutoconfig(){
    return new SpringCasAutoconfig();
  } 

  /**
   * 用于实现单点登出功能
   */
  @Bean
  public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> singleSignOutHttpSessionListener() {
    ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> listener = new ServletListenerRegistrationBean<>();
    listener.setEnabled(casEnabled);
    listener.setListener(new SingleSignOutHttpSessionListener());
    listener.setOrder(1);
    return listener;
  } 

  /**
   * 该过滤器用于实现单点登出功能,单点退出配置,一定要放在其他filter之前
   */
  @Bean
  public FilterRegistrationBean logOutFilter() {
    FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
    LogoutFilter logoutFilter = new LogoutFilter(autoconfig.getCasServerUrlPrefix() + "/logout?service=" + autoconfig.getServerName(),new SecurityContextLogoutHandler());
    filterRegistration.setFilter(logoutFilter);
    filterRegistration.setEnabled(casEnabled);
    if(autoconfig.getSignOutFilters().size()>0)
      filterRegistration.setUrlPatterns(autoconfig.getSignOutFilters());
    else
      filterRegistration.addUrlPatterns("/logout");
    filterRegistration.addInitParameter("casServerUrlPrefix", autoconfig.getCasServerUrlPrefix());
    filterRegistration.addInitParameter("serverName", autoconfig.getServerName());
    filterRegistration.setOrder(2);
    return filterRegistration;
  } 

  /**
   * 该过滤器用于实现单点登出功能,单点退出配置,一定要放在其他filter之前
   */
  @Bean
  public FilterRegistrationBean singleSignOutFilter() {
    FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
    filterRegistration.setFilter(new SingleSignOutFilter());
    filterRegistration.setEnabled(casEnabled);
    if(autoconfig.getSignOutFilters().size()>0)
      filterRegistration.setUrlPatterns(autoconfig.getSignOutFilters());
    else
      filterRegistration.addUrlPatterns("/*");
    filterRegistration.addInitParameter("casServerUrlPrefix", autoconfig.getCasServerUrlPrefix());
    filterRegistration.addInitParameter("serverName", autoconfig.getServerName());
    filterRegistration.setOrder(3);
    return filterRegistration;
  } 

  /**
   * 该过滤器负责用户的认证工作
   */
  @Bean
  public FilterRegistrationBean authenticationFilter() {
    FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
    filterRegistration.setFilter(new AuthenticationFilter());
    filterRegistration.setEnabled(casEnabled);
    if(autoconfig.getAuthFilters().size()>0)
      filterRegistration.setUrlPatterns(autoconfig.getAuthFilters());
    else
      filterRegistration.addUrlPatterns("/*");
    //casServerLoginUrl:cas服务的登陆url
    filterRegistration.addInitParameter("casServerLoginUrl", autoconfig.getCasServerLoginUrl());
    //本项目登录ip+port
    filterRegistration.addInitParameter("serverName", autoconfig.getServerName());
    filterRegistration.addInitParameter("useSession", autoconfig.isUseSession()?"true":"false");
    filterRegistration.addInitParameter("redirectAfterValidation", autoconfig.isRedirectAfterValidation()?"true":"false");
    filterRegistration.setOrder(4);
    return filterRegistration;
  } 

  /**
   * 该过滤器负责对Ticket的校验工作
   */
  @Bean
  public FilterRegistrationBean cas20ProxyReceivingTicketValidationFilter() {
    FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
    Cas20ProxyReceivingTicketValidationFilter cas20ProxyReceivingTicketValidationFilter = new Cas20ProxyReceivingTicketValidationFilter();
    //cas20ProxyReceivingTicketValidationFilter.setTicketValidator(cas20ServiceTicketValidator());
    cas20ProxyReceivingTicketValidationFilter.setServerName(autoconfig.getServerName());
    filterRegistration.setFilter(cas20ProxyReceivingTicketValidationFilter);
    filterRegistration.setEnabled(casEnabled);
    if(autoconfig.getValidateFilters().size()>0)
      filterRegistration.setUrlPatterns(autoconfig.getValidateFilters());
    else
      filterRegistration.addUrlPatterns("/*");
    filterRegistration.addInitParameter("casServerUrlPrefix", autoconfig.getCasServerUrlPrefix());
    filterRegistration.addInitParameter("serverName", autoconfig.getServerName());
    filterRegistration.setOrder(5);
    return filterRegistration;
  } 

  /**
   * 该过滤器对HttpServletRequest请求包装, 可通过HttpServletRequest的getRemoteUser()方法获得登录用户的登录名
   *
   */
  @Bean
  public FilterRegistrationBean httpServletRequestWrapperFilter() {
    FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
    filterRegistration.setFilter(new HttpServletRequestWrapperFilter());
    filterRegistration.setEnabled(true);
    if(autoconfig.getRequestWrapperFilters().size()>0)
      filterRegistration.setUrlPatterns(autoconfig.getRequestWrapperFilters());
    else
      filterRegistration.addUrlPatterns("/*");
    filterRegistration.setOrder(6);
    return filterRegistration;
  } 

  /**
   * 该过滤器使得可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。
   比如AssertionHolder.getAssertion().getPrincipal().getName()。
   这个类把Assertion信息放在ThreadLocal变量中,这样应用程序不在web层也能够获取到当前登录信息
   */
  @Bean
  public FilterRegistrationBean assertionThreadLocalFilter() {
    FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
    filterRegistration.setFilter(new AssertionThreadLocalFilter());
    filterRegistration.setEnabled(true);
    if(autoconfig.getAssertionFilters().size()>0)
      filterRegistration.setUrlPatterns(autoconfig.getAssertionFilters());
    else
      filterRegistration.addUrlPatterns("/*");
    filterRegistration.setOrder(7);
    return filterRegistration;
  }
} 

2.为了让你们更省力且直接的看到效果,我把相关配置也贴出来

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; 

import java.util.Arrays;
import java.util.List; 

@ConfigurationProperties(prefix = "spring.cas")
public class SpringCasAutoconfig { 

  static final String separator = ","; 

  private String validateFilters;
  private String signOutFilters;
  private String authFilters;
  private String assertionFilters;
  private String requestWrapperFilters; 

  private String casServerUrlPrefix;
  private String casServerLoginUrl;
  private String serverName;
  private boolean useSession = true;
  private boolean redirectAfterValidation = true; 

  public List<String> getValidateFilters() {
    return Arrays.asList(validateFilters.split(separator));
  }
  public void setValidateFilters(String validateFilters) {
    this.validateFilters = validateFilters;
  }
  public List<String> getSignOutFilters() {
    return Arrays.asList(signOutFilters.split(separator));
  }
  public void setSignOutFilters(String signOutFilters) {
    this.signOutFilters = signOutFilters;
  }
  public List<String> getAuthFilters() {
    return Arrays.asList(authFilters.split(separator));
  }
  public void setAuthFilters(String authFilters) {
    this.authFilters = authFilters;
  }
  public List<String> getAssertionFilters() {
    return Arrays.asList(assertionFilters.split(separator));
  }
  public void setAssertionFilters(String assertionFilters) {
    this.assertionFilters = assertionFilters;
  }
  public List<String> getRequestWrapperFilters() {
    return Arrays.asList(requestWrapperFilters.split(separator));
  }
  public void setRequestWrapperFilters(String requestWrapperFilters) {
    this.requestWrapperFilters = requestWrapperFilters;
  }
  public String getCasServerUrlPrefix() {
    return casServerUrlPrefix;
  }
  public void setCasServerUrlPrefix(String casServerUrlPrefix) {
    this.casServerUrlPrefix = casServerUrlPrefix;
  }
  public String getCasServerLoginUrl() {
    return casServerLoginUrl;
  }
  public void setCasServerLoginUrl(String casServerLoginUrl) {
    this.casServerLoginUrl = casServerLoginUrl;
  }
  public String getServerName() {
    return serverName;
  }
  public void setServerName(String serverName) {
    this.serverName = serverName;
  }
  public boolean isRedirectAfterValidation() {
    return redirectAfterValidation;
  }
  public void setRedirectAfterValidation(boolean redirectAfterValidation) {
    this.redirectAfterValidation = redirectAfterValidation;
  }
  public boolean isUseSession() {
    return useSession;
  }
  public void setUseSession(boolean useSession) {
    this.useSession = useSession;
  } 

}

3.配置文件  dev.yml

  #cas client config
  spring:cas:
  sign-out-filters: /logout
  auth-filters: /*
  validate-filters: /*
  request-wrapper-filters: /*
  assertion-filters: /*
  cas-server-login-url: cas登录url
  cas-server-url-prefix:cas登录域名
  redirect-after-validation: true
  use-session: true
  server-name: http://localhost:8080

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 详解spring boot配置单点登录

    概述 企业内部一般都有一套单点登录系统(常用的实现有apereo cas),所有的内部系统的登录认证都对接它.本文介绍spring boot的程序如何对接CAS服务. 常用的安全框架有spring security和apache shiro.shiro的配置和使用相对简单,本文使用shrio对接CAS服务. 配置 新增依赖 pom.xml新增: <properties> <shiro.version>1.2.4</shiro.version> </properti

  • 基于SpringMVC+Bootstrap+DataTables实现表格服务端分页、模糊查询

    前言 基于SpringMVC+Bootstrap+DataTables实现数据表格服务端分页.模糊查询(非DataTables Search),页面异步刷新. 说明:sp:message标签是使用了SpringMVC国际化 效果 DataTable表格 关键字查询 自定义关键字查询,非DataTable Search 代码 HTML代码 查询条件代码 <!-- 查询.添加.批量删除.导出.刷新 --> <div class="row-fluid"> <di

  • springboot实现拦截器之验证登录示例

    整理文档,搜刮出一个springboot实现拦截器之验证登录示例,稍微整理精简一下做下分享. 添加jar包,这个jar包不是必须的,只是在拦截器里用到了,如果不用的话,完全可以不引入 <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dep

  • 详解eclipse下创建第一个spring boot项目

    spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者.也就是说,Spring Boot是为了简化Spring开发而生,主要思想是降低spring的入门,使得新手可以以最快的速度让程序在spring框架下跑起来. 今天我们就来创建

  • Spring Boot 启动端口如何启动

    Spring Boot 启动端口 Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动.其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat).当然你也可以将项目打包成war包,放到独立的web容器中(Tomcat.weblogic等等),当然在此之前你要对程序入口做简单调整. spring boot是个好东西,可以不用容器直接在main方法中启动,而且无需配置文件,方便快速搭建环境.可是当我们要同时启动2个springb

  • springboot与mybatis整合实例详解(完美融合)

    简介 从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boot 的作用在于创建和启动新的基于 Spring 框架的项目.它的目的是帮助开发人员很容易的创建出独立运行和产品级别的基于 Spring 框架的应用.Spring Boot 会选择最适合的 Spring 子项目和第三方开源库进行整合.大部分 Spring Boot 应用只需要非常少的配置就可以快速运行起来. Spring Boot 包含的特性如下: 创建可以独立运行的 Spring 应用. 直接嵌入 Tomc

  • spring boot实现过滤器和拦截器demo

    整理文档,搜刮出一个spring boot实现过滤器和拦截器demo ,稍微整理精简一下做下分享. 拦截器定义: @WebServlet public class ActionInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Ex

  • SpringBoot获取yml和properties配置文件的内容

    (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @ConfigurationProperties 注解 --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor --> <dependency> <groupId>org.springframework.boot</groupId>

  • 深入理解Spring Boot的日志管理

    前言 Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持, 如:Java Util Logging,Log4J, Log4J2和Logback.每种Logger都可以通过配置使用控制台或者文件输出日志内容. 日志输出格式 2016-08-19 10:22:04.233 INFO 7368 --- [ main] com.juzi.AsyncTest : Started AsyncTest in 10.084 seconds (JVM r

  • 在Spring Boot中如何使用log4j记录日志

    前言 Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J, Log4J2和Logback.每种Logger都可以通过配置使用控制台或者文件输出日志内容.本文主要介绍了在Spring Boot中如何使用log4j记录日志,感兴趣的来一起学习学习. 引入log4j依赖 在创建Spring Boot工程时,我们引入了spring-boot-starter,其中包含了spring-boot-s

随机推荐