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

【自学Spring Security】之第一个能运行的Sample

    博客分类:
  • SSH
阅读更多

     最近学习了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自动为我们添加了登录页面,还是挺简单的。就到这里吧,虽然好多标签不知道什么意思,权限管理的相关概念也不清楚,慢慢摸索吧。高手路过请轻拍,谢谢啦。

0
0
分享到:
评论
3 楼 leman_zk 2012-03-05  
keer2345 写道
强烈推荐 http://lengyun3566.iteye.com/,更推荐看那本英文原版的

感谢,正在看,也是记录一下自己的新的。
2 楼 keer2345 2012-03-04  
强烈推荐 http://lengyun3566.iteye.com/,更推荐看那本英文原版的
1 楼 smith789 2012-03-04  
 

相关推荐

Global site tag (gtag.js) - Google Analytics