SpringBoot整合SpringSecurity系列(8)-角色權限控制


一、角色權限判斷
除了內置權限控制外,Spring Security 中還支持很多其他權限控制,這些方法一般都用於用戶已經被認證后,判斷用戶是否具有特定的權限
例如登錄微信之后,是否有權限發紅包、發朋友圈等,后面的操作是建立在登錄驗證之后,表示授權部分,前者為認證部分
內置主要有以下五個角色權限驗證方法

public class AuthorizedUrl {
/**
* Shortcut for specifying URLs require a particular role. If you do not want to
* have "ROLE_" automatically inserted see {@link #hasAuthority(String)}.
* @param role the role to require (i.e. USER, ADMIN, etc). Note, it should not
* start with "ROLE_" as this is automatically inserted.
* @return the {@link ExpressionUrlAuthorizationConfigurer} for further
* customization
*/
public ExpressionInterceptUrlRegistry hasRole(String role) {
return access(ExpressionUrlAuthorizationConfigurer.hasRole(role));
}

/**
* Shortcut for specifying URLs require any of a number of roles. If you do not
* want to have "ROLE_" automatically inserted see
* {@link #hasAnyAuthority(String...)}
* @param roles the roles to require (i.e. USER, ADMIN, etc). Note, it should not
* start with "ROLE_" as this is automatically inserted.
* @return the {@link ExpressionUrlAuthorizationConfigurer} for further
* customization
*/
public ExpressionInterceptUrlRegistry hasAnyRole(String... roles) {
return access(ExpressionUrlAuthorizationConfigurer.hasAnyRole(roles));
}

/**
* Specify that URLs require a particular authority.
* @param authority the authority to require (i.e. ROLE_USER, ROLE_ADMIN, etc).
* @return the {@link ExpressionUrlAuthorizationConfigurer} for further
* customization
*/
public ExpressionInterceptUrlRegistry hasAuthority(String authority) {
return access(ExpressionUrlAuthorizationConfigurer.hasAuthority(authority));
}

/**
* Specify that URLs requires any of a number authorities.
* @param authorities the requests require at least one of the authorities (i.e.
* "ROLE_USER","ROLE_ADMIN" would mean either "ROLE_USER" or "ROLE_ADMIN" is
* required).
* @return the {@link ExpressionUrlAuthorizationConfigurer} for further
* customization
*/
public ExpressionInterceptUrlRegistry hasAnyAuthority(String... authorities) {
return access(ExpressionUrlAuthorizationConfigurer.hasAnyAuthority(authorities));
}

/**
* Specify that URLs requires a specific IP Address or <a href=
* "https://forum.spring.io/showthread.php?102783-How-to-use-hasIpAddress&p=343971#post343971"
* >subnet</a>.
* @param ipaddressExpression the ipaddress (i.e. 192.168.1.79) or local subnet
* (i.e. 192.168.0/24)
* @return the {@link ExpressionUrlAuthorizationConfigurer} for further
* customization
*/
public ExpressionInterceptUrlRegistry hasIpAddress(String ipaddressExpression) {
return access(ExpressionUrlAuthorizationConfigurer.hasIpAddress(ipaddressExpression));
}
}

 

二、內置權限判斷
2.1 hasAuthority(String)
判斷用戶是否具有特定的權限,用戶的權限在登錄邏輯中(UserDetailsService實現類)返回UserDetails對象時指定,也可以是數據庫驗證方式中在數據庫配置的權限
如之前創建的用戶權限信息(user_role)中的下面信息就表示所具備的權限
ROLE_ADMIN和ROLE_USER就是用戶的權限,權限嚴格區分大小寫

3. 在配置類中通過hasAuthority(“ROLE_ADMIN”) 設置表示具有管理員權限時才能訪問

  • resources/static/ 創建只有ROLE_ADMIN權限(root用戶)才能訪問的root.html

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>SpringBoot Security</title>
</head>
<body>
<h3>歡迎登錄SpringBoot Security管理員首頁</h3>
</body>
</html>

.antMatchers("/admin.html").hasAuthority("ROLE_ADMIN")

  1. 注意hasAuthority只能指定一個權限
    • ExpressionInterceptUrlRegistry hasAuthority(String authority)
  2. 使用admin賬號登錄(root才有ROLE_ADMIN權限),提示403禁止訪問

 

使用root賬號登錄,正常訪問

 

7. 權限正常控制,特別注意權限區分大小寫

ROLE_ADMIN和role_admin是兩個權限
2.2 hasAnyAuthority(String …)
如果用戶具備給定權限中某一個,就允許訪問
在resources/static/ 創建具備ROLE_USER和ROLE_ADMIN權限(root和admin用戶)能訪問的admin.html

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>SpringBoot Security</title>
</head>
<body>
<h3>歡迎登錄SpringBoot Security Admin首頁</h3>
</body>
</html>

配置文件中配置權限訪問

.antMatchers("/admin.html").hasAnyAuthority("ROLE_ADMIN", "ROLE_USER")

此時使用root或admin用戶均可以正常訪問

 

2.3 hasRole(String)
判斷用戶是否具備指定角色,如果用戶具備給定角色就允許訪問,否則出現 403
參數取值來源於自定義登錄邏輯UserDetailsService實現類中創建返回User對象時給User賦予的授權
在給用戶賦予角色時角色需要以**ROLE_**開頭,后面添加角色名稱
ROLE_ADMIN中**ROLE_**是固定開頭,ADMIN是角色名稱
特別注意和hasAnyAuthority、hasAuthority的區別,后者必須要全稱,前者只要ROLE_角色
使用 hasRole()時參數只需要寫ADMIN、USER即可,如果帶前綴啟動報錯

.antMatchers("/root.html").hasRole("ADMIN")

帶上ROLE_啟動報錯

role should not start with 'ROLE_' since it is automatically inserted. Got 'ROLE_ADMIN'

這是由於源碼里面默認會拼接ROLE_,並且斷言了不能以其開圖

  • org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer

2.4 hasAnyRole(String …)

  1. 如果用戶具備給定角色的任意一個,就允許被訪問
  2. 和之前hasAnyAuthority(String …)類似,只需要替換為hasAnyRole規則即可

.antMatchers("/admin.html").hasAnyRole("ADMIN", "USER")

使用 hasRole()時參數也只需要寫ADMIN、USER即可

  • org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer

2.5 hasIpAddress(String)
如果請求ip是指定的ip發起則允許訪問,可以通過**request.getRemoteAddr()**獲取ip地址
需要注意在本機進行測試時 localhost 和 127.0.0.1 輸出的 ip地址是不一樣的
通過 localhost 進行訪問時IP地址:0:0:0:0:0:0:0:1
通過 127.0.0.1 訪問時IP地址:127.0.0.1
通過具體 ip 進行訪問時IP地址:具體IP地址
在resources/static/ 創建只有ip是127.0.0.1時允許訪問的ip.html

 

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>SpringBoot Security</title>
</head>
<body>
<h3>IP驗證成功,SpringBoot Security Admin首頁</h3>
</body>
</html>

.antMatchers("/ip.html").hasIpAddress("127.0.0.1")


通過正常IP地址訪問

通過localhost訪問

 


————————————————
版權聲明:本文為CSDN博主「TianXinCoord」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/sinat_34104446/article/details/116031067


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM