日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
如何在TienChin項(xiàng)目中自定義權(quán)限表達(dá)式

1. SpEL 回顧

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

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、網(wǎng)絡(luò)空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、揭陽(yáng)網(wǎng)站維護(hù)、網(wǎng)站推廣。

在 SpEL 表達(dá)式中,如果上來(lái)就直接寫(xiě)要執(zhí)行的方法名,那么就說(shuō)明這個(gè)方法是 RootObject 對(duì)象中的方法,如果要執(zhí)行其他對(duì)象的方法,那么還需要寫(xiě)上對(duì)象的名字,例如如下兩個(gè)例子:

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

上面這個(gè)例子中,表達(dá)式中的方法是 hasAuthority,沒(méi)有寫(xiě)對(duì)象名,那么就說(shuō)明這個(gè)方法是 SpEL 中 RootObject 對(duì)象中的方法。

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

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

好啦,經(jīng)過(guò)前面文章的學(xué)習(xí),這些基本知識(shí)大家都已經(jīng)掌握了。

2. 如何自定義

其實(shí)上面給出來(lái)的第二個(gè)例子就是一個(gè)自定義的例子。

不過(guò),這種自定義方式太自由了,自由到?jīng)]有在 Spring Security 架構(gòu)內(nèi)完成這件事。所以,今天我想和小伙伴們聊一聊,如何在不使用第三方對(duì)象的情況下,來(lái)自定義一個(gè)權(quán)限判斷的表達(dá)式。

首先小伙伴們知道,我們?cè)?@PreAuthorize 注解中使用的不用加對(duì)象名就能調(diào)用的權(quán)限方法,如 hasAuthority、hasPermission、hasRole、hasAnyRole 等,基本上都是由 SecurityExpressionRoot 及其子類(lèi)提供的,準(zhǔn)確來(lái)說(shuō)是由 MethodSecurityExpressionRoot 類(lèi)提供的。

MethodSecurityExpressionRoot 類(lèi)實(shí)際上繼承自 SecurityExpressionRoot,只不過(guò)增加了過(guò)濾對(duì)象以及返回值對(duì)象。我們來(lái)看下 MethodSecurityExpressionRoot 的方法摘要:

再來(lái)看看 SecurityExpressionRoot 中的方法:

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

那么現(xiàn)在想在已有方法上繼續(xù)擴(kuò)展新方法,那么我們可以通過(guò)自定義類(lèi)繼承自 SecurityExpressionRoot 對(duì)象,擴(kuò)展這個(gè) RootObject 對(duì)象,在該對(duì)象中繼續(xù)添加新的方法,進(jìn)而實(shí)現(xiàn)自定義權(quán)限表達(dá)式。

好啦,說(shuō)干就干,開(kāi)搞!

本文的案例在前文的基礎(chǔ)上繼續(xù)完成,所以這里我就不從頭開(kāi)始寫(xiě)了。

3. 自定義 ExpressionRoot

首先我們自定義一個(gè)類(lèi)繼承自 SecurityExpressionRoot 并實(shí)現(xiàn) MethodSecurityExpressionOperations 接口(本來(lái)直接繼承自 MethodSecurityExpressionRoot 即可,但是因?yàn)檫@個(gè)類(lèi)不是 public 的,沒(méi)法繼承,所以我們就實(shí)現(xiàn) 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);
}

/**
* 判斷當(dāng)前對(duì)象是否具備某一個(gè)權(quán)限
* @param permission
* @return
*/
public boolean hasPermission(String permission) {
//獲取當(dāng)前登錄用戶(hù)所具有的權(quán)限
Collection authorities = authentication.getAuthorities();
for (GrantedAuthority authority : authorities) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
return true;
}
}
return false;
}

/**
* 是否具備多個(gè)權(quán)限中的任意一個(gè)權(quán)限
* @param permissions
* @return
*/
public boolean hasAnyPermissions(String... permissions) {
if (permissions == null || permissions.length == 0) {
return false;
}
Collection 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 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 注解的方法,都是普普通通的常規(guī)方法,沒(méi)啥好說(shuō)的。我們自己主要實(shí)現(xiàn)了三個(gè)方法,分別是:

  • hasPermission:判斷當(dāng)前用戶(hù)是否具備某一個(gè)給定的權(quán)限。
  • hasAnyPermissions:判斷當(dāng)前用戶(hù)是否具備給定的多個(gè)權(quán)限中的某一個(gè)。
  • hasAllPermissions:判斷當(dāng)前用戶(hù)是否具備所有的給定的權(quán)限。

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

另外,用 AntPathMatcher 做比對(duì)是為了支持通配符,這個(gè)在上篇文章中已經(jīng)說(shuō)過(guò)了,這里不再贅述。

Spring Security 中,MethodSecurityExpressionRoot 的配置是通過(guò) DefaultMethodSecurityExpressionHandler 來(lái)完成的,現(xiàn)在我們自定義了 CustomSecurityExpressionRoot,那也得有一個(gè) Handler 來(lái)配置 CustomSecurityExpressionRoot,所以,再來(lái)一個(gè)類(lèi)繼承自 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 方法中創(chuàng)建一個(gè) CustomSecurityExpressionRoot 對(duì)象,對(duì)象的 TrustResolver、權(quán)限評(píng)估器以及角色層級(jí)等,統(tǒng)統(tǒng)都用默認(rèn)的方案即可。

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

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

好啦,這就注入成功了。

接下來(lái),我們就可以在權(quán)限注解中使用這個(gè)自定義的方法了:

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

這個(gè)自定義權(quán)限表達(dá)式的思路,說(shuō)到底還是在 Spring Security 體系中玩,個(gè)人感覺(jué)這種方式更合理一些。


當(dāng)前名稱(chēng):如何在TienChin項(xiàng)目中自定義權(quán)限表達(dá)式
轉(zhuǎn)載源于:http://m.5511xx.com/article/cccdsoc.html