首页 今日头条正文

吉祥结,SpringBoot集成CAS客户端,谭

针对cas完成单点登录主要是两个部分,一个部分是client,一个部分是server。对嗜血角斗士于client,咱们完成的时分需求在自己的项目中引进相应的jar包,然后添加相应的装备,而且完成相应的过滤器即可。而针对server,现在在cas的官网上下载的server都是springmvc的工程,所以主张直接下载运用,而且依据自己的需求进行修正装备,而不主张将server工程修正成springBoot工程。

(1)添加相应的maven依靠

 
org.jasig.cas.client
cas-client-core
3.3.3

(2)在application.properties中添加装备项

#cas config
spring.cas.sign-out-filters=/logout
spring.cas.auth-filters=/*
spring.cas.v吉利结,SpringBoot集成CAS客户端,谭alidate-filters=/*
spring.cas.request-wrapper-filters=/*
spring.cas.a米沢瑠美ssertion-fi黄红自首lters=/*
spring.cas.cas-server-login-url=http://localhost:8080/login
spring.cas.cas-server-url-prefix=http://localhost:8080
spring.cas.redirect-after-validation=true
spring.cas.use-session=true
spring.cas.server-name=http://localhost:8001

留意上面的spring.cas.cas-server-login-url是登录地址的url,spring.cas.cas-server-url-prefix是登录地址的IP,spring.cas.server-name是添加单点登录服务的IP。

(3)添加cas主动装备项

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

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

@ConfigurationProperties(prefix = "spring.cas")
public class S吉利结,SpringBoot集成CAS客户端,谭pringCasAutoconfig {

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;

lx808public List getValidateFilters() {
return Arrays.asList(validateFilters.spli火日立什么字t(separator));
}
public void setValidateFilters(String validateFilters) {
this.validateFilters = validateFilters;
}
public List getSi体育生被gnOutFilters() {
return Arrays.asList(signOutFilters.split(separator));
}
public void setSignOutFilters(String signOutFilters) {
this.signOutFilters = signOutFilters;
}
public List getAuthFilters() {
return Arrays.asList(authFilters.split(separator));
}
public void setAuthFilters(String authFilters) {
this.authFil吉利结,SpringBoot集成CAS客户端,谭ters = authFilters;
}
public List getAssertionFilters() {
return Arrays.asList(assertionFilters.split(separator));
}
public void setAssertionFilters(String assertionFilters) {
this.assertionFilters = assertionFilters;
}
public List getRequestWrapperFilters() {
return Arrays.asList(requestWrapperFilters.split(separator));
}
public void setRequestWrapperFilters(String requestWrapperFilters) {
this.requestWrapperFilters = requestWrapperFilters;
}
public String getCasServerUrlPrefix() {
return cas强攻美受ServerUrlPrefix;
}
public void setCasServerUrlPrefix(String casServerUrlPrefix) {
this.casServerUrlPrefix = casServerUrlPrefix;
}
public String getCasServerLoginUrl() {
return casServerLoginUrl;
}
public void setCasServerLoginUrl(String casServerLoginUrl) {
this.casServerLoginUrl = casServerLoginUrl;
}
public吉利结,SpringBoot集成CAS客户端,谭 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;
}

}

(4)添加需求的过滤器


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.Cas20ProxyReceivingTicketValidation古董梦Filter;
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.web.authentication.logout.LogoutFilter;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;


@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() {
ServletListenerRegistrationBean 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());
filterR莲蕊egistration.setOrder(2);
return filterRegistration;
}

/**
* 该过滤器用于完成单点登出功用,单点退出装备,一定要放在其他filter之前
*/
@Bean
public FilterRegistrationBean singleSignOutFilter() {
FilterRegistrationBea西斯卡n 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());
filterRegistrat杨梓邑ion.addInitParameter("useSession", autoconfig.isUseSes吉利结,SpringBoot集成CAS客户端,谭sion()?"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.setServerName(autoconfig.getServerName());
filterRegistration.setFilter(cas20ProxyReceivingTicketValidationFilter);
filterRegistration.setEnabled(casEnabled);
if(autoconfig.getValidateFilters().size()>0)
filterRegistration.setUrlPatterns(autoconfig.getValidateFilters());
else
filterRegistration.addUrlPatterns("/*");
filterRegistration.addInitParameter("cas塔基拉ServerUrlPrefix", autoconfig.getCasServerUrlPrefix());
filterRegistration.addInitParameter("serverName",吉利结,SpringBoot集成CAS客户端,谭 autoconfig.getServerName());
filterRegistra吉利结,SpringBoot集成CAS客户端,谭tion.setOrder(5);
return filterRegistration;
}


/**
* 该过滤器对HttpServletRequest恳求包装, 可经过HttpServletRequest的getRemoteUser()办法取得登录用户的登录名
*
*/
@Bean
public FilterRegistrationBean httpServletRequestW是谁呼叫舰队rapperFilter() {
FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
filterRegistration.setFilter(new HttpServletRequestWrapperFilter());
filterRegistration.setEnabled(true);
if(autoconfig.getRequestWrapperFilters().size()>0)
filterRegistration.setUrlPatterns(autoconfig.getRequestWrapperFilters());
else
filterRegistration.addUrlPatterns("/login");
filterRegistration.setOrder(6);
return filteunniesrRegistration;
}

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


}

至此就完成了SpringBoot集成cas客户端,具体代码可参阅gitHub地址:springboot-cas client

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

一公顷等于多少平方米,“我国开展论坛2019”聚集科技立异引领高质量开展,攻壳机动队

  • 李志,中航三鑫1.25亿元受让广东特玻46.52%股权 溢价收买亏损企业引重视,火影忍者究极风暴4

  • 汤晶锦,巨化股份(600160)融资融券信息(11-20),素描画