Spring Security教程

时间:2024-11-07 23:03:56

原文地址:http://blog.****.net/jaune161/article/details/17640071

http://blog.****.net/jaune161/article/details/18736687

spring-security官网网址:http://docs.spring.io/spring-security/site/docs/3.2.0.RELEASE/reference/htmlsingle/#ns-config

目前Spring官方只提供Meven的下载方式。但在http://maven.springframework.org中有SpringSecurity及其他所有Sprin*品的下载方式。

http://maven.springframework.org/release/org/springframework/中有Spring相关的所有下载,但好像直到3.2版的,最新的版本在这个里面找不到

http://maven.springframework.org/release/org/springframework/security/spring-security/3.2.0.RELEASE/这个是SpringSecurity3.2的下载地址

Meven下载地址:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.security</groupId>
  4. <artifactId>spring-security-web</artifactId>
  5. <version>3.2.0.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.security</groupId>
  9. <artifactId>spring-security-config</artifactId>
  10. <version>3.2.0.RELEASE</version>
  11. </dependency>
  12. </dependencies>

本教程是基于SpringMVC3.2+Hibernate4+JPA2.0+SpringSecurity3.2的环境。SpringMVC3.2+Hibernate4+JPA2.0环境的搭建在这里就不多说了,主要讲下SpringSecurity的环境搭建

web.xml配置

  1. <!-- 加载Spring的配置文件 -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>
  5. classpath:applicationContext.xml,
  6. classpath:applicationContext-security.xml
  7. </param-value>
  8. </context-param>
  9. <!-- SpringSecurity 核心过滤器配置 -->
  10. <filter>
  11. <filter-name>springSecurityFilterChain</filter-name>
  12. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>springSecurityFilterChain</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>

applicationContext-security.xml命名空间配置,官方提供了两种配置方案

第一种、命名空间用beans开头,但是在配置中一直需要用<security:*>来配置。

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:security="http://www.springframework.org/schema/security"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/security
  7. http://www.springframework.org/schema/security/spring-security.xsd">
  8. ...
  9. </beans>

第二种、命名空间用security开头,在配置中不需要security前缀,但是bean的配置需要用<beans:bean>配置。

  1. <beans:beans xmlns="http://www.springframework.org/schema/security"
  2. xmlns:beans="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/security
  7. http://www.springframework.org/schema/security/spring-security.xsd">
  8. ...
  9. </beans:beans>

到此为止SpringSecurity的环境配置已基本完成

命名空间的配置可在spring的官方文档,第4章 Security Namespace Configuration 中找到,一下附上链接地址

http://docs.spring.io/spring-security/site/docs/3.2.0.RELEASE/reference/htmlsingle/#ns-config