`

Spring Security 初试

 
阅读更多
最近想改进以前系统的权限管理功能,发现Spring Security功能很强大,于是学习下改进以往的权限管理
 
需要用到的jar包有:spring.jar、spring-security-core-2.0.4.jar、spring-security-core-tiger-2.0.4.jar
 
开发环境是:JDK6.0
 
    Web容器:
 
    Apache Tomcat6.0
 
    IDE工具:
 
    Eclipse3.2+MyEclipse5.0
 
首先配置web.xml
 
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value><!--加载spring配置文件,多个可用","分割,这里使用的是通配符,加载所有名称还有applicationContext的配置文件-->
    </context-param>
   <!--所有的用户在访问项目之前,都要先通过Spring Security的检测,这从第一时间把没有授权的请求排除在系统之外,保证系统资源的安全。-->      
    <filter>
     <filter-name>springSecurityFilterChain</filter-name>
     <filter-class>
      org.springframework.security.util.FilterToBeanProxy
     </filter-class>
     <init-param>
      <param-name>targetClass</param-name>
      <param-value>
      org.springframework.security.util.FilterChainProxy
      </param-value>
     </init-param>
    </filter> 
    
    <filter-mapping>
     <filter-name>springSecurityFilterChain</filter-name>
     <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <!-- 对spring容器实例化-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
applicationContext*.xml配置
 
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security" 声明在xml中使用Spring 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-2.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
 
    <http auto-config='true'><!--http部分配置如何拦截用户请求。auto-config='true'将自动配置几种常用的权限控制机制,包括form, anonymous, rememberMe-->
        <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" /><!--利用intercept-url来判断用户需要具有何种权限才能访问对应的url资源,可以在pattern中指定一个特定的url资源,也可以使用通配符指定一组类似的url资源。例子中定义的两个intercepter-url,第一个用来控制对/admin.jsp的访问,第二个使用了通配符/**,说明它将控制对系统中所有url资源的访问。-->
        <intercept-url pattern="/**" access="ROLE_USER" />
    </http>
 
    <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>
</beans:beans> 
 
页面index.jsp
 
<%@ page contentType="text/html; charset=GBK" %>
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<title>hello Spring Scecurity</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
</head>
<body>
 <h1>hello Spring Scecurity!</h1>
 您的用户名:<%=session.getAttribute("SPRING_SECURITY_LAST_USERNAME") %><br>
 <br><a href="admin.jsp">admin</a>
 <br><a href="logout.jsp">logout</a>
</body>
</html>
 
admin.jsp
 
<%@ page contentType="text/html; charset=GBK" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<title>hello Spring Scecurity</title>
</head>
<body>
 hello Spring Scecurity
 <br>您的用户名:<%=session.getAttribute("SPRING_SECURITY_LAST_USERNAME") %></body>
</html>
 
logout.jsp
 
<%@ page contentType="text/html; charset=GBK" %>
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<title>hello Spring Scecurity</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<%
 session.invalidate();//清空session
 response.sendRedirect("");//返回登陆页
 %>
</head>
<body>
 
</body>
</html>
 
至此一个简单的权限管理完成了。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics