Spring Security整合CAS的示例代碼
這里使用的是spring-security和原生的jasig cas包來進(jìn)行整合,為什么沒有直接使用spring提供的spring-security-cas,后面會(huì)進(jìn)行解釋。
配置
web.xml
<filter> <filter-name>casFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>casFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class> </listener>
applicationContext-security.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="casFilterChain" class="org.springframework.security.web.FilterChainProxy">
<constructor-arg>
<util:list>
<security:filter-chain pattern="/**" filters="singleSignOutFilter, cas20ProxyReceivingTicketValidationFilter, authenticationFilter, httpServletRequestWrapperFilter, assertionThreadLocalFilter"/>
</util:list>
</constructor-arg>
</bean>
<bean id="singleSignOutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>
<bean id="cas20ProxyReceivingTicketValidationFilter"
class="org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter">
<property name="serverName" value="${client.url}"/>
<property name="ticketValidator" ref="cas20ServiceTicketValidator"/>
</bean>
<bean id="cas20ServiceTicketValidator" class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
<constructor-arg value="${cas.url}"/>
<property name="renew" value="false"/>
</bean>
<bean id="authenticationFilter" class="org.jasig.cas.client.authentication.AuthenticationFilter">
<property name="renew" value="false"/>
<property name="casServerLoginUrl" value="${cas.url}"/>
<property name="serverName" value="${client.url}"/>
</bean>
<bean id="httpServletRequestWrapperFilter" class="org.jasig.cas.client.util.HttpServletRequestWrapperFilter"/>
<bean id="assertionThreadLocalFilter" class="org.jasig.cas.client.util.AssertionThreadLocalFilter"/>
</beans>
properties
#CAS服務(wù)地址 cas.url=https://cas.example.com:8443 #CAS客戶端地址,就是本應(yīng)用的地址 client.url=http://localhost:8080
分析
在applicationContext-security.xml中的security filter chain中,我們使用了5個(gè)filter,分別是:singleSignOutFilter、cas20ProxyReceivingTicketValidationFilter、authenticationFilter、httpServletRequestWrapperFilter、assertionThreadLocalFilter。
為什么不用spring-security-cas
spring-security-cas
在spring-security-cas中負(fù)責(zé)ticket validator filter使用的是org.springframework.security.cas.authentication.CasAuthenticationProvider。
private CasAuthenticationToken authenticateNow(final Authentication authentication) throws AuthenticationException {
try {
final Assertion assertion = this.ticketValidator.validate(authentication.getCredentials().toString(), getServiceUrl(authentication));
...
在構(gòu)建validator的validator方法的第二個(gè)參數(shù)時(shí)
private String getServiceUrl(Authentication authentication) {
String serviceUrl;
if(authentication.getDetails() instanceof ServiceAuthenticationDetails) {
serviceUrl = ((ServiceAuthenticationDetails)authentication.getDetails()).getServiceUrl();
}else if(serviceProperties == null){
throw new IllegalStateException("serviceProperties cannot be null unless Authentication.getDetails() implements ServiceAuthenticationDetails.");
}else if(serviceProperties.getService() == null){
throw new IllegalStateException("serviceProperties.getService() cannot be null unless Authentication.getDetails() implements ServiceAuthenticationDetails.");
}else {
serviceUrl = serviceProperties.getService();
}
if(logger.isDebugEnabled()) {
logger.debug("serviceUrl = "+serviceUrl);
}
return serviceUrl;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
HelloSpringMVC配置版實(shí)現(xiàn)步驟解析
這篇文章主要介紹了HelloSpringMVC配置版實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
舉例講解Java編程中this關(guān)鍵字與super關(guān)鍵字的用法
這篇文章主要介紹了Java編程中this關(guān)鍵字與super關(guān)鍵字的用法示例,super是this的父輩,在繼承過程中兩個(gè)關(guān)鍵字經(jīng)常被用到,需要的朋友可以參考下2016-03-03
SpringBoot自定義注解及AOP的開發(fā)和使用詳解
在公司項(xiàng)目中,如果需要做一些公共的功能,如日志等,最好的方式是使用自定義注解,自定義注解可以實(shí)現(xiàn)我們對想要添加日志的方法上添加,這篇文章基于日志功能來講講自定義注解應(yīng)該如何開發(fā)和使用,需要的朋友可以參考下2023-08-08
Java Applet查找素?cái)?shù)小程序代碼實(shí)例
這篇文章主要介紹了Java Applet查找素?cái)?shù)小程序代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
java實(shí)現(xiàn)壓縮字符串和java字符串過濾
這篇文章主要介紹了java實(shí)現(xiàn)壓縮字符串和java字符串過濾,需要的朋友可以參考下2014-04-04
Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù)
這篇文章主要介紹了Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù),本文給出了提出問題、分析問題、解決問題三個(gè)步驟,需要的朋友可以參考下2015-04-04
SpringCloud實(shí)現(xiàn)基于RabbitMQ消息隊(duì)列的詳細(xì)步驟
在Spring Cloud框架中,我們可以利用RabbitMQ實(shí)現(xiàn)強(qiáng)大而可靠的消息隊(duì)列系統(tǒng),本篇將詳細(xì)介紹如何在Spring Cloud項(xiàng)目中集成RabbitMQ,并創(chuàng)建一個(gè)簡單的消息隊(duì)列,感興趣的朋友一起看看吧2024-03-03

