Java项目首先要提的就是jar包了,Springsecurity的jar下载地址:http://static.springsource.org/spring-security/site/downloads.html。不过我的项目里的jar包比较旧点了,是从以前项目抽取出来的,我的工程结构图如下:
第一个实例:
第一个例子是最基本,最简单的,我第一次接触springsecurity时候觉得这个技术真惊艳,不过现在感觉也就那么回事了。
我首先编写的是web.xml:
<?xml version="1.0" encoding="UTF-8"?>
02.<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03.xmlns="http://java.sun.com/xml/ns/javaee"
04.xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
05.xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
06.id="WebApp_ID" version="2.5">
07. <display-name>SpringSecurityPrj</display-name>
08. <context-param>
09. <param-name>contextConfigLocation</param-name>
10. <param-value>
11. classpath:applicationContext*.xml
12. </param-value>
13. </context-param>
14. <filter>
15. <filter-name>springSecurityFilterChain</filter-name>
16. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
17. </filter>
18. <filter-mapping>
19. <filter-name>springSecurityFilterChain</filter-name>
20. <url-pattern>/*</url-pattern>
21. </filter-mapping>
22. <listener>
23. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
24. </listener>
25. <welcome-file-list>
26. <welcome-file>index.jsp</welcome-file>
27. </welcome-file-list>
28.</web-app>
接下来编写的是applicationContext-security.xml文件:
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. <!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 -->
10. <http auto-config="true">
11. <intercept-url pattern="/**" access="ROLE_USER"/>
12. </http>
13. <!-- 认证管理器。用户名密码都集成在配置文件中 -->
14. <authentication-manager>
15. <authentication-provider>
16. <user-service>
17. <user name="sharp" password="sharp" authorities="ROLE_USER"/>
18. </user-service>
19. </authentication-provider>
20. </authentication-manager>
21.</beans:beans>
另外我新建了一个index.jsp文件,作用是登录成功后返回到index.jsp页面:
[java] view plain copy
01.<%@ page language="java" contentType="text/html; charset=UTF-8"
02. pageEncoding="UTF-8"%>
03.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.<html>
05.<head>
06.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
07.<title>登录首页</title>
08.</head>
09.<body>
10.<span color="red">登录成功!</span>
11.</body>
12.</html>
当我们在浏览器地址栏里输入下面的url:
呵呵,内置的登录页面,挺好玩的。没有使用过springsecurity可能还没发现我在那里配置用户名和密码吧,看下面一段代码,这里就是用户名和密码:
<user name="sharp" password="sharp" authorities="ROLE_USER"/>
测试一:
我们录入用户名:admin;密码:admin,然后点击提交查询,最终页面如下:
登录失败了哦!
测试二:我们录入用户名:sharp;密码:sharp;如下图:
点击提交查询后,页面如下:
页面跳转到index.jsp页面,登录成功了。
哈哈,用这个做登录是不是很easy啊!
(博主温馨提示:我开始做测试是使用myeclipse,所有操作都没问题,然后改用eclipse-Java EE,每次启动tomcat,eclipse都报了Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor错误,启动了45秒后tomcat自动停止,我在百度查原因,找到了一个解决方法,解决方法如下:
[java] view plain copy
01.Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
02.修改 workspace\.metadata\.plugins\org.eclipse.wst.server.core\servers.xml文件。
03.<servers>
04.<server hostname="localhost" id="JBoss v5.0 at localhost" name="JBoss v5.0 at
05.localhost" runtime-id="JBoss v5.0" server-type="org.eclipse.jst.server.generic.jboss5"
06.server-type-id="org.eclipse.jst.server.generic.jboss5" start-timeout="1000" stop-
07.timeout="15" timestamp="0">
08.<map jndiPort="1099" key="generic_server_instance_properties" port="8090"
09.serverAddress="127.0.0.1" serverConfig="default"/>
10.</server>
11.</servers>
12.把 start-timeout="45" 改为 start-timeout="1000" 或者更长
13.重启eclipse就可以了。
14.这个原因就是:启动tomcat需要的时间比45秒大了,Eclipse会判断tomcat在默认的时间是否启动了,如果在默认45秒没有启动就会报错了。
在第一个实例基础上我做了第二个实例。
第二个实例:
第一个例子里的登录页面是springsecurity的默认页面,这种死板的页面满足不了千变万化的用户需求,因此这个实例里我将自定义登录界面,这里我们还要加入几个jar包,最新的lib包下的目录如下所示:
新建一个login.jsp页面代码如下:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
02.<%@ page language="java" contentType="text/html; charset=UTF-8"
03. pageEncoding="UTF-8"%>
04.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
05.<html>
06.<head>
07.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
08.<title>用户登录</title>
09.</head>
10.<body onLoad="document.f.j_username.focus();">
11.<c:if test="${not empty param.login_error}">
12. <font color="red">
13. 登录失败,请重试.<br/><br/>
14. 原因:<c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>
15. </font>
16.</c:if>
17.<form name="f" action="<c:url value='j_spring_security_check'/>" method="POST">
18. <table>
19. <tr>
20. <td>用户名:</td>
21. <td>
22. <input type='text' name='j_username' value='<c:if test="${not empty param.login_error}"><c:out value="${SPRING_SECURITY_LAST_USERNAME}"/></c:if>'/>
23. </td>
24. </tr>
25. <tr>
26. <td>密 码:</td>
27. <td><input type='password' name='j_password'></td>
28. </tr>
29. <tr>
30. <td>
31. <input type="checkbox" name="_spring_security_remember_me"></td><td>两周内自动登录
32. </td>
33. </tr>
34. <tr>
35. <td colspan='2' align="center">
36. <input name="submit" type="submit">
37. <input name="reset" type="reset">
38. </td>
39. </tr>
40. </table>
41.</form>
42.</body>
43.</html>
修改applicationContext-security.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
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. <!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 -->
10. <http auto-config="true">
11. <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
12. <intercept-url pattern="/**" access="ROLE_USER"/>
13. <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/>
14. </http>
15. <!-- 认证管理器。用户名密码都集成在配置文件中 -->
16. <authentication-manager>
17. <authentication-provider>
18. <user-service>
19. <user name="sharp" password="sharp" authorities="ROLE_USER"/>
20. </user-service>
21. </authentication-provider>
22. </authentication-manager>
23. <!-- 指定中文资源 。默认命名空间是security,所以要加前缀beans: -->
24. <beans:bean id="messageSource"
25. class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
26. <beans:property name="basename" value="classpath:org/springframework/security/messages_zh_CN"/>
27. </beans:bean>
28.</beans:beans>
我们在浏览器地址栏里输入下面的url,点击回车,界面如下:
登录失败!
测试二:我们录入用户名:sharp;密码:sharp;如下图:
点击提交查询,结果如下:
第三个实例:
只要是接触过权限管理的程序员都知道,一般的权限管理都有角色的概念,但是传统的角色都是在数据库建模,然后用编程的方式来实现的。在springsecurity里面就有角色的概念,用起来也很方便,上面的例子里我们使用了一个角色ROLE_USER,现在我们添加一个角色ROLE_ADMIN,我们修改applicationContext-security.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
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. <!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 -->
10. <http auto-config="true">
11. <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
12. <!-- 增加 ROLE_ADMIN角色-->
13. <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/>
14. <intercept-url pattern="/**" access="ROLE_USER"/>
15. <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/>
16. </http>
17. <!-- 认证管理器。用户名密码都集成在配置文件中 -->
18. <authentication-manager>
19. <authentication-provider>
20. <user-service>
21. <!-- 添加ROLE_ADMIN角色 -->
22. <user name="admin" password="admin" authorities="ROLE_USER,ROLE_ADMIN"/>
23. <user name="sharp" password="sharp" authorities="ROLE_USER"/>
24. </user-service>
25. </authentication-provider>
26. </authentication-manager>
27. <!-- 指定中文资源 。默认命名空间是security,所以要加前缀beans: -->
28. <beans:bean id="messageSource"
29. class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
30. <beans:property name="basename" value="classpath:org/springframework/security/messages_zh_CN"/>
31. </beans:bean>
32.</beans:beans>
另外我新建一个admin.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
02. pageEncoding="UTF-8"%>
03.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.<html>
05.<head>
06.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
07.<title>Admin 管理界面</title>
08.</head>
09.<body>
10.<p style="color:red">admin.jsp页面</p>
11.</body>
12.</html>
修改下index.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
02. pageEncoding="UTF-8"%>
03.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.<html>
05.<head>
06.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
07.<title>登录首页</title>
08.</head>
09.<body>
10.<span color="red">登录成功!</span>
11.<br/>
12.<a href="admin.jsp">admin.jsp</a>
13.</body>
14.</html
测试一:
我们输入的用户名:sharp;密码:sharp,登录成功了,我们进入到了页面index.jsp:
点击admin.jsp链接,结果如下:
sharp用户没有ROLE_ADMIN角色的权限,所以sharp访问不了admin.jsp页面。
测试二:
我们输入的用户名:admin;密码:admin,登录成功了,我们进入到了页面index.jsp(如上图),
然后
点击admin.jsp链接,结果如下:
用户admin是可以访问admin.jsp页面。
好了,今天学习结束了!
总结下:今天都是具体操作,而且这些操作在网上多的不得了,不过我想学springsecurity都得从这一步开始,现在我对springsecurity理解还不深,整篇文章都是如何去编码,而没有一些知识的讲解,我会尽全力一步步深入,做软件最好还是知其所以然,明天看能不能研究原理,下一篇由springsecurity和数据库的结合开始。