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

二 ,如何實現(xiàn)
1 ,導入依賴
<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() // 其他請求需要認證
.and()
.formLogin() // 使用表單登錄
.loginPage("/login") // 指定登錄頁面
.permitAll() // 允許所有用戶訪問登錄頁面
.and()
.logout() // 配置注銷
.logoutUrl("/logout") // 注銷路徑
.logoutSuccessUrl("/login?logout") // 注銷成功后跳轉的頁面
.permitAll();
}
//緩存中
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
//添加 名字為 admin 密碼 為 admin123 的用戶 基于權限 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}表示不加密,實際項目中應該使用加密的密碼
.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";
}
}到此這篇關于springSecurity用戶認證和授權的實現(xiàn)的文章就介紹到這了,更多相關springSecurity 認證和授權內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Springboot整合SpringSecurity實現(xiàn)登錄認證和鑒權全過程
- SpringBoot整合SpringSecurity和JWT和Redis實現(xiàn)統(tǒng)一鑒權認證
- SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權動態(tài)權限問題
- SpringBoot集成SpringSecurity和JWT做登陸鑒權的實現(xiàn)
- SpringSecurity動態(tài)加載用戶角色權限實現(xiàn)登錄及鑒權功能
- springboot+jwt+springSecurity微信小程序授權登錄問題
- SpringSecurity實現(xiàn)權限認證與授權的使用示例
- SpringSecurity進行認證與授權的示例代碼
- 深入淺析springsecurity入門登錄授權
- mall整合SpringSecurity及JWT實現(xiàn)認證授權實戰(zhàn)
- SpringSecurity頁面授權與登錄驗證實現(xiàn)(內(nèi)存取值與數(shù)據(jù)庫取值)
- SpringSecurity 鑒權與授權的具體使用
相關文章
struts2 validation.xml 驗證規(guī)則代碼解析
這篇文章主要介紹了struts2 validation.xml 驗證規(guī)則代碼解析,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
如何解決Project SDK is not defined問題
這篇文章主要介紹了如何解決Project SDK is not defined問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Springboot居然可以設置動態(tài)的Banner(推薦)
這篇文章主要介紹了Springboot居然可以設置動態(tài)的Banner,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Java內(nèi)存各部分OOM出現(xiàn)原因及解決方法(必看)
下面小編就為大家?guī)硪黄狫ava內(nèi)存各部分OOM出現(xiàn)原因及解決方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04

