您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何在Spring Security中使用CAS,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
配置
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服務地址 cas.url=https://cas.example.com:8443 #CAS客戶端地址,就是本應用的地址 client.url=http://localhost:8080
分析
在applicationContext-security.xml中的security filter chain中,我們使用了5個filter,分別是:singleSignOutFilter、cas20ProxyReceivingTicketValidationFilter、authenticationFilter、httpServletRequestWrapperFilter、assertionThreadLocalFilter。
為什么不用spring-security-cas
spring-security-cas
在spring-security-cas中負責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)); ...
在構建validator的validator方法的第二個參數時
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; }
關于如何在Spring Security中使用CAS就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。