ITeye博客

spring-security3 配置和使用.

cleverbing · 2011-04-17 22:26 · 企业架构 · Markdown

最近项目中要使用到spring-security,闲来没事就研究了下。发现入门挺简单的,在这里把自己的心得发下,希望对没有接触过想接触的朋友有帮助。

1、在spring-security官网下载最新jar然后拷贝jar到项目的lib下。 2、在classpath下添加security配置文件,例如applicationContext-security.xml.网上现在大多都是2.0的schema. 要根据自己使用的版本而定.下面是3.0的schema.

Xml代码 01.
02. <beans:beans xmlns="http://www.springframework.org/schema/security" 03. xmlns:beans="http://www.springframework.org/schema/beans" 04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 05. xsi:schemaLocation="http://www.springframework.org/schema/beans 06. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
07. http://www.springframework.org/schema/security
08. http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 09.
10. </beans:beans> <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中添加配置,内容如下:

Xml代码 01.
02. 03. contextConfigLocation 04. 05. classpath*:/applicationContext*.xml
06. 07. 08.
09. 10. springSecurityFilterChain 11. 12. org.springframework.web.filter.DelegatingFilterProxy
13. 14. 15. 16. springSecurityFilterChain 17. / 18. 19. 20. 21. org.springframework.web.context.ContextLoaderListener
22. 23. contextConfigLocation classpath
:/applicationContext*.xml

<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页面.内容如下:

Html代码 01.
02. USERNAME: 03. PASSWORD: 04. 两周之内不必登陆 05. 06. USERNAME: PASSWORD: 两周之内不必登陆

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

2、xml配置,配置内容如下:

Xml代码 01.
02. <beans:beans xmlns="http://www.springframework.org/schema/security" 03. xmlns:beans="http://www.springframework.org/schema/beans" 04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 05. xsi:schemaLocation="http://www.springframework.org/schema/beans 06. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
07. http://www.springframework.org/schema/security
08. http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 09.
10. 13. 14. 15. 16. 17. 18. 19. 20. 21.
22. 23. 27. 28. 29. 30.
31. 32. <form-login login-page="/login.jsp" 33. authentication-failure-url="/common/403.jsp" 34. default-target-url="/admin.jsp" /> 35. 36. 37. 38.
39. 40.
41. 50.
51. 52. 53. 54. 61. 62. <jdbc-user-service data-source-ref="dataSource" 63. users-by-username-query="select username,password,enabled from user where username = ? and enabled = 1" 64. 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 = ?" /> 65. 72. 73. 74.
77.
78. 85.</beans:beans> <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的朋友有所帮助.

本文来自http://www.cnblogs.com/wenxiu/archive/2011/01/22/1942084.html

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。