springsecurity 基本使用詳解
最近開(kāi)始學(xué)習(xí)了springsecurity框架,為寫(xiě)后臺(tái)頁(yè)面做個(gè)權(quán)限管理什么的打基礎(chǔ)。
springsecurity是基礎(chǔ)springboot的,所以創(chuàng)建一個(gè)springboot工程引入依賴(lài)就可以很輕松的整合springsecurity了。(類(lèi)似的權(quán)限管理框架還有shiro)
1. 創(chuàng)建一個(gè)普通的springboot項(xiàng)目(不用勾選任何東西),我這邊使用的springboot版本是2.2.1.RELEASE
依賴(lài)如下:
pom.xml
<dependencies> <!--spring web依賴(lài)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--spring security依賴(lài)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!--mysql驅(qū)動(dòng)--> <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>
隨意編寫(xiě)一個(gè)測(cè)試的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"; } }
啟動(dòng)springboot服務(wù),打開(kāi)控制臺(tái)會(huì)發(fā)現(xiàn)有這么一串東西
Using generated security password: 649a23c2-fcbc-4f9b-b643-0a0d8167dcf4
當(dāng)你在瀏覽器輸入上面的controller時(shí),會(huì)彈出一個(gè)登錄的界面:如圖
出現(xiàn)這個(gè)界面就說(shuō)明springsecurity整合進(jìn)來(lái)了,springsecurity默認(rèn)有一個(gè)用戶名為user
,密碼就是控制臺(tái)那一串649a23c2-fcbc-4f9b-b643-0a0d8167dcf4
,輸入之后點(diǎn)擊login in就可以訪問(wèn)到controller了
這樣就算是把springsecurity整合好了。
到此這篇關(guān)于springsecurity 基本使用的文章就介紹到這了,更多相關(guān)springsecurity使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單局域網(wǎng)聊天室
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單局域網(wǎng)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Java調(diào)用echarts提供的地圖壓縮方法來(lái)壓縮地圖
今天小編就為大家分享一篇關(guān)于Java調(diào)用echarts提供的地圖壓縮方法來(lái)壓縮地圖,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Spring?Boot深入排查?java.lang.ArrayStoreException異常
這篇文章介紹了Spring?Boot深入排查?java.lang.ArrayStoreException異常,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12Java Hibernate對(duì)象(瞬時(shí)態(tài),持久態(tài),脫管態(tài))詳解
這篇文章主要介紹了Java Hibernate對(duì)象(瞬時(shí)態(tài),持久態(tài),脫管態(tài))詳解的相關(guān)資料,這里對(duì)Java Hibernate對(duì)象進(jìn)行了介紹及總結(jié),需要的朋友可以參考下2016-11-11基于jstree使用JSON數(shù)據(jù)組裝成樹(shù)
這篇文章主要為大家詳細(xì)介紹了基于jstree使用JSON數(shù)據(jù)組裝成樹(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08