SpringSecurity3.X--Cas client 配置

目录

SpringSecurity3.X--一个简单实现

SpringSecurity3.X--前台与后台登录认证

SpringSecurity3.X--remember-me

SpringSecurity3.X--验证码

 

最近参照springsecury3.x的官方帮助文档,对cas客户端进行了配置,确实与springsecurity2.X的配置方式有很大区别,

下面给出SpringSecurity3.X的Cas client配置。

applicationContext-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
			http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"
	default-lazy-init="true">

	<http entry-point-ref="casEntryPoint" access-decision-manager-ref="accessDecisionManager"
		access-denied-page="/access/denied.do" auto-config="false">
		<intercept-url pattern="/demo.do*" access="IS_AUTHENTICATED_REMEMBERED" />
		<intercept-url pattern="/**/*.do*" access="HODLE" />
		
		<session-management>
			<concurrency-control max-sessions="1"
				expired-url="/access/same_login.do" error-if-maximum-exceeded="false" />
		</session-management>
	
		<custom-filter position="CAS_FILTER" ref="casFilter" />
		<custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER" />
		<custom-filter ref="singleLogoutFilter" before="CAS_FILTER" />
	</http>
	
	<!-- cas 认证过滤器 -->
	<beans:bean id="casFilter"
		class="org.springframework.security.cas.web.CasAuthenticationFilter">
		<beans:property name="authenticationManager" ref="authenticationManager" />
		<beans:property name="authenticationFailureHandler"
			ref="authenticationFailureHandler" />
		<beans:property name="authenticationSuccessHandler"
			ref="authenticationSuccessHandler" />
		<beans:property name="filterProcessesUrl" value="/j_spring_cas_security_check" />
	</beans:bean>
	<!-- cas 认证失败控制器 -->
	<beans:bean id="authenticationFailureHandler"
		class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
		<beans:property name="defaultFailureUrl" value="/fail.jsp" />
	</beans:bean>
	<!-- cas 认证成功控制器 -->
	<beans:bean id="authenticationSuccessHandler"
		class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
		<beans:property name="alwaysUseDefaultTargetUrl" value="true" />
		<beans:property name="defaultTargetUrl" value="/frame.do" />
	</beans:bean>

	<!-- 注销客户端 -->
	<beans:bean id="singleLogoutFilter"
		class="org.jasig.cas.client.session.SingleSignOutFilter" />
		
	<!-- 注销服务器端 -->
	<beans:bean id="requestSingleLogoutFilter"
		class="org.springframework.security.web.authentication.logout.LogoutFilter">
		<beans:constructor-arg
			value="https://hello.cas.server:8443/BOSS_CAS_SERVER/logout" />
		<beans:constructor-arg>
			<beans:bean
				class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
		</beans:constructor-arg>
		<beans:property name="filterProcessesUrl" value="/j_spring_cas_security_logout" />
	</beans:bean>

	
	<!-- 登录成功后的返回地址 -->
	<beans:bean id="serviceProperties"
		class="org.springframework.security.cas.ServiceProperties">
		<beans:property name="service"
			value="http://hello.cas.server:8081/spring/j_spring_cas_security_check" />
		<beans:property name="sendRenew" value="false" />
	</beans:bean>
	
	<!-- CAS认证切入点,声明cas服务器端登录的地址 -->
	<beans:bean id="casEntryPoint"
		class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
		<beans:property name="loginUrl"
			value="https://hello.cas.server:8443/BOSS_CAS_SERVER/login" />
		<beans:property name="serviceProperties" ref="serviceProperties" />
	</beans:bean>

	<!-- cas认证提供器,定义客户端的验证方式 -->
	<beans:bean id="casAuthenticationProvider"
		class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
               <!-- 客户端只验证用户名是否合法 -->
                <beans:property name="authenticationUserDetailsService">
			<beans:bean
				class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
				<beans:constructor-arg ref="userService" />
			</beans:bean>
		</beans:property>

		<beans:property name="serviceProperties" ref="serviceProperties" />
		<beans:property name="ticketValidator">
			<beans:bean
				class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
				<beans:constructor-arg index="0"
					value="https://hello.cas.server:8443/BOSS_CAS_SERVER" />
			</beans:bean>
		</beans:property>
		<beans:property name="key"
			value="an_id_for_this_auth_provider_only" />
	</beans:bean>
	
        <!-- 在认证管理器中注册cas认证提供器 -->
	<authentication-manager alias="authenticationManager">
		<authentication-provider ref="casAuthenticationProvider" />
	</authentication-manager>


	<!-- 事件日志 -->
	<beans:bean id="loggerListener"
		class="org.springframework.security.authentication.event.LoggerListener" />

	<!-- 获取客户端用户 -->
	<beans:bean id="userService" class="com.piaoyi.common.security.UserService" />

	<!-- 认证拦截器,用于客户端权限验证 -->
	<beans:bean id="accessDecisionManager"
		class="org.springframework.security.access.vote.AffirmativeBased">
		<beans:property name="decisionVoters">
			<beans:list>
				<beans:bean class="org.springframework.security.access.vote.RoleVoter" />
				<beans:bean
					class="org.springframework.security.access.vote.AuthenticatedVoter" />
				<beans:bean class="com.piaoyi.common.security.DynamicRoleVoter" />
			</beans:list>
		</beans:property>
	</beans:bean>
</beans:beans>

 除了自定义了UserService(客户端用户验证)和DynamicRoleVoter(客户端权限投票器)外,其它均是springsecurity自己的组件。

关于上面两个类的实现,可以参考

SpringSecurity3.X--一个简单实现

另外,为了使注销生效,需要在web.xml中增加一个cas注销监听器,如下:

web.xml

<listener>
		<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
	</listener>
 

ok,完成。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security可以与CAS(Central Authentication Service)集成,实现单点登录(SSO)功能。CAS是一个开源的单点登录协议,它通过一个中心认证服务系统来管理用户的登录状态。在CAS系统中,有CAS Server和CAS Client两部分。 要使用Spring Security实现CAS单点登录,需要进行以下步骤: 1. 添加CAS依赖:在项目的构建文件中添加CAS相关的依赖,例如Maven的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-cas</artifactId> <version>5.4.2</version> </dependency> ``` 2. 配置CAS Server和CAS Client:在Spring Security配置文件中,配置CAS Server和CAS Client的相关信息。例如,在application.properties文件中添加以下配置: ```properties # CAS Server配置 cas.server.url.prefix=https://cas.example.com:8443/cas cas.server.login.url=https://cas.example.com:8443/cas/login cas.server.logout.url=https://cas.example.com:8443/cas/logout # CAS Client配置 cas.client.server-url-prefix=https://your-application.com cas.client.service-url=https://your-application.com/login/cas cas.client.logout-url=https://your-application.com/logout # 其他Spring Security配置 spring.security.user.name=user spring.security.user.password=password spring.security.user.roles=USER ``` 3. 配置Spring Security过滤器链:在Spring Security配置类中,配置CAS认证过滤器链。例如,在SecurityConfig.java文件中添加以下配置: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .logout() .logoutSuccessUrl("/") .and() .addFilter(casAuthenticationFilter()); } @Bean public CasAuthenticationFilter casAuthenticationFilter() throws Exception { CasAuthenticationFilter casAuthenticationFilter = new CasAuthenticationFilter(); casAuthenticationFilter.setAuthenticationManager(authenticationManager()); return casAuthenticationFilter; } // 其他配置... } ``` 4. 创建登录和注销页面:在应用程序中创建登录和注销页面,以便用户进行CAS单点登录和注销操作。 通过以上步骤,就可以使用Spring Security实现CAS单点登录了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值