SpringSecurity6.x簡單使用

SpringBoot2.X和SpringSecurity的整合使用,前面已經(jīng)講過。不過是5.x的版本。
參考:
SpringBoot整合SpringSecurity
現(xiàn)在使用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() {
        // 若使用純靜態(tài)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,以適應(yīng)當前的上下文路徑。這樣,無論應(yīng)用程序部署在哪個上下文路徑下,生成的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>版權(quán)所有 &copy; 2023</p>
                </footer>
            </div>
        </body>
    </html>
1.4 啟動訪問

項目啟動后,我們可以直接訪問頁面:http://127.0.0.1:8080/

image.png

2. 引入SpringSecurity

可以先看官網(wǎng)的文檔:spring-security
官網(wǎng)給了 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/
這時會跳轉(zhuǎn)到login登陸頁面,讓我們登陸的,我們只是引入依賴,什么都沒做,就會這樣。

login

2.3 登陸

使用默認賬號密碼登陸:賬號默認是user,密碼在console控制臺,是隨機生成的UUID。

image.png

輸入賬號密碼登陸后,就跳轉(zhuǎn)到剛才的頁面了。

2.4 賬號密碼

SpringSecurity登陸后,會在cookie中生成JSESSIONID的,以后訪問都是靠這個的。

JSESSIONID

默認的賬號密碼在springboot自動配置包下的SecurityProperties類中:org.springframework.boot.autoconfigure.security.SecurityProperties
image.png

我們?nèi)绻蛔銎渌渲?,只想自定義賬號密碼,可以在application.yml設(shè)置如下內(nèi)容:

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

這是項目重啟就不會生成uuid密碼了,使用設(shè)置的就可以登陸了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容