Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架。它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和受权。如下是百科里对于Spring Security的介绍。java
Spring Security是一个可以为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组能够在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减小了为企业系统安全控制编写大量重复代码的工做。web
经过介绍也能够看出,spring security并不像apache shiro那样支持javase和ejb环境。spring
目前最新版本的Spring Security为4.2.2,可是我这里用了稳定版本3.1.3。下面例子为一个简单的Spring Security配置应用。apache
新建好项目以后在webapp下添加了两个jsp文件,adminPage.jsp和index.jsp。其中adminPage.jsp只有那些拥有ROLE_ADMIN,ROLE_USER其中一种权限的用户才能访问,而index.jsp只容许那些拥有ROLE_USER权限的用户才能访问。编程
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>SpringSecurity</display-name> <!-- 加载配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/config/applicationContext*.xml</param-value> </context-param> <!-- spring security 的过滤器配置 --> <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> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
在WEB-INF/config/下新建applicationContext.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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <http auto-config='true'> <intercept-url pattern="/adminPage.jsp" access="ROLE_ADMIN" /> <intercept-url pattern="/**" access="ROLE_USER" /> </http> <authentication-manager> <authentication-provider> <user-service> <user name="admin" password="123" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="user" password="123" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
说明:tomcat
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zmc</groupId> <artifactId>SpringSecurityDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringSecurityDemo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java-version>1.7</java-version> <org.springframework-version>3.2.2.RELEASE</org.springframework-version> <org.aspectj-version>1.6.10</org.aspectj-version> <org.slf4j-version>1.6.1</org.slf4j-version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <!-- Spring security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>3.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>3.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>3.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-crypto</artifactId> <version>3.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> <version>3.1.3.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
这样一个项目就构建完成了,部署到tomcat进行测试。安全
在浏览器上输入:http://localhost:8888/SpringSecurityDemo/,由于没有登录,因此没法访问index.jsp页面,这个时候spring security就起做用了,对资源进行拦截,由于没有符合权限的用户登录,因此就跳转到登录页面,其中这个登录页面是Spring Security自动生成的,这也是auto-config=”true”起的做用之一。mvc
而后输入用户名和密码,成功跳转到index.jsp页面。app
这里由于admin用户有ROLE_ADMIN和ROLE_USER权限,而index.jsp页面ROLE_USER权限便可访问,因此admin用户能够成功访问index.jsp和adminPage.jsp页面。
下面再来测试用户user,注意已经登录了的话,应该重启浏览器,要否则会一直记住用户,没法作测试。
从上图中能够看到,登录用户user,能够访问index.jsp页面可是没法访问adminPage.jsp。这是由于user用户只有ROLE_USER权限,而adminPage.jsp页面须要ROLE_USER权限,因此就拒绝访问。以上就是一个简单的spring security配置应用。