成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring Security權限控制系列(六)

開發 架構
Spring Security還提供了基于訪問注解的方式細化接口權限的控制定義,接下來使用基于注解的方式控制Controller接口權限。

環境:Springboot2.4.12 + Spring Security 5.4.9

本篇主要內容:

  1. 業務接口權限認證

上一篇:《??Spring Security權限控制系列(五)??》

演示案例

有如下接口:

@RestController
@RequestMapping("/business")
public class BussinessController {
@GetMapping("/{id}")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}
}

安全配置:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() ;
http.authorizeRequests().antMatchers("/resources/**", "/cache/**", "/process/login").permitAll() ;
http.authorizeRequests().antMatchers("/demos/**").hasRole("USERS") ;
http.authorizeRequests().antMatchers("/api/**").hasRole("ADMIN") ;
// 上面的配置都是基于之前的文章,這里我們不需要關心,僅僅看下面這個接口配置接口
// 這里我們會要求所有以/business開始的所有請求
http.authorizeRequests().antMatchers("/business/**").authenticated() ;
}
}

有了上面的配置,啟動服務訪問http://localhost:8080/business/100接口時會要求登錄,只要登錄成功,接口就可以訪問。

這里我不希望通過如下方式進行的權限設置:

// hasRole("xxx")  或 hasAuthority("xxxx")
http.authorizeRequests().antMatchers("/business/**").hasRole("xxx")

這種寫法限定了所有的/business開頭的請求都由于固定的權限,/business可能會有很多的子接口,每種子接口可能我們都需要定義不同的權限才可訪問,這時候如果在通過上面的方式配置就太繁瑣了。Spring Security還提供了基于訪問注解的方式細化接口權限的控制定義,接下來使用基于注解的方式控制Controller接口權限。

注意:并不是基于注解的權限控制只能應用到Controller上,只是我們一般都會加到Controller上;其實任何Service方法都是可以使用的。這些注解也可以直接加到接口方法上。

開啟方法認證

@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true, prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}

屬性說明:

jsr250Enabled:啟用對JSR-250注釋的支持。@RolesAllowed。

prePostEnabled:啟用基于表達式的語法支持(jsr250Enabled和securedEnabled都是基于簡單角色的約束)。@PreAuthorize。

securedEnabled:啟用@Secured注解的支持。

示例:

@GetMapping("/{id}")
@RolesAllowed("ROLE_USERS") // ①
@Secured("ROLE_USERS1") // ②
@PreAuthorize("hasRole('USERS')") // ③
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}
  1. 接收一個String[] 數組,可以定義多個角色。
  2. 接收一個String[] 數組,可以定義多個角色。
  3. 可以使用SpEL表達式。

本篇內容只演示基于@PreAuthorize注解的權限控制,其它兩個都非常簡單不做演示。

PreAuthorize注解使用

該注解用于指定方法訪問控制表達式的注釋,該表達式將被計算以確定是否允許方法調用。默認支持的如下表達式:

示例1:

訪問該接口必須具備USERS角色。

@PreAuthorize("hasRole('USERS')")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}

示例2:

訪問該接口只要具有其中任意一種角色即可。

@PreAuthorize("hasAnyRole('USERS', 'ADMIN')")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}

示例3:

訪問該接口必須擁有bus:news:see權限。

@PreAuthorize("hasAuthority('bus:news:see')")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}

實例4:

該接口只要擁有如下任意一個權限即可。

@PreAuthorize("hasAnyAuthority('bus:news:see', 'bus:news:write')")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}

注意:這里的hasRole和hasAuthority區別?

權限認證使用的 表達式根對象的基類是SecurityExpressionRoot。該基類中實現了相應方法的調用

public abstract class SecurityExpressionRoot implements SecurityExpressionOperations {
private String defaultRolePrefix = "ROLE_";
@Override
public final boolean hasRole(String role) {
return hasAnyRole(role);
}
@Override
public final boolean hasAnyRole(String... roles) {
return hasAnyAuthorityName(this.defaultRolePrefix, roles);
}
@Override
public final boolean hasAuthority(String authority) {
return hasAnyAuthority(authority);
}
@Override
public final boolean hasAnyAuthority(String... authorities) {
return hasAnyAuthorityName(null, authorities);
}
private boolean hasAnyAuthorityName(String prefix, String... roles) {
Set<String> roleSet = getAuthoritySet();
for (String role : roles) {
// 拼接ROLE_前綴
String defaultedRole = getRoleWithDefaultPrefix(prefix, role);
if (roleSet.contains(defaultedRole)) {
return true;
}
}
return false;
}
}

通過上面的源碼知道,不管是hasRole還是hasAuthority最終都是調用的hasAnyAuthorityName方法,而hasRole方法拼接ROLE_前綴。

總結:

  • 業務接口Controller權限控制個各種方式。
責任編輯:姜華 來源: 今日頭條
相關推薦

2022-08-30 08:50:07

Spring權限控制

2022-08-15 08:42:46

權限控制Spring

2022-08-30 08:36:13

Spring權限控制

2022-08-30 08:43:11

Spring權限控制

2022-08-15 08:45:21

Spring權限控制

2024-02-18 12:44:22

2020-06-17 08:31:10

權限控制Spring Secu

2021-07-27 10:49:10

SpringSecurity權限

2023-01-13 08:11:24

2022-06-16 10:38:24

URL權限源代碼

2020-09-16 08:07:54

權限粒度Spring Secu

2023-05-26 01:05:10

2022-05-05 10:40:36

Spring權限對象

2017-04-25 10:46:57

Spring BootRESRful API權限

2022-06-27 14:21:09

Spring語言

2022-01-07 07:29:08

Rbac權限模型

2021-08-29 18:36:57

項目

2021-04-23 07:33:10

SpringSecurity單元

2019-11-22 09:40:40

SpringJava編程語言

2020-09-09 09:19:00

SpringSecurity權限
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品一区二区在线 | 国产黄色大片 | 日本特黄a级高清免费大片 特黄色一级毛片 | 久久久久久免费精品一区二区三区 | av一区二区在线观看 | 久久天堂网 | 亚洲国产精品成人 | 国产一区二区三区四区五区加勒比 | 国产精品久久久久久久久大全 | 岛国av免费在线观看 | 成人av免费在线观看 | 日韩一区二区在线视频 | 亚洲一区二区av在线 | 91国产精品| 国产成人精品免费 | 国产黄色小视频 | 国产免费xxx | 中文字幕在线免费视频 | 男人天堂久久久 | 黄色毛片大全 | 中文字幕日韩欧美一区二区三区 | 国产成人精品高清久久 | 久久成人免费观看 | 一级黄色夫妻生活 | 日韩午夜一区二区三区 | 久久久青草婷婷精品综合日韩 | 爱爱免费视频 | 久久精品国产一区 | 天天操天天射天天 | 在线免费观看毛片 | 91亚洲国产 | 国产一区二区在线视频 | 91 在线| 99精品国产一区二区三区 | 久久久久久久久毛片 | 国产精品欧美一区二区三区不卡 | 怡红院怡春院一级毛片 | 91av免费版 | 亚洲在线中文字幕 | 亚洲精品国产成人 | 日本成人片在线观看 |