最近想改进以前系统的权限管理功能,发现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>
至此一个简单的权限管理完成了。
分享到:
相关推荐
Spring Security 演讲PPT(演讲嘉宾:张明星) WebSphere技术专家沙龙在广州圆满举办,WSC超级版主Fastzch(张明星)担任本次沙龙的演讲嘉宾,他给广州的WebSphere技术专家带来了以“Spring Security ”为主题的...
Spring Security三份资料,实战Spring Security 3.x.pdf;Spring Security 3.pdf;Spring Security使用手册.pdf
spring security spring security 中文文档
springsecurity是一个功能强大且高度可定制的身份验证和访问控制框架。springsecurity是一个专注于为Java应用程序提供身份验证和授权的框架。与所有Spring项目一样,Spring安全性的真正威力在于它可以很容易地扩展以...
Spring Security in Action
SpringSecurity框架的权限认证流程原理,请求到来时SpringSecurity如果调用层层过滤器来完成认证;
很多独立软件供应商,因为灵活的身份验证模式二选择Spring Security。这样做允许他们快速的集成到他们的终端客户需求的解决方案而不用进行大量工程或者改变客户的环境。如果上面的验证机制不符合你的需求,Spring ...
1.本项目为SpringCloud Gateway的微服务框架,整合了SpringSecurity,微服务间使用Redis来获取登陆的用户信息。 2.由于Gat
Spring Security:spring家族一员。是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转...
SpringSecurity学习总结源代码
Spring Security 3.pdf Spring Security 3.pdf Spring Security 3.pdf Spring Security 3.pdf
三更springsecurity学习笔记
SpringSecurity入门到进阶到高级,是我们老师给我们讲课用的,我们都照着配就没有问题,可以跑通,
该资源是基本Spring Security实战七篇文档中组织的源码,详情如下: ssecurity项目是Spring Security实战(一和二)的源码; ssecurity-db项目是Spring Security实战(三)的源码; ssceurity-page项目是Spring ...
Spring Security。 官网 Spring Security API(Spring Security 开发文档).CHM
spring security2.5 jar 和spring security2.5 整合必须的jar包
SpringSecurity课程文档下载 pdf 教学
Spring Security OAuth2.0学习笔记 什么是认证、授权、会话。 Java Servlet为支持http会话做了哪些事儿。 基于session认证机制的运作流程。 基于token认证机制的运作流程。 理解Spring Security的工作原理,Spring ...
spring security3 中文版本
Spring Security 2.0.x 参考手册 中文版