`
andy_ghg
  • 浏览: 290561 次
  • 性别: Icon_minigender_1
  • 来自: 扬州
社区版块
存档分类
最新评论

MyEclipse中Spring Security 3.0.3无废话配置(第一章)。

    博客分类:
  • Java
阅读更多
第一件事,下载中文说明文档,搜索一下就能找到。
第二件事,找到官方原版英文说明文档,搜索一下就能找到。
第三件事,需要注意的问题:
1.请使用MyEclipse 自带的Spring 3与Spring Security 3(以下简称SS3),我的MyEclipse是8.6,自带的版本是SS3.0.3。
2.登录的表单用户名必须是j-username,密码必须是j-password,表单的action必须是"/你的项目/j_spring_security_check",这里是否可以定制,我不清楚。
3.MyEclise 8.6自带的Hibernate与Spring与SS3之间没有任何Jar包冲突,出现问题请不要怀疑Jar包的问题。(至少我的目前为止没有任何冲突)
4.注意头文件,Spring的头文件是:
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="  
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>

SS3的头文件是
<?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:bean>

5.Spring security 3的Filter一定要在Struts2后面,否则表单提交会首先被Struts2拦截,会有可能出现异常。

好了,正式配置开始。
第一步:配置web.xml
在web.xml中加入Spring以及SS3的参数如下:
<!--Spring的ApplicationContext 载入 -->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- SpringSecurity filter-->
	<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>

第二步:加入SS3配置文件
在applicationContext.xml中加载SS3的配置文件,在文件最末加上以下代码(路径自己决定,但是要保证能加载到SS3的配置文件):
<import resource="applicationContext-security.xml"/>

第三步:最简单的运行配置
为SS3的配置文件加上最简单的配置,让整个项目能运行起来,在SS3配置文件中加入如下代码(头文件请参考上面的代码):
<http auto-config='true'>
    <intercept-url pattern="/**" access="ROLE_USER" />
</http>

运行一下,会报错,是否说没有配置<authentication-manager>?如果不是,请解决掉问题否则进入下一步
第四步:配置<authentication-manager>
在SS3的配置文件中加入以下代码:
<authentication-manager>
	<authentication-provider>
		<user-service>
			<user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
			<user name="bob" password="bobspassword" authorities="ROLE_USER" />
		</user-service>
	</authentication-provider>
</authentication-manager>

再次运行,如果没有出错,则进入下一步,否则请解决掉错误。

第五步:访问我们的项目
打开浏览器输入项目的URL地址,然后定位到index.jsp,这时候你会发现页面被强制定位到了一个登录界面,虽然难看,但是代表我们成功了,尝试输入用户名:jimi,密码:jimispassword,点击登录按钮,是否跳转到index.jsp(你得保证你项目里确实有这个页面)?
右键查看源代码,找到它的表单各项的名称、action等信息,下面备用。

第六步:定制我们自己的登录界面
新建login.jsp,然后在SS3.0的配置文件中的http标签中加入配置,最终如下所示:
<http auto-config='true' access-denied-page="/common/403.jsp">
		<intercept-url pattern="/css/**" filters="none" />
		<intercept-url pattern="/images/**" filters="none" />
		<intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
                <form-login login-page='/login.jsp' />
		<intercept-url pattern="/**" access="ROLE_USER" /> />
	</http>

common/403.jsp代表当用户没有权限的时候跳转到这个页面,form-login标签内就是指向你自定义的登录界面。配置完毕后,再次运行项目,输入URL,这时就会跳转到你的自定义登录界面了。输入帐号密码,点击登录,功能与刚才看到的一样(表单设计请参考第三件事中的第2小段文字)。

要工作了,晚上在下一章介绍如何使用数据库验证用户名密码。
2
0
分享到:
评论
3 楼 l5061109 2012-05-09  
密码校验是自己通过从数据库中取到用户信息,包括用户名,密码,角色。然后封装为一个User对象,返回给security,框架自己进行密码校验
2 楼 andy_ghg 2012-03-19  
Navee 写道
我有一点疑惑就是密码的验证是交给这个框架做的还是服务层自己实现的呢?

交给这个框架
1 楼 Navee 2012-03-18  
我有一点疑惑就是密码的验证是交给这个框架做的还是服务层自己实现的呢?

相关推荐

Global site tag (gtag.js) - Google Analytics