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

如何在 TienChin 項目中自定義權限表達式

開發
MethodSecurityExpressionRoot 類實際上繼承自 SecurityExpressionRoot,只不過增加了過濾對象以及返回值對象。

1. SpEL 回顧

在 Spring Security 中,@PreAuthorize、@PostAuthorize 等注解都是支持 SpEL 表達式的。

在 SpEL 表達式中,如果上來就直接寫要執行的方法名,那么就說明這個方法是 RootObject 對象中的方法,如果要執行其他對象的方法,那么還需要寫上對象的名字,例如如下兩個例子:

@PreAuthorize("hasAuthority('system:user:add')")
public String add() {
return "add";
}

上面這個例子中,表達式中的方法是 hasAuthority,沒有寫對象名,那么就說明這個方法是 SpEL 中 RootObject 對象中的方法。

@PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
@GetMapping("/list")
public TableDataInfo list(SysOperLog operLog) {
startPage();
List<SysOperLog> list = operLogService.selectOperLogList(operLog);
return getDataTable(list);
}

上面這個例子中,權限注解中的表達式方法是 @ss.hasPermi('monitor:operlog:list'),其中 ss 是指 Spring 容器中的一個對象名,hasPermi 則是這個對象中的方法。

好啦,經過前面文章的學習,這些基本知識大家都已經掌握了。

2. 如何自定義

其實上面給出來的第二個例子就是一個自定義的例子。

不過,這種自定義方式太自由了,自由到沒有在 Spring Security 架構內完成這件事。所以,今天我想和小伙伴們聊一聊,如何在不使用第三方對象的情況下,來自定義一個權限判斷的表達式。

首先小伙伴們知道,我們在 @PreAuthorize 注解中使用的不用加對象名就能調用的權限方法,如 hasAuthority、hasPermission、hasRole、hasAnyRole 等,基本上都是由 SecurityExpressionRoot 及其子類提供的,準確來說是由 MethodSecurityExpressionRoot 類提供的。

MethodSecurityExpressionRoot 類實際上繼承自 SecurityExpressionRoot,只不過增加了過濾對象以及返回值對象。我們來看下 MethodSecurityExpressionRoot 的方法摘要:

圖片

再來看看 SecurityExpressionRoot 中的方法:

圖片

這些就是 RootObject 對象中的所有方法了,也是我們能夠在 @PreAuthorize 注解中使用的所有方法了。

那么現在想在已有方法上繼續擴展新方法,那么我們可以通過自定義類繼承自 SecurityExpressionRoot 對象,擴展這個 RootObject 對象,在該對象中繼續添加新的方法,進而實現自定義權限表達式。

好啦,說干就干,開搞!

本文的案例在前文的基礎上繼續完成,所以這里我就不從頭開始寫了。

3. 自定義 ExpressionRoot

首先我們自定義一個類繼承自 SecurityExpressionRoot 并實現 MethodSecurityExpressionOperations 接口(本來直接繼承自 MethodSecurityExpressionRoot 即可,但是因為這個類不是 public 的,沒法繼承,所以我們就實現 MethodSecurityExpressionOperations 接口即可):

public class CustomSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {

private Object filterObject;
private Object returnObject;
private AntPathMatcher antPathMatcher = new AntPathMatcher();

/**
* Creates a new instance
*
* @param authentication the {@link Authentication} to use. Cannot be null.
*/
public CustomSecurityExpressionRoot(Authentication authentication) {
super(authentication);
}

/**
* 判斷當前對象是否具備某一個權限
* @param permission
* @return
*/
public boolean hasPermission(String permission) {
//獲取當前登錄用戶所具有的權限
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority authority : authorities) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
return true;
}
}
return false;
}

/**
* 是否具備多個權限中的任意一個權限
* @param permissions
* @return
*/
public boolean hasAnyPermissions(String... permissions) {
if (permissions == null || permissions.length == 0) {
return false;
}
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority authority : authorities) {
for (String permission : permissions) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
return true;
}
}
}
return false;
}

public boolean hasAllPermissions:(String... permissions) {
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
if (permissions == null || permissions.length == 0) {
return false;
}
for (String permission : permissions) {
boolean flag = false;
for (GrantedAuthority authority : authorities) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
flag = true;
}
}
if (!flag) {
return false;
}
}
return true;
}

@Override
public void setFilterObject(Object filterObject) {
this.filterObject = filterObject;
}

@Override
public Object getFilterObject() {
return filterObject;
}

@Override
public void setReturnObject(Object returnObject) {
this.returnObject = returnObject;
}

@Override
public Object getReturnObject() {
return returnObject;
}

@Override
public Object getThis() {
return this;
}
}

加了 @Override 注解的方法,都是普普通通的常規方法,沒啥好說的。我們自己主要實現了三個方法,分別是:

  • hasPermission:判斷當前用戶是否具備某一個給定的權限。
  • hasAnyPermissions:判斷當前用戶是否具備給定的多個權限中的某一個。
  • hasAllPermissions:判斷當前用戶是否具備所有的給定的權限。

這里邊的邏輯我就不啰嗦了,都是基本的 Java 語法而已。

另外,用 AntPathMatcher 做比對是為了支持通配符,這個在上篇文章中已經說過了,這里不再贅述。

Spring Security 中,MethodSecurityExpressionRoot 的配置是通過 DefaultMethodSecurityExpressionHandler 來完成的,現在我們自定義了 CustomSecurityExpressionRoot,那也得有一個 Handler 來配置 CustomSecurityExpressionRoot,所以,再來一個類繼承自 DefaultMethodSecurityExpressionHandler,如下:

public class CustomMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {
@Override
protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, MethodInvocation invocation) {
CustomSecurityExpressionRoot root = new CustomSecurityExpressionRoot(authentication);
root.setTrustResolver(getTrustResolver());
root.setPermissionEvaluator(getPermissionEvaluator());
root.setRoleHierarchy(getRoleHierarchy());
return root;
}
}

在 createSecurityExpressionRoot 方法中創建一個 CustomSecurityExpressionRoot 對象,對象的 TrustResolver、權限評估器以及角色層級等,統統都用默認的方案即可。

配置完成后,再配置一下 CustomMethodSecurityExpressionHandler 這個 Bean 即可,如下:

@Bean
CustomMethodSecurityExpressionHandler customMethodSecurityExpressionHandler() {
return new CustomMethodSecurityExpressionHandler();
}

好啦,這就注入成功了。

接下來,我們就可以在權限注解中使用這個自定義的方法了:

@PreAuthorize("hasPermission('system:user:add')")
public String add() {
return "add";
}

這個自定義權限表達式的思路,說到底還是在 Spring Security 體系中玩,個人感覺這種方式更合理一些。

責任編輯:武曉燕 來源: 江南一點雨
相關推薦

2024-12-25 15:09:38

Python字符串函數

2024-03-25 13:46:12

C#Lambda編程

2019-12-02 21:29:45

Keras神經網絡TensorFlow

2021-07-01 11:07:49

Swift 自定義操作符

2022-06-21 14:18:06

RBACTienChin項目

2014-01-05 17:41:09

PostgreSQL表達式

2021-02-20 11:40:35

SpringBoot占位符開發技術

2009-07-03 18:20:45

VSTS 2010網絡

2021-05-25 09:18:04

正則表達式Linux字符串

2013-06-27 11:10:01

iOS開發自定義UISlider

2009-09-09 13:01:33

LINQ Lambda

2009-09-11 12:32:33

LINQ表達式

2009-09-15 15:18:00

Linq Lambda

2011-10-28 16:34:13

LINQ

2009-07-03 18:31:04

JSP表達式

2009-09-10 15:35:07

LINQ查詢表達式

2022-12-05 09:31:51

接口lambda表達式

2016-07-29 15:13:00

awk文本處理工具編程

2021-06-08 07:48:26

lambda表達式編譯器

2013-03-28 16:59:56

Android開發自定義TitleBar
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人免费视频网站在线观看 | 国产免费a| 在线播放一区二区三区 | 我要看黄色录像一级片 | 日韩av福利在线观看 | 一区二区三区国产 | 国内久久 | 国产精品99久久久久久www | 亚洲美乳中文字幕 | 精品一区在线 | 精品欧美一区二区三区免费观看 | 久久精品网 | 国产成人在线看 | 国产精品久久久久久久久久久久 | 精品在线视频播放 | 一级片av| 日韩不卡一二区 | 亚洲在线一区 | 久久久久久久综合 | 成人在线精品 | 中文字幕一区二区三区四区五区 | 麻豆成人在线视频 | 亚洲综合视频一区 | 成年人免费看 | 特黄特色大片免费视频观看 | 91精品国产综合久久精品图片 | 亚洲精品乱码久久久久久按摩观 | av黄在线观看 | 免费h在线 | 亚洲免费大片 | 精品视频在线观看 | 久久福利网站 | 最新中文字幕在线 | 久久美女网| 欧美激情在线精品一区二区三区 | 亚洲国产一区二区视频 | 国产精品成人在线播放 | 欧美淫| 国产日韩欧美精品一区二区三区 | 国产亚洲成av人片在线观看桃 | 亚洲精品无|