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