Java中的@PreAuthorize注解使用詳解
@PreAuthorize注解使用
@PreAuthorize注解會在方法執(zhí)行前進行權限驗證,支持Spring EL表達式,它是基于方法注解的權限解決方案。只有當@EnableGlobalMethodSecurity(prePostEnabled=true)的時候,@PreAuthorize才可以使用,@EnableGlobalMethodSecurity注解在SPRING安全中心進行設置,如下:
/** * 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)); }
自定義權限實現
@PreAuthorize("@pms.hasPermission('archsys_sysarchreport_view')" )
- pms是一個注冊在 Spring容器中的Bean,對應的類是cn.hadoopx.framework.web.service.PermissionService;
- hasPermission是PermissionService類中定義的方法;
- 當Spring EL 表達式返回TRUE,則權限校驗通過;
- PermissionService.java的定義如下:
public class PermissionService { /** * 判斷接口是否有任意xxx,xxx權限 * @param permissions 權限 * @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 { /** * 鑒權具體的實現邏輯 * @return (#pms.xxx) */ @Bean("pms") public PermissionService permissionService() { return new PermissionService(); } }
到此這篇關于Java中的@PreAuthorize注解使用詳解的文章就介紹到這了,更多相關@PreAuthorize注解使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
高分面試從Hotspot源碼層面剖析java多態(tài)實現原理
這篇文章主要為大家介紹了在面試中從Hotspot源碼層面來剖析java多態(tài)的實現原理,這樣回答薪資隨你開,有需要的朋友可以借鑒參考下,希望大家多多加薪2022-01-01簡述springboot及springboot cloud環(huán)境搭建
這篇文章主要介紹了簡述springboot及springboot cloud環(huán)境搭建的方法,包括spring boot 基礎應用環(huán)境搭建,需要的朋友可以參考下2017-07-07Java中l(wèi)ist.foreach()和list.stream().foreach()用法詳解
在Java中List是一種常用的集合類,用于存儲一組元素,List提供了多種遍歷元素的方式,包括使用forEach()方法和使用Stream流的forEach()方法,這篇文章主要給大家介紹了關于Java中l(wèi)ist.foreach()和list.stream().foreach()用法的相關資料,需要的朋友可以參考下2024-07-07詳解Spring 框架中切入點 pointcut 表達式的常用寫法
這篇文章主要介紹了詳解Spring 框架中切入點 pointcut 表達式的常用寫法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04Java多線程程序中synchronized修飾方法的使用實例
synchronized關鍵字主要北用來進行線程同步,這里我們主要來演示Java多線程程序中synchronized修飾方法的使用實例,需要的朋友可以參考下:2016-06-06