SpringSecurity6.x简单使用

SpringBoot2.X和SpringSecurity的整合使用,前面已经讲过。不过是5.x的版本。
参考:
SpringBoot整合SpringSecurity
现在使用SpringBoot3.X,SpringSecurity变成6.X了,有一些变化,记录一下。

  • SpringBoot版本:3.3.3
  • SpringSecurity版本:6.3.3(boot配套的)

1. 不引入SpringSecurity

新建Springboot3.3.3的测试项目

1.1 pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 方便测试引入的模板 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- thymeleaf和springsecurity6兼容性 -->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
1.2 新建 IndexController
@Controller
public class IndexController {
    
    @GetMapping("/")
    public String index() {
        // 若使用纯静态html,返回要写全,且index.html要放到resource/static目录下
//      return "index.html";
        // 若使用thymeleaf,不用后缀,且index.html要放到resource/templates目录下
        return "index";
    }
}
1.3 index.html

resource/templates目录下新建index.html

<html xmlns:th="https://www.thymeleaf.org">
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>测试security</title>
            <style>
                body {
                    font-family: Arial, sans-serif;
                    margin: 0;
                    padding: 0;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    height: 100vh;
                    background-color: #f7f7f7;
                }

                .container {
                    width: 80%;
                    max-width: 1200px;
                    padding: 20px;
                    background-color: #fff;
                    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
                }

                .shi {
                    text-align: center;
                }

                .out {
                    margin-right: 50px;
                    text-align: right;
                }

                header,
                footer {
                    text-align: center;
                    padding: 20px 0;
                }
            </style>
        </head>
        <body>
            <div class="container">
                <div class="out">
                    <h3>登出</h1>
                    <!--通过使用@{/logout},Thymeleaf将自动处理生成正确的URL,以适应当前的上下文路径。这样,无论应用程序部署在哪个上下文路径下,生成的URL都能正确地指向注销功能。-->
                     <a th:href="@{/logout}">Log Out</a>
                </div>

                <header>
                    <h1>测试用的</h1>
                </header>

                <main class="shi">
                    <p>童年,是追逐蝴蝶的欢笑,</p>
                    <p>是捉迷藏的惊险,</p>
                    <p>是那无忧无虑的时光,</p>
                    <p>藏在心底最温暖的角落。</p>
                </main>

                <footer>
                    <p>版权所有 &copy; 2023</p>
                </footer>
            </div>
        </body>
    </html>
1.4 启动访问

项目启动后,我们可以直接访问页面:http://127.0.0.1:8080/

image.png

2. 引入SpringSecurity

可以先看官网的文档:spring-security
官网给了 security 做了些什么:

功能

2.1 pom.xml

pom文件添加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.2 重启

重启项目,我们再访问页面:http://127.0.0.1:8080/
这时会跳转到login登陆页面,让我们登陆的,我们只是引入依赖,什么都没做,就会这样。

login

2.3 登陆

使用默认账号密码登陆:账号默认是user,密码在console控制台,是随机生成的UUID。

image.png

输入账号密码登陆后,就跳转到刚才的页面了。

2.4 账号密码

SpringSecurity登陆后,会在cookie中生成JSESSIONID的,以后访问都是靠这个的。

JSESSIONID

默认的账号密码在springboot自动配置包下的SecurityProperties类中:org.springframework.boot.autoconfigure.security.SecurityProperties
image.png

我们如果不做其他配置,只想自定义账号密码,可以在application.yml设置如下内容:

spring.security.user.name: zs
spring.security.user.password: zs123

这是项目重启就不会生成uuid密码了,使用设置的就可以登陆了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容