本文演示了Spring Security的最最基本用法,二个页面(或理解成二个url),一个需要登录认证后才能访问(比如:../admin/),一个可匿名访问(比如:../welcome)
注:以下内容参考了 http://www.mkyong.com/spring-security/spring-security-hello-world-example/
一、利用STS(Spring Tools Suite)创建一个Spring MVC Project
如果不想使用STS,在普通Eclipse上安装Spring Tool Suite插件也行,用Spring插件创建项目的好处在于,很多配置已经自动帮我们生成好了,基本的项目架子已经具备,不需要在这上面花太多心思,下面是项目结构图
pom文件中的dependencies
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.cnblogs</groupId> 6 <artifactId>SpringSecurity-HelloWorld-XML</artifactId> 7 <name>SpringSecurity-HelloWorld-XML</name> 8 <packaging>war</packaging> 9 <version>1.0</version> 10 <properties> 11 <jdk.version>1.6</jdk.version> 12 <spring.version>3.2.8.RELEASE</spring.version> 13 <spring.security.version>3.2.3.RELEASE</spring.security.version> 14 <jstl.version>1.2</jstl.version> 15 </properties> 16 17 <dependencies> 18 <!-- Spring dependencies --> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-core</artifactId> 22 <version>${spring.version}</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-aop</artifactId> 27 <version>${spring.version}</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-beans</artifactId> 32 <version>${spring.version}</version> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-expression</artifactId> 37 <version>${spring.version}</version> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-context</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-context-support</artifactId> 47 <version>${spring.version}</version> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-web</artifactId> 52 <version>${spring.version}</version> 53 </dependency> 54 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-webmvc</artifactId> 58 <version>${spring.version}</version> 59 </dependency> 60 61 <!-- Spring Security --> 62 <dependency> 63 <groupId>org.springframework.security</groupId> 64 <artifactId>spring-security-core</artifactId> 65 <version>${spring.security.version}</version> 66 </dependency> 67 68 <dependency> 69 <groupId>org.springframework.security</groupId> 70 <artifactId>spring-security-web</artifactId> 71 <version>${spring.security.version}</version> 72 </dependency> 73 74 <dependency> 75 <groupId>org.springframework.security</groupId> 76 <artifactId>spring-security-config</artifactId> 77 <version>${spring.security.version}</version> 78 </dependency> 79 80 <!-- jstl for jsp page --> 81 <dependency> 82 <groupId>jstl</groupId> 83 <artifactId>jstl</artifactId> 84 <version>${jstl.version}</version> 85 </dependency> 86 87 </dependencies> 88 <build> 89 <plugins> 90 <plugin> 91 <artifactId>maven-eclipse-plugin</artifactId> 92 <version>2.9</version> 93 <configuration> 94 <additionalProjectnatures> 95 <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 96 </additionalProjectnatures> 97 <additionalBuildcommands> 98 <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 99 </additionalBuildcommands> 100 <downloadSources>true</downloadSources> 101 <downloadJavadocs>true</downloadJavadocs> 102 </configuration> 103 </plugin> 104 <plugin> 105 <groupId>org.apache.maven.plugins</groupId> 106 <artifactId>maven-compiler-plugin</artifactId> 107 <version>2.5.1</version> 108 <configuration> 109 <source>1.6</source> 110 <target>1.6</target> 111 <compilerArgument>-Xlint:all</compilerArgument> 112 <showWarnings>true</showWarnings> 113 <showDeprecation>true</showDeprecation> 114 </configuration> 115 </plugin> 116 <plugin> 117 <groupId>org.codehaus.mojo</groupId> 118 <artifactId>exec-maven-plugin</artifactId> 119 <version>1.2.1</version> 120 <configuration> 121 <mainClass>org.test.int1.Main</mainClass> 122 </configuration> 123 </plugin> 124 </plugins> 125 </build> 126 </project>