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

第一件事,下载中文说明文档,搜索一下就能找到。
第二件事,找到官方原版英文说明文档,搜索一下就能找到。
第三件事,需要注意的问题:
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拦截,会有可能出现异常。

好了,正式配置开始。
[size=large]第一步:配置web.xml[/size]
在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>

[size=large]第二步:加入SS3配置文件[/size]
在applicationContext.xml中加载SS3的配置文件,在文件最末加上以下代码(路径自己决定,但是要保证能加载到SS3的配置文件):

<import resource="applicationContext-security.xml"/>

[size=large]第三步:最简单的运行配置[/size]
为SS3的配置文件加上最简单的配置,让整个项目能运行起来,在SS3配置文件中加入如下代码(头文件请参考上面的代码):

<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>

运行一下,会报错,是否说没有配置<authentication-manager>?如果不是,请解决掉问题否则进入下一步
[size=large]第四步:配置<authentication-manager>[/size]
在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>

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

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

[size=large]第六步:定制我们自己的登录界面[/size]
新建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小段文字)。

要工作了,晚上在下一章介绍如何使用数据库验证用户名密码。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值