日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

springboot security 权限不足_SpringBoot 整合 SpringSecurity 之起源篇(零)

發(fā)布時間:2025/3/12 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot security 权限不足_SpringBoot 整合 SpringSecurity 之起源篇(零) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本篇為SpringSecurity的第一篇,主要來介紹下什么是SpringSecurity,以及在springboot中如何使用它

I. 基本知識點

官方文檔: https://docs.spring.io/spring-security/site/docs/5.2.2.BUILD-SNAPSHOT/reference/htmlsingle/#community-help

下面是官方介紹

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

用國語,簡單抽象的說一下它的定義

  • 很的認(rèn)證和訪問權(quán)限校驗框架

那么具體能干嘛?

  • 用戶登錄認(rèn)證:用戶名+密碼登錄,確定用戶身份
  • 用戶訪問鑒權(quán)(常見的ACL訪問控制列表,RBAC角色訪問控制):判定是否有權(quán)限訪問某個資源
  • 安全保護(hù)(CSRF跨站點攻擊,Session Fixation會話固定攻擊...)

II. 初體驗

接下來我們看一下再springboot中如何使用springsecurity

1. 配置

首先得是spring boot項目,然后添加上security的依賴即可,相對完整的pom配置如下(注意我們使用的springboot版本為2.2.1.RELEASE)

org.springframework.boot spring-boot-starter-parent 2.2.1.RELEASEUTF-8UTF-81.8org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin spring-snapshotsSpring Snapshotshttps://repo.spring.io/libs-snapshot-localtruespring-milestonesSpring Milestoneshttps://repo.spring.io/libs-milestone-localfalsespring-releasesSpring Releaseshttps://repo.spring.io/libs-release-localfalse

2. 實例demo

上面配置完之后,啥都不需要干,項目已經(jīng)接入了spring security;項目中的服務(wù)都需要登錄之后才能訪問

// 程序啟動類@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}// rest 服務(wù)@RestControllerpublic class IndexRest { @GetMapping(path = {"/", "/index"}) public String index() { return "hello this is index!"; } @GetMapping(path = "hello") public String hello(String name) { return "welcome " + name; }}

當(dāng)我們需要訪問首頁時,會發(fā)現(xiàn)直接302重定向到登錄頁面了,如下圖

spring security默認(rèn)給我們生成了一個用戶名為user,密碼為控制臺中輸出的一行日志如Using generated security password: aa410186-5c04-4282-b217-507ffb1f61eb

登錄之后會重定向回我們之前訪問的url,通過抓包可以看到,登錄成功之后,會設(shè)置請求方的cookie,后續(xù)的請求攜帶cookie來表明用戶身份

3. 基本配置

上面雖然演示了一個hello world的初體驗項目,但是這個默認(rèn)的用戶名/密碼有點鬼畜,默認(rèn)的配置主要來自于org.springframework.boot.autoconfigure.security.SecurityProperties.User,下面是截圖(所以前面的用戶名為user)

接下來我們需要配置為對人類友好的方式,在項目的配置文件application.yml中,指定登錄的用戶名/密碼

spring: security: user: name: yihuihui password: 123456

重啟測試項目,使用新的用戶名/密碼(yihuihui/123456)就可以登錄成功了;

4. 用戶身份獲取

上面雖然是一個簡單的case,但還有一點不得不提一下,在我的接口中,雖然知道你登錄了,但怎么知道你是誰呢?

我們可以直接通過HttpServletRequest#getRemoteUser()的方法來獲取登錄用戶; 或者通過SecurityContextHolder.getContext().getAuthentication().getPrincipal()來獲取授權(quán)信息

我們來寫一個通用方法

public String getUser() { return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteUser();}// orpublic Object getUser() { SecurityContextHolder.getContext().getAuthentication().getPrincipal();}

然后稍微改一下我們的服務(wù)接口

@GetMapping(path = {"/", "/index"})public String index() { return "hello this is index! welcome " + getUser();}

再次訪問之后,結(jié)果如下

5. 小結(jié)

本文主要是spring security系列的起源篇,第一節(jié)介紹了下什么是SpringSecurity,有什么特點

  • spring security是一個很的認(rèn)證(可以簡單理解為登錄驗證)和鑒權(quán)(可簡單理解為訪問控制)框架
  • 三大特點:登錄 + 鑒權(quán) + 安全防護(hù)

第二節(jié)介紹了一個簡單入門的HelloWorld實例

  • springboot項目,添加依賴 spring-boot-starter-security; 所有的http接口訪問都需要登錄,默認(rèn)提供一個用戶名為user,密碼為控制臺輸出的UUID字符串
  • 通過spring.security.user.name和spring.security.user.password來指定用戶名密碼
  • 通過HttpServletRequest#getRemoteUser()獲取登錄用戶

那么問題來了,什么系統(tǒng)可能只有一個用戶呢?要多用戶怎么辦?不同的用戶不同的權(quán)限怎么辦?某些接口所有人都可以訪問又怎么辦?

II. 其他

0. 項目

  • 工程:https://github.com/liuyueyi/spring-boot-demo
  • 代碼: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-security/000-basic-demo

1. 一灰灰Blog

盡信書則不如,以上內(nèi)容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發(fā)現(xiàn)bug或者有更好的建議,歡迎批評指正,不吝感激

下面一灰灰的個人博客,記錄所有學(xué)習(xí)和工作中的博文,歡迎大家前去逛逛

  • 一灰灰Blog個人博客 https://blog.hhui.top
  • 一灰灰Blog-Spring專題博客 http://spring.hhui.top

總結(jié)

以上是生活随笔為你收集整理的springboot security 权限不足_SpringBoot 整合 SpringSecurity 之起源篇(零)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。