Spring Security入门Demo

一、spring Security简介

SpringSecurity,这是一种基于Spring AOP和Servlet过滤器的安全框架。它提供全面的安全性解决方案,同时在Web请求级和方法调用级处理身份确认和授权。在Spring Framework基础上,Spring Security充分利用了依赖注入(DI,Dependency Injection)和面向切面技术。

 

二、建立工程

参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建名为spring-security-demo的Maven工程。

工程的最终目录结构为

 

 

三、源代码

1 pom.xml里引入所需要的包

 

[html]  view plain  copy
 
 
  1. <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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  2.   <modelVersion>4.0.0modelVersion>  
  3.   <groupId>spring-security-demogroupId>  
  4.   <artifactId>spring-security-demoartifactId>  
  5.   <version>0.0.1-SNAPSHOTversion>  
  6.   <packaging>warpackaging>  
  7.   <name>spring-security-demoname>  
  8.   <description/>  
  9.   <properties>  
  10.     <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  11.   properties>  
  12.     <dependencies>  
  13.         <dependency>  
  14.             <groupId>javaxgroupId>  
  15.             <artifactId>javaee-apiartifactId>  
  16.             <version>7.0version>  
  17.             <scope>providedscope>  
  18.         dependency>  
  19.     <dependency>  
  20.         <groupId>jstlgroupId>  
  21.         <artifactId>jstlartifactId>  
  22.         <version>1.2version>  
  23.     dependency>  
  24.         <dependency>  
  25.             <groupId>org.springframeworkgroupId>  
  26.             <artifactId>spring-webmvcartifactId>  
  27.             <version>3.2.9.RELEASEversion>  
  28.             <type>jartype>  
  29.             <scope>compilescope>  
  30.         dependency>  
  31.         <dependency>  
  32.             <groupId>org.springframeworkgroupId>  
  33.             <artifactId>spring-contextartifactId>  
  34.             <version>3.2.9.RELEASEversion>  
  35.         dependency>  
  36.         <dependency>  
  37.             <groupId>org.springframework.securitygroupId>  
  38.             <artifactId>spring-security-configartifactId>  
  39.             <version>3.1.6.RELEASEversion>  
  40.             <type>jartype>  
  41.             <scope>compilescope>  
  42.         dependency>  
  43.         <dependency>  
  44.             <groupId>org.springframework.securitygroupId>  
  45.             <artifactId>spring-security-taglibsartifactId>  
  46.             <version>3.1.6.RELEASEversion>  
  47.             <type>jartype>  
  48.             <scope>compilescope>  
  49.         dependency>  
  50.         <dependency>  
  51.             <groupId>log4jgroupId>  
  52.             <artifactId>log4jartifactId>  
  53.             <version>1.2.15version>  
  54.             <type>jartype>  
  55.             <scope>compilescope>  
  56.         dependency>  
  57.     dependencies>  
  58.    
  59. project>  

 

 

2 web.xml

 

[html]  view plain  copy
 
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  
  3.   <display-name>spring-security-demodisplay-name>  
  4.     <filter>  
  5.         <filter-name>springSecurityFilterChainfilter-name>  
  6.         <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>  
  7.     filter>  
  8.     <filter-mapping>  
  9.         <filter-name>springSecurityFilterChainfilter-name>  
  10.         <url-pattern>/*url-pattern>  
  11.     filter-mapping>  
  12.   
  13.     <context-param>  
  14.         <param-name>contextConfigLocationparam-name>  
  15.         <param-value>  
  16.         /WEB-INF/spring-security.xml  
  17.         /WEB-INF/applicationContext.xml  
  18.         param-value>  
  19.     context-param>  
  20.   
  21.     <servlet>  
  22.         <servlet-name>springservlet-name>  
  23.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  24.         <load-on-startup>1load-on-startup>  
  25.     servlet>  
  26.     <servlet-mapping>  
  27.         <servlet-name>springservlet-name>  
  28.         <url-pattern>/url-pattern>  
  29.     servlet-mapping>  
  30.   
  31.     <listener>  
  32.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  33.     listener>  
  34. web-app>  

 

 

这里两处关于springsecurity的配置表示项目中所有路径的资源都要经过Spring Security。

注意:最好是将DelegatingFilterProxy写在DispatcherServlet之前,否则Spring Security可能不会正常工作。

 

3 spring-servlet.xml

[html]  view plain  copy
 
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  6.   
  7.       
  8.     <bean id="viewResolver"  
  9.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  10.         p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />  
  11. beans>  

 

这个XML配置声明一个视图解析器.在控制器中会根据JSP名映射到/WEB-INF/jsp中相应的位置。

 

4 applicationContext.xml

 

[html]  view plain  copy
 
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.             http://www.springframework.org/schema/context  
  9.             http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.             http://www.springframework.org/schema/mvc   
  11.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  12.   
  13.       
  14.     <context:annotation-config />  
  15.   
  16.       
  17.     <mvc:annotation-driven />  
  18.   
  19. beans>  

 

 

5 spring-security.xml

 

[html]  view plain  copy
 
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:security="http://www.springframework.org/schema/security"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.             http://www.springframework.org/schema/security   
  8.             http://www.springframework.org/schema/security/spring-security-3.1.xsd">  
  9.       
  10.       
  11.       
  12.     <security:authentication-manager>  
  13.             <security:authentication-provider user-service-ref="customUserDetailsService">  
  14.                     <security:password-encoder ref="passwordEncoder"/>  
  15.             security:authentication-provider>  
  16.     security:authentication-manager>  
  17.       
  18.       
  19.     <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>  
  20.   
  21.     

你可能感兴趣的:(Spring Security入门Demo)