Spring Security - 01 新建项目

环境

操作系统:

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

  1. 选择菜单 File > Import…

  2. Import 向导中,选择 Maven > Existing Maven Projects,然后,点击 Next > 按钮进入下一步

    在这里插入图片描述

  3. Import Maven Projects 对话框中,点击 Browse… 按钮,选择刚刚下载解压得到的项目,然后点击 Finish 按钮导入项目

    在这里插入图片描述

项目结构

完成以上操作之后,可以看到刚刚导入的项目:

在这里插入图片描述

再次在 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/ 链接的资源,属于正常现象。

在这里插入图片描述

参考

“How-to” Guides > 13. Security

Web > 4. Spring Security

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值