springSecurity用戶認(rèn)證和授權(quán)的實(shí)現(xiàn)
一,框架介紹
Spring 是一個(gè)非常流行和成功的 Java 應(yīng)用開發(fā)框架。Spring Security 基于 Spring 框架,提供了一套 Web 應(yīng)用安全性的完整解決方案。一般來說,Web 應(yīng)用的安全性包括用戶認(rèn)證(Authentication)和用戶授權(quán)(Authorization)兩個(gè)部分。
(1)用戶認(rèn)證指的是:驗(yàn)證某個(gè)用戶是否為系統(tǒng)中的合法主體,也就是說用戶能否訪問該系統(tǒng)。用戶認(rèn)證一般要求用戶提供用戶名和密碼。系統(tǒng)通過校驗(yàn)用戶名和密碼來完成認(rèn)證過程。
(2)用戶授權(quán)指的是驗(yàn)證某個(gè)用戶是否有權(quán)限執(zhí)行某個(gè)操作。在一個(gè)系統(tǒng)中,不同用戶所具有的權(quán)限是不同的。比如對一個(gè)文件來說,有的用戶只能進(jìn)行讀取,而有的用戶可以進(jìn)行修改。一般來說,系統(tǒng)會為不同的用戶分配不同的角色,而每個(gè)角色則對應(yīng)一系列的權(quán)限。
Spring Security其實(shí)就是用filter,多請求的路徑進(jìn)行過濾。
(1)如果是基于Session,那么Spring-security會對cookie里的sessionid進(jìn)行解析,找到服務(wù)器存儲的sesion信息,然后判斷當(dāng)前用戶是否符合請求的要求。
(2)如果是token,則是解析出token,然后將當(dāng)前請求加入到Spring-security管理的權(quán)限信息中去
認(rèn)證與授權(quán)實(shí)現(xiàn)思路

二 ,如何實(shí)現(xiàn)
1 ,導(dǎo)入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>2,代碼
package com.demo.config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@EnableWebSecurity
public class Security extends WebSecurityConfiguration {
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/public/**").permitAll() // 允許訪問公開路徑
.antMatchers("/public/index").hasRole("vip") // 需要指定用戶在可以使用
.antMatchers("/admin/**").hasRole("admin") // 需要指定用戶在可以使用
.anyRequest().authenticated() // 其他請求需要認(rèn)證
.and()
.formLogin() // 使用表單登錄
.loginPage("/login") // 指定登錄頁面
.permitAll() // 允許所有用戶訪問登錄頁面
.and()
.logout() // 配置注銷
.logoutUrl("/logout") // 注銷路徑
.logoutSuccessUrl("/login?logout") // 注銷成功后跳轉(zhuǎn)的頁面
.permitAll();
}
//緩存中
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
//添加 名字為 admin 密碼 為 admin123 的用戶 基于權(quán)限 ADMIN
.withUser("admin").password("admin123").roles("ADMIN")
.and()
.withUser("user").password("user123").roles("USER");
}
//從數(shù)據(jù)庫中
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 從數(shù)據(jù)庫或其他數(shù)據(jù)源加載用戶信息
// 這里簡單起見,直接使用內(nèi)存中的用戶信息
if ("admin".equals(username)) {
return org.springframework.security.core.userdetails.User.builder()
.username("admin")
.password("{noop}admin") // {noop}表示不加密,實(shí)際項(xiàng)目中應(yīng)該使用加密的密碼
.roles("ADMIN")
.build();
} else if ("user".equals(username)) {
return org.springframework.security.core.userdetails.User.builder()
.username("user")
.password("{noop}user")
.roles("USER")
.build();
} else {
throw new UsernameNotFoundException("User not found");
}
}
}
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MyController {
@GetMapping("/user")
@Secured("ROLE_USER") // 要求用戶具有ROLE_USER角色才能訪問
public String userPage() {
return "user";
}
}import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MyController {
@GetMapping("/admin")
@PreAuthorize("hasRole('ADMIN')") // 要求用戶具有ROLE_ADMIN角色才能訪問
public String adminPage() {
return "admin";
}
@GetMapping("/user")
@PreAuthorize("hasRole('USER')") // 要求用戶具有ROLE_USER角色才能訪問
public String userPage() {
return "user";
}
}到此這篇關(guān)于springSecurity用戶認(rèn)證和授權(quán)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springSecurity 認(rèn)證和授權(quán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot整合SpringSecurity實(shí)現(xiàn)登錄認(rèn)證和鑒權(quán)全過程
- SpringBoot整合SpringSecurity和JWT和Redis實(shí)現(xiàn)統(tǒng)一鑒權(quán)認(rèn)證
- SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動態(tài)權(quán)限問題
- SpringBoot集成SpringSecurity和JWT做登陸鑒權(quán)的實(shí)現(xiàn)
- SpringSecurity動態(tài)加載用戶角色權(quán)限實(shí)現(xiàn)登錄及鑒權(quán)功能
- springboot+jwt+springSecurity微信小程序授權(quán)登錄問題
- SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例
- SpringSecurity進(jìn)行認(rèn)證與授權(quán)的示例代碼
- 深入淺析springsecurity入門登錄授權(quán)
- mall整合SpringSecurity及JWT實(shí)現(xiàn)認(rèn)證授權(quán)實(shí)戰(zhàn)
- SpringSecurity頁面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn)(內(nèi)存取值與數(shù)據(jù)庫取值)
- SpringSecurity?鑒權(quán)與授權(quán)的具體使用
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(54)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08
最新IntelliJ?IDEA?2022配置?Tomcat?8.5?的詳細(xì)步驟演示
這篇文章主要介紹了IntelliJ?IDEA?2022?詳細(xì)配置?Tomcat?8.5?步驟演示,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
深入理解Java運(yùn)行時(shí)數(shù)據(jù)區(qū)_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java運(yùn)行時(shí)數(shù)據(jù)區(qū)的相關(guān)知識,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06
解決SpringCloud Gateway配置自定義路由404的坑
這篇文章主要介紹了解決SpringCloud Gateway配置自定義路由404的坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

