`
hyperprice
  • 浏览: 49242 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring-security3 配置和使用.

阅读更多
刚才想发到论坛来的.. 结果使用的是chrome打完字没注意就点了发布.. T_T.
   
最近项目中要使用到spring-security,闲来没事就研究了下。发现入门挺简单的,在这里把自己的心得发下,希望对没有接触过想接触的朋友有帮助。

1、在spring-security官网下载最新jar然后拷贝jar到项目的lib下。
2、在classpath下添加security配置文件,例如applicationContext-security.xml.网上现在大多都是2.0的schema. 要根据自己使用的版本而定.下面是3.0的schema.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/security
	http://www.springframework.org/schema/security/spring-security-3.0.xsd">

</beans:beans>

3、然后在web.xml中添加配置,内容如下:
<!-- spring security  -->
        <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/applicationContext*.xml
		</param-value>
	</context-param>

	<filter>
		<filter-name>springSecurityFilterChain</filter-name>
		<filter-class>
			org.springframework.web.filter.DelegatingFilterProxy
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>springSecurityFilterChain</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>


配置起来很简单,由于我的security是整合到现有项目中的.一些jar可能已经存在. 单独做demo的朋友配置的时候可能会出现问题.

本想分开发挣点积分..但怕大家看起来累.. 就发到一起吧.. (*^__^*)

使用篇

1、建立login.jsp页面.内容如下:
<form action="<%=path %>/j_spring_security_check" method="post">
    	USERNAME:<input type="text" name="j_username" value="${sessionScope['SPRING_SECURITY_LAST_USERNAME']}" /><br/>
    	PASSWORD:<input type="password" name="j_password" value="" /><br/>
    	<input type="checkbox" name="_spring_security_remember_me" />两周之内不必登陆<br/>
		<input type="submit">    	
    </form>


j_spring_security_check : 为security验证中心(不知道怎么说合适.暂时这么理解吧..).
j_username: 验证用户名;
j_password: 验证密码;
${sessionScope['SPRING_SECURITY_LAST_USERNAME']}:使用最后一次登录用户名.
_spring_security_remember_me:记住我...

2、xml配置,配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/security
	http://www.springframework.org/schema/security/spring-security-3.0.xsd">

	<!-- auto-config = true 则使用from-login. 如果不使用该属性 则默认为http-basic(没有session). 
		access-denied-page:出错后跳转到的错误页面;
	-->
	<http auto-config="true" access-denied-page="/common/403.jsp">
		<!-- intercept-url:拦截器,可以设定哪些路径需要哪些权限来访问. filters=none 不使用过滤,也可以理解为忽略 -->
		<intercept-url pattern="/index.jsp" access="ROLE_USER" />
		<intercept-url pattern="/login.jsp" filters="none" />
		<intercept-url pattern="/common/**" filters="none" />
		<intercept-url pattern="/script/**" filters="none" />
		<intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" />
		<intercept-url pattern="/user.jsp" access="ROLE_USER" />
		
		<!-- session-management是针对session的管理. 这里可以不配置. 如有需求可以配置. -->
		<!-- id登陆唯一. 后登陆的账号会挤掉第一次登陆的账号  error-if-maximum-exceeded="true" 禁止2次登陆;
			session-fixation-protection="none" 防止伪造sessionid攻击. 用户登录成功后会销毁用户当前的session.
			创建新的session,并把用户信息复制到新session中.
		 -->
		<session-management session-fixation-protection="none">
			<concurrency-control/>
		</session-management>
		
		<!-- login-page:默认指定的登录页面. authentication-failure-url:出错后跳转页面. default-target-url:成功登陆后跳转页面 -->
		<form-login login-page="/login.jsp"
			authentication-failure-url="/common/403.jsp"
			default-target-url="/admin.jsp" />
		<!-- logout-success-url:成功注销后跳转到的页面; -->
		<logout logout-success-url="/login.jsp"/>
		<http-basic />
		
	</http>

	<!-- 
	连接池.我spring配置文件中配的有.所以这里就注掉了.
	<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
        <beans:property name="url" value="jdbc:mysql://localhost/demo"/> 
        <beans:property name="username" value="root"/> 
        <beans:property name="password" value="root"/> 
    </beans:bean> 
	 -->

	<!-- 权限管理操作 -->
	<authentication-manager>
		<authentication-provider>
			<!-- 
			密码加密方式. 常用的有md5 和 sha. 
			salt-source:忘记了.. 手头api关了,网速卡就不上网查了. 类似在md5上又加了一层. 放置暴力破解. 追加安全性.
			<password-encoder hash="md5">
				<salt-source user-property="username"/>
			</password-encoder>
			 -->
			 <!-- 注入dataSource验证数据库中的用户名.密码.账号状态.和权限相关; -->
			<jdbc-user-service data-source-ref="dataSource"
				users-by-username-query="select username,password,enabled from user where username = ? and enabled = 1"
				authorities-by-username-query="select u.username,r.name from user u join user_role ur on u.uid = ur.uid join role r on r.rid = ur.rid where u.username = ?" />
			<!--
				使用固定的用户名和密码及权限来做验证. 
				<user-service>
				<user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
				<user name="user" password="user" authorities="ROLE_USER" />
				</user-service>
			-->
		</authentication-provider>
	</authentication-manager>
	<!-- 
		<beans:bean id="userDetailsServiceImpl" class="com.demo.test.service.impl.UserDetailsServiceImpl" />
	-->
	
	<!-- 
		此配置只是自己学习的一个小demo. 数据库也建的比较随意 比较简单. 使用的是角色权限. 个人比较推荐组权限来控制.. (由于工作经验限制,此处为个人理解)
		我的库如下:
		user:username\password\enabled
		role:name\desc
		user_role:uid\rid
	 -->
</beans:beans>

以上配置结束后可以完成用户登录\权限验证等操作.
配置和使用到这里就结束了. 今天下午的小心得.. spring-security很强大. 在这里感谢family168的在线帮助. 谢谢.. 希望对没有接触过spring-security的朋友有所帮助.
分享到:
评论
16 楼 ieanwfg201 2013-01-05  
sundoctor 写道
hyperprice 写道
看了family168的2和3的官方汉化文档. 里面有章为保护方法. 有几种实现方式. 其中一种是annotation注解来控制访问权限. 但今天做了几个例子都没有成功.

security xml:
<global-method-security secured-annotations="enabled"
		jsr250-annotations="enabled" />



java:
@Secured("ROLE_USER")
	void user();
	
	@Secured("ROLE_ADMIN")
	void admin();


然后运行访问用户对应的方法时后台就会抛异常. 如下
2010-06-23 14:58:36,156 [http-80-1] ERROR [500.jsp] - An Authentication object was not found in the SecurityContext
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext


求解..


这个用过,没问题,挺好的。


如果要使用这个方法保存的annotation的话需要设置一个全局的开启该注解的配置,好像是需要在http标签之前使用吧,具体标签可以去查
15 楼 aslily1234 2012-07-14  
顶一个~~~~~写得很详细,感谢楼主!!!!
14 楼 yangzhichenhb 2011-03-23  
LZ的东西学习了,第一次接触感触颇深。
13 楼 Wen_Chang 2011-01-05  
打个包 研究研究
12 楼 faming521 2010-10-10  
楼主,要是把源码打包就好了,,
11 楼 counters15 2010-10-07  
感谢楼主分享,用楼主的代码配置成功了,呼~
经过实践session-fixation-protection="none"的作用,是保证当前登录用户不能在其他位置重复登录。
10 楼 faithfighting 2010-09-30  
family168的在线帮助 真的很好 整个网上就这个最好了,
不过挺希望楼主多写的相关学习的资料,项目中的心得
9 楼 海风308 2010-09-03  
我也想知道3相对于2来说,有哪些改动?
8 楼 archerfrank 2010-06-24  
3 和 2 区别有多大?
7 楼 sundoctor 2010-06-24  
hyperprice 写道
看了family168的2和3的官方汉化文档. 里面有章为保护方法. 有几种实现方式. 其中一种是annotation注解来控制访问权限. 但今天做了几个例子都没有成功.

security xml:
<global-method-security secured-annotations="enabled"
		jsr250-annotations="enabled" />



java:
@Secured("ROLE_USER")
	void user();
	
	@Secured("ROLE_ADMIN")
	void admin();


然后运行访问用户对应的方法时后台就会抛异常. 如下
2010-06-23 14:58:36,156 [http-80-1] ERROR [500.jsp] - An Authentication object was not found in the SecurityContext
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext


求解..


这个用过,没问题,挺好的。
6 楼 take 2010-06-24  
先收下, 有空再回来看..
唉, 时间好紧..
5 楼 7454103 2010-06-24  
LZ辛苦 不错!
4 楼 mxdba321123 2010-06-23  
hyperprice 写道
看了family168的2和3的官方汉化文档. 里面有章为保护方法. 有几种实现方式. 其中一种是annotation注解来控制访问权限. 但今天做了几个例子都没有成功.

security xml:
<global-method-security secured-annotations="enabled"
		jsr250-annotations="enabled" />



java:
@Secured("ROLE_USER")
	void user();
	
	@Secured("ROLE_ADMIN")
	void admin();


然后运行访问用户对应的方法时后台就会抛异常. 如下
2010-06-23 14:58:36,156 [http-80-1] ERROR [500.jsp] - An Authentication object was not found in the SecurityContext
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext


求解..



这个功能我当初使用的时候也有问题,虽然没有抛出异常,但是根本没有起作用,估计是BUG吧,但是官方的例子是没有问题的

还有官方pdf中用的是

@Pre and @Post Annotations这个注释

具体是15.3章节,我试过,可惜也没有作用
3 楼 steafler 2010-06-23  
不错,楼主打个包共享下源码研究研究
2 楼 hyperprice 2010-06-23  
看了family168的2和3的官方汉化文档. 里面有章为保护方法. 有几种实现方式. 其中一种是annotation注解来控制访问权限. 但今天做了几个例子都没有成功.

security xml:
<global-method-security secured-annotations="enabled"
		jsr250-annotations="enabled" />



java:
@Secured("ROLE_USER")
	void user();
	
	@Secured("ROLE_ADMIN")
	void admin();


然后运行访问用户对应的方法时后台就会抛异常. 如下
2010-06-23 14:58:36,156 [http-80-1] ERROR [500.jsp] - An Authentication object was not found in the SecurityContext
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext


求解..
1 楼 liushihua71632 2010-06-23  
LZ辛苦了,虽然基础,但看看可以回忆一下基础知识 

相关推荐

Global site tag (gtag.js) - Google Analytics