Spring Security 初试

本文介绍了一个基于SpringSecurity实现的简单权限管理系统。该系统通过配置实现了用户登录验证、角色权限控制等功能,并详细展示了web.xml及applicationContext*.xml的配置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


最近想改进以前系统的权限管理功能,发现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>

至此一个简单的权限管理完成了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值