這篇文章將為大家詳細講解有關一文教你正確的使用springsecurity,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
springsecurity是基礎springboot的,所以創建一個springboot工程引入依賴就可以很輕松的整合springsecurity了。(類似的權限管理框架還有shiro)
1. 創建一個普通的springboot項目(不用勾選任何東西),我這邊使用的springboot版本是2.2.1.RELEASE
依賴如下:pom.xml
<dependencies>
<!--spring web依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring security依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--mysql驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>隨意編寫一個測試的controller即可,eg:TestController.java
package com.sixteen.springsecurity01.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/hello")
public String hello(){
return "hello security";
}
}啟動springboot服務,打開控制臺會發現有這么一串東西

Using generated security password: 649a23c2-fcbc-4f9b-b643-0a0d8167dcf4
當你在瀏覽器輸入上面的controller時,會彈出一個登錄的界面:如圖

出現這個界面就說明springsecurity整合進來了,springsecurity默認有一個用戶名為user,密碼就是控制臺那一串649a23c2-fcbc-4f9b-b643-0a0d8167dcf4,輸入之后點擊login in就可以訪問到controller了

關于一文教你正確的使用springsecurity就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。