`
liuqq
  • 浏览: 52069 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Spring Security 3 与 CAS单点登录配置-Client

阅读更多
如果CAS配置完成。就可以进行Client的配置了。
以下Client的配置前提是你对Spring Security有一定了解。如果不熟悉,还是希望能先读一下Spring Security相关的文章。

下面是Client的Spring Security 3的最基础的配置

1.配置<http>标签
	<http auto-config="false" entry-point-ref="casEntryPoint" servlet-api-provision="true">
		<intercept-url pattern="/manage/**" access="ROLE_ADMIN" />
		<intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN" />
		<!-- logout-success-url="/login.html" -->
		<logout logout-url="/logout.html" success-handler-ref="casLogoutSuccessHandler"/>
		<custom-filter position="FORM_LOGIN_FILTER" ref="casFilter"/>
	</http>

这里,重点是:

* 不使用http的自动配置。
* entry-point-ref="casEntryPoint"作用是认证的入口,是一个实现     AuthenticationEntryPoint接口的类。为ExceptionTranslationFilter类提供认证依据。
* <custom-filter position="FORM_LOGIN_FILTER" ref="casFilter"/> 使用自定义的Filter,放置在过滤器链的FORM_LOGIN_FILTER的位置。

    似乎casFilter与casEntryPoint的功能有重叠。
其实,casEntryPoint只是提供认证入口的作用,当没有权限,将跳转到该地址。
casFilter是处理CAS service ticket的。当无权访问时,会使用casEntryPoint提供认证入口。

2.分别配置casEntryPoint和casFilter
  配置:casEntryPoint
<beans:bean id="casEntryPoint"
	class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
	<beans:property name="loginUrl" value="https://cas.boc.com:8443/casServer/login"/>
	<beans:property name="serviceProperties" ref="serviceProperties"/>
</beans:bean>
<beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
	<beans:property name="service"
        	value="https://report.boc.com:8443/report/j_spring_cas_security_check"/>
    <beans:property name="sendRenew" value="false"/>
</beans:bean>


  配置casFilter
<beans:bean id="casFilter"
			class="org.springframework.security.cas.web.CasAuthenticationFilter">
		<beans:property name="authenticationManager" ref="authenticationManager"/>
	</beans:bean>
	
	<authentication-manager alias="authenticationManager">
		<authentication-provider ref="casAuthenticationProvider"/>
	</authentication-manager>
	
	<beans:bean id="userDetailsService" class="com.reportstart.security.service.impl.BocUserDetaislServiceImpl">
		<beans:property name="userDao">
			<beans:ref bean="userDao"/>
		</beans:property>
	</beans:bean>
   
	<beans:bean id="casAuthenticationUserDetailsService" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
		<beans:property name="userDetailsService" >
			<beans:ref local="userDetailsService"/>
		</beans:property>
	</beans:bean>
   
	<beans:bean id="casAuthenticationProvider"
			class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
		<beans:property name="authenticationUserDetailsService" ref="casAuthenticationUserDetailsService"/>
		<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://cas.boc.com:8443/casServer" />
	        </beans:bean>
		</beans:property>
		<beans:property name="key" value="an_id_for_this_auth_provider_only"/>
	</beans:bean>
</beans:bean>

如果对Spring Security比较熟悉,就不用多说什么了。
这里的"https://report.boc.com:8443/report/j_spring_cas_security_check"地址要注意,以后这个地址要注册到CAS service里,从而改变CAS的"open model".
也只有这个地址是指向Client的,其他都指向Server


最后,casLogoutSuccessHandler
如果Client要注销,需在Client先注销,之后让Server注销提供的ticket。
如果不这样,不论是只注销Client还是Server,注销后,系统仍然还是可以访问的。
(按照开始的想法,注销Client,Client应该可以主动去Server去注销ticket,但是org.springframework.security.web.authentication.logout.LogoutFilter总注销自己,而不去跟Client交互。如果你知道的话,请告知。)
CasLogoutSuccessHandler 代码
package net.viiso.security.util;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
public class CasLogoutSuccessHandler implements LogoutSuccessHandler {

	private String url = "";
	@Override
	public void onLogoutSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {

		if ("".equals(url)) {
			url = "https://cas.boc.com:8443/casServer/logout";
		}
		response.sendRedirect(url);
	}
	public void setTargetUrl(String url) {
		this.url  = url;
	}
}


启动后,对一个安全地址进行访问,会跳到CAS登录地址。
如果登录成功,会跳至访问页。
到此,简单的Client已经配置完成。
接下来,还要在Server注册Client。这个虽然不是必须,但是出于安全考虑,如果CAS服务器在外网,就非常有必要对支持的Client进行注册了,因为当你访问Client在CAS登陆成功后,CAS会给你的Client提供登录者的用户信息。如果你模拟一个Client应用,使用暴力方式,不断给CAS提供用户口令和密码,会对安全性造成破坏。
另外,也可以给CAS登录页加一个验证码。

分享到:
评论
4 楼 bastengao 2012-05-14  
http://static.springsource.org/spring-security/site/docs/3.1.x/reference/cas.html
试了这个,可以正确工作。环境 spring-securty-3.0.7 cas-client-3.1.10 cas-server-3.4.11
3 楼 liuqq 2011-04-15  
liuxiaori 写道
兄弟你好:
最近在研究cas 与spring security 3.0的集成,按照上面的配置,完成后。可以登录。但是提示:
org.springframework.security.authentication.AnonymousAuthenticationToken@6faa3d44: Principal: anonymousUser; Password: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff4c9c: RemoteIpAddress: 192.168.1.241; [color=red]SessionId: null;[/color] [color=red]Granted Authorities: ROLE_ANONYMOUS[/color]

但是在数据库中具有ROLE_ADMIN,ROLE_USER角色,以至于无法访问页面。请赐教


看一下你对接口org.springframework.security.core.userdetails.UserDetailsService的实现类里的loadUserByUsername方法,用户是否取得到了角色信息(子应用服务器)
2 楼 liuxiaori 2011-04-02  
兄弟你好:
最近在研究cas 与spring security 3.0的集成,按照上面的配置,完成后。可以登录。但是提示:
org.springframework.security.authentication.AnonymousAuthenticationToken@6faa3d44: Principal: anonymousUser; Password: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff4c9c: RemoteIpAddress: 192.168.1.241; [color=red]SessionId: null;[/color] [color=red]Granted Authorities: ROLE_ANONYMOUS[/color]

但是在数据库中具有ROLE_ADMIN,ROLE_USER角色,以至于无法访问页面。请赐教
1 楼 surpass_li 2010-08-25  
CAS3解决了这个问题吧

相关推荐

    spring boot整合CAS Client实现单点登陆验证的示例

    本篇文章主要介绍了spring boot整合CAS Client实现单点登陆验证的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    spring-security-cas-client-2.0.4.jar

    spring-security-cas-client-2.0.4.jar

    cas-server-client-springsecurity.zip

    cas 5.3 overlay 整合MySQL 整合普通客户端,整合springsecurity客户端

    springboot+security+cas集成demo

    springboot+security+cas集成demo。。

    spring boot 实现SSO单点登陆

    spring boot整合spring security 实现SSO单点登陆 完整DEMO. 1、配置本地hosts 127.0.0.1 sso-login 127.0.0.1 sso-resource 127.0.0.1 sso-tmall 127.0.0.1 sso-taobao windows系统的路径在C:\WINDOWS\system...

    spring security 4 需要 jar 包

    cas-client-core-3.1.12-sources.jar cas-client-core-3.1.12.jar ehcache-1.6.2.jar guice-2.0-javadoc.jar guice-2.0-sources.jar guice-2.0-src.jar guice-2.0.jar httpclient-4.1.1.jar jsr250-api-1.0.jar ...

    spring-security-3.0.2 jar

    spring-security-cas-client-3.0.2.RELEASE.jar spring-security-config-3.0.2.RELEASE.jar spring-security-core-3.0.2.RELEASE.jar spring-security-taglibs-3.0.2.RELEASE.jar spring-security-web-3.0.2.RELEASE...

    CAS单点登录框架整合Spring Security

    CAS 包含两个部分: CAS Server 和 CAS Client ...CAS Client:就是开发过程中的web层, 负责处理对客户端受保护资源的访问请求,需要登录时,重定向到 CAS Server。不需要对这个部分进行过多编码,进行简单配置即可。

    cas 配置client 1.0 &2.0 及proxy DEMO 说明

    3 cas client 1.0配置说明 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...

    Spring Security 中文教程.pdf

    CAS - spring-security-cas-client.jar 1.4.1.7. OpenID - spring-security-openid.jar 1.4.2. 获得源代码 2. Security命名空间配置 2.1. 介绍 2.1.1. 命名空间的设计 2.2. 开始使用安全命名空间配置 ...

    Spring Security-3.0.1中文官方文档(翻译版)

    CAS - spring-security-cas-client.jar 1.4.1.7. OpenID - spring-security-openid.jar 1.4.2. 获得源代码 2. Security 命名空间配置 2.1. 介绍 2.1.1. 命名空间的设计 2.2. 开始使用安全命名空间配置 ...

    java-spring-security-cas-client-demo:由 Spring Security 的 CAS 客户端保护的演示 webapp

    受 Spring Security CAS 客户端保护的 Java webapp ( ) Maven 演示使用来自 Spring Security 项目 (v3.2.5) 的 CAS 客户端来保护 Web 应用程序。 使用mvn clean compile jetty:run在上启动 webapp。 url '...

    SpringSecurity 3.0.1.RELEASE.CHM

    CAS - spring-security-cas-client.jar 1.4.1.7. OpenID - spring-security-openid.jar 1.4.2. 获得源代码 2. Security命名空间配置 2.1. 介绍 2.1.1. 命名空间的设计 2.2. 开始使用安全命名空间配置 2.2.1....

    spring security3.2.0

    JA-SIG Central Authentication Service (CAS,这是一个流行的开源单点登录系统) Transparent authentication context propagation for Remote Method Invocation and HttpInvoker (一个Spring远程调用协议)

    J2EE项目开发常用Jar包源代码-src.zip

    spring-security-cas-client-3.0.3.RELEASE-sources.jar spring-security-config-3.0.3.RELEASE-sources.jar spring-security-core-3.0.3.RELEASE-sources.jar spring-security-ldap-3.0.3.RELEASE-sources.jar ...

    前后端分离集成cas

    springboot(springboot + shiro + oracle) + vue 集成cas

    xmljava系统源码-custom-cas:最佳实践-使用Maven3WAROverlay方法在本地设置CAS

    Security(client-spring-security) 三种客户端。 使用单机完成了三个客户端的 SSO 。 使用多机完成了三个客户端的 SSO 。 CAS without SSL 。 整合遗留系统。 相关软件 cas-server-3.4.11 cas-client-core 3.2.0 ...

    javaweb项目常用jar包

    cas-client-core-3.3.3.jar cglib-2.2.2.jar commons-beanutils-1.8.0.jar commons-cli-1.2.jar commons-codec-1.9.jar commons-collections-3.2.1.jar commons-dbcp-1.4.jar commons-fileupload-1.3.1.jar ...

    cas server 4.2.7 环境搭建maven

    cas server overlay 搭建方式,和spring security 加密算法集成

    Spring主流jar包大全

    cas-client-core-3.2.1.jar cglib-3.1.jar ckfinder-2.3.jar ckfinderplugin-fileeditor-2.3.jar ckfinderplugin-imageresize-2.3.jar classmate-1.1.0.jar commons-beanutils-1.9.1.jar commons-codec-1.9.jar ...

Global site tag (gtag.js) - Google Analytics