Java中的@PreAuthorize注解使用詳解
@PreAuthorize注解使用
@PreAuthorize注解會在方法執(zhí)行前進行權(quán)限驗證,支持Spring EL表達式,它是基于方法注解的權(quán)限解決方案。只有當@EnableGlobalMethodSecurity(prePostEnabled=true)的時候,@PreAuthorize才可以使用,@EnableGlobalMethodSecurity注解在SPRING安全中心進行設(shè)置,如下:
/**
* SPRING安全中心
* @author ROCKY
*/
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}注解如何使用?
@Operation(summary = "通過id查詢檔案報表", description = "通過id查詢檔案報表")
@GetMapping("/{reportId}" )
@PreAuthorize("@pms.hasPermission('archsys_sysarchreport_view')" )
public R getById(@PathVariable("reportId" ) Long reportId) {
return R.ok(sysArchReportService.getById(reportId));
}自定義權(quán)限實現(xiàn)
@PreAuthorize("@pms.hasPermission('archsys_sysarchreport_view')" )
- pms是一個注冊在 Spring容器中的Bean,對應(yīng)的類是cn.hadoopx.framework.web.service.PermissionService;
- hasPermission是PermissionService類中定義的方法;
- 當Spring EL 表達式返回TRUE,則權(quán)限校驗通過;
- PermissionService.java的定義如下:
public class PermissionService {
/**
* 判斷接口是否有任意xxx,xxx權(quán)限
* @param permissions 權(quán)限
* @return {boolean}
*/
public boolean hasPermission(String... permissions) {
if (ArrayUtil.isEmpty(permissions)) {
return false;
}
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
return false;
}
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
return authorities.stream().map(GrantedAuthority::getAuthority).filter(StringUtils::hasText)
.anyMatch(x -> PatternMatchUtils.simpleMatch(permissions, x));
}
}@RequiredArgsConstructor
@EnableConfigurationProperties(PermitAllUrlProperties.class)
public class PigResourceServerAutoConfiguration {
/**
* 鑒權(quán)具體的實現(xiàn)邏輯
* @return (#pms.xxx)
*/
@Bean("pms")
public PermissionService permissionService() {
return new PermissionService();
}
}到此這篇關(guān)于Java中的@PreAuthorize注解使用詳解的文章就介紹到這了,更多相關(guān)@PreAuthorize注解使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
高分面試從Hotspot源碼層面剖析java多態(tài)實現(xiàn)原理
這篇文章主要為大家介紹了在面試中從Hotspot源碼層面來剖析java多態(tài)的實現(xiàn)原理,這樣回答薪資隨你開,有需要的朋友可以借鑒參考下,希望大家多多加薪2022-01-01
簡述springboot及springboot cloud環(huán)境搭建
這篇文章主要介紹了簡述springboot及springboot cloud環(huán)境搭建的方法,包括spring boot 基礎(chǔ)應(yīng)用環(huán)境搭建,需要的朋友可以參考下2017-07-07
Java中l(wèi)ist.foreach()和list.stream().foreach()用法詳解
在Java中List是一種常用的集合類,用于存儲一組元素,List提供了多種遍歷元素的方式,包括使用forEach()方法和使用Stream流的forEach()方法,這篇文章主要給大家介紹了關(guān)于Java中l(wèi)ist.foreach()和list.stream().foreach()用法的相關(guān)資料,需要的朋友可以參考下2024-07-07
詳解Spring 框架中切入點 pointcut 表達式的常用寫法
這篇文章主要介紹了詳解Spring 框架中切入點 pointcut 表達式的常用寫法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Java數(shù)據(jù)脫敏實現(xiàn)的方法總結(jié)
數(shù)據(jù)脫敏,指的是對某些敏感信息通過脫敏規(guī)則進行數(shù)據(jù)的變形,實現(xiàn)敏感隱私數(shù)據(jù)的可靠保護,本文主要是對后端數(shù)據(jù)脫敏實現(xiàn)的簡單總結(jié),希望對大家有所幫助2023-07-07
Java多線程程序中synchronized修飾方法的使用實例
synchronized關(guān)鍵字主要北用來進行線程同步,這里我們主要來演示Java多線程程序中synchronized修飾方法的使用實例,需要的朋友可以參考下:2016-06-06
Java設(shè)計模式之組合模式(Composite模式)介紹
這篇文章主要介紹了Java設(shè)計模式之組合模式(Composite模式)介紹,Composite定義:將對象以樹形結(jié)構(gòu)組織起來,以達成“部分-整體” 的層次結(jié)構(gòu),使得客戶端對單個對象和組合對象的使用具有一致性,需要的朋友可以參考下2015-03-03

