Spring Security 是 Spring Framework 的一个子项目
Spring Security 能用于保护各类 Java 应用程序(权限管理框架). 但在基于 Web 的应用程序中使用得最为普遍.
Spring Security 能以声明的方式来保护 Web 应用程序的 URL 访问. 只需简单的配置便可实现.
Spring Security 经过一系列 Servlet 过滤器为 Web 应用程序提供多种安全服务.
1. 搭建环境: 1). 加入 jar 包: Spring 的 jar 包和 SpringSecurity 的 jar 包. commons-logging-1.1.1.jar spring-aop-4.0.0.RELEASE.jar spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar spring-web-4.0.0.RELEASE.jar spring-security-config-3.1.0.M1.jar spring-security-core-3.1.0.M1.jar spring-security-taglibs-3.1.0.M1.jar spring-security-web-3.1.0.M1.jar 2). web.xml 文件中的配置: 配置 Spring, 配置 SpringSecurity 的 Filter <!-- 配置 Spring 的 Listener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置 SpringSecurity 的 Filter --> <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> 3). 在 Spring 的配置文件中配置 SpringSecurity 的信息: 须要拦截的 URL, 用户信息 <!-- 配置 http 请求相关的信息 --> <security:http auto-config="true"> <!-- 拦截的路径, 以及访问该路径须要有的权限 --> <security:intercept-url pattern="/index.jsp" access="ROLE_USER"/> <security:intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/> </security:http> <!-- 配置用户信息 --> <security:user-service id="userSerivce"> <!-- 配置用户名, 密码, 权限 --> <security:user name="admin" password="admin" authorities="ROLE_ADMIN, ROLE_USER"/> <security:user name="user" password="user" authorities="ROLE_USER"/> </security:user-service> <!-- 配置权限管理信息 --> <security:authentication-manager> <!-- 使用前面配置的用户信息 --> <security:authentication-provider user-service-ref="userSerivce"> </security:authentication-provider> </security:authentication-manager>