@toc
操作系统:
Windows 10 x64
集成开发环境:
Spring Tool Suite 4
Version: 4.12.1.RELEASE
Build Id: 202110260750
浏览器(客户端):
Google Chrome
版本 97.0.4692.71(正式版本) (64 位)
访问 spring initializr,填写相关信息,最重要的一步是添加 Spring Security 依赖:
完成以上配置之后,点击 GENERATE 按钮生成项目,并下载本地。
解压得到的压缩包。然后,在 PowerShell 中使用 tree /a /f
命令查看其目录结构:
PS G:\workspace-spring-tool-suite-4-4.12.1.RELEASE\Spring-Security-Hello> tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 0000-4823
G:.
| .gitignore
| HELP.md
| mvnw
| mvnw.cmd
| pom.xml
|
+---.mvn
| \---wrapper
| maven-wrapper.jar
| maven-wrapper.properties
|
\---src
+---main
| +---java
| | \---com
| | \---mk
| | SpringSecurityHelloApplication.java
| |
| \---resources
| | application.properties
| |
| +---static
| \---templates
\---test
\---java
\---com
\---mk
SpringSecurityHelloApplicationTests.java
打开 Spring Tool Suite 4:


完成以上操作之后,可以看到刚刚导入的项目:
再次在 PowerShell 中使用 tree /a /f
命令查看其目录结构:
PS G:\workspace-spring-tool-suite-4-4.12.1.RELEASE\Spring-Security-Hello> tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 0000-4823
G:.
| .classpath
| .factorypath
| .gitignore
| .project
| HELP.md
| mvnw
| mvnw.cmd
| pom.xml
|
+---.mvn
| \---wrapper
| maven-wrapper.jar
| maven-wrapper.properties
|
+---.settings
| org.eclipse.core.resources.prefs
| org.eclipse.jdt.apt.core.prefs
| org.eclipse.jdt.core.prefs
| org.eclipse.m2e.core.prefs
|
+---src
| +---main
| | +---java
| | | \---com
| | | \---mk
| | | SpringSecurityHelloApplication.java
| | |
| | \---resources
| | | application.properties
| | |
| | +---static
| | \---templates
| \---test
| \---java
| \---com
| \---mk
| SpringSecurityHelloApplicationTests.java
|
\---target
+---classes
| | application.properties
| |
| +---com
| | \---mk
| | SpringSecurityHelloApplication.class
| |
| \---META-INF
| | MANIFEST.MF
| |
| \---maven
| \---com.mk
| \---Spring-Security-Hello
| pom.properties
| pom.xml
|
+---generated-sources
| \---annotations
+---generated-test-sources
| \---test-annotations
\---test-classes
\---com
\---mk
SpringSecurityHelloApplicationTests.class
其中,pom.xml 文件的内容如下,第 26 ~ 29 行是 Spring Security 的依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.mk</groupId>
<artifactId>Spring-Security-Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Security-Hello</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Spring Boot 启动器:
package com.mk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringSecurityHelloApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSecurityHelloApplication.class, args);
}
}
以 Spring Boot App 方式启动项目,控制台输出:
提示:蓝色选中的文字是登录密码(随机,每次都不一样),默认的用户名是
user
(参考:Web > 4. Spring Security)。
打开浏览器,访问 http://localhost:8080/,Spring Security 检测到我们是未经身份认证的用户,所以将我们重定向至登录页面(拒绝我们的访问,要求登录):
在登录页面输入正确的用户名和密码,登录成功之后,我们就可以访问 http://localhost:8080/(通过身份认证,允许访问):
注意:这里出现 Whitelabel Error Page 是因为服务器并没有提供指向 http://localhost:8080/ 链接的资源,属于正常现象。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。