【自学Spring Security】之第一个会运行的Sample
最近学习了SSH,越发觉得权限管理的重要性,上网看了一下权限管理框架,大致有Apache Shiro和Spring Security比较出名,因为刚学习了Spring,所以就选择Spring Security吧,上官网把jar包下载下来,运行了一下spring-security-samples-tutorial示例程序,感觉配置好难呀。上网搜索了一下Spring Security相关的文章,折腾了一下,没有跑通一个程序,感觉入门还是比较困难的,一上来就是相关概念头都大了。没办法,对照参考文档一步一步摸索吧。
先做一个最简单的应用,二个页面:index.html和admin.html,分别代表具有ROLE_USER和ROLE_ADMIN权限的人可以访问的页面,当然具有ROLE_ADMIN权限的用户两个页面都可以访问了。
首先新建web应用程序,导入tutorial实例程序下WEB-INF/lib下所有的jar文件,并将logbak.xml文件拷贝到src文件夹下。
1.修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext-security.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
原理很简单:listener用来加载配置文件,filter用来过滤所有访问请求。
2.新建applicationContext-security.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.1.xsd"> <debug /> <http auto-config="true"> <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/> <intercept-url pattern="/**" access="ROLE_USER"/> </http> <authentication-manager> <authentication-provider> <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:beans>
监听http请求,intercept-url中的权限要按照由细到粗的顺序排列,Spring Security从上到下匹配规则,符合条件即完成授权,若果上面两个intercept调换位置,任意请求都满足/**,则用户只要具有ROLE_USER权限则可以访问所有资源,包括/admin.jsp了。
<authentication-manager>中定义了两个用户,权限之间用逗号隔开。
3.新建index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>欢迎页面</title> </head> <body> <h2>这是首页</h2> <a href="admin.jsp">进入admin页面</a> </body> </html>
4.新建admin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <h2>管理员页面</h2> </body> </html>
好了,大功告成,运行代码看一下吧。Spring Security自动为我们添加了登录页面,还是挺简单的。就到这里吧,虽然好多标签不知道什么意思,权限管理的相关概念也不清楚,慢慢摸索吧。高手路过请轻拍,谢谢啦。
感谢,正在看,也是记录一下自己的新的。