Shiro @RequiresPermissions不生效原因及解決方案
Shiro @RequiresPermissions不生效原因
原因一、AOP不生效導(dǎo)致
檢查maven,添加AOP依賴:
<!-- AOP依賴,必須,否則shiro權(quán)限攔截驗證不生效 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>檢查代碼,開啟了注解支持
/**
* Shiro生命周期處理器
*/
@Bean(name = "lifecycleBeanPostProcessor")
public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
/**
* 開啟Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP掃描使用Shiro注解的類,并在必要時進(jìn)行安全邏輯驗證
*/
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
advisorAutoProxyCreator.setProxyTargetClass(true);
return advisorAutoProxyCreator;
}
/**
* 開啟Shiro-aop注解支持
*/
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}原因二、@RequiresPermissions 寫法含有(冒號: 逗號, 星號*)
源碼校驗權(quán)限的時候,會調(diào)用
org.apache.shiro.realm.AuthorizingRealm#isPermitted(org.apache.shiro.authz.Permission, org.apache.shiro.authz.AuthorizationInfo)
源碼:
org.apache.shiro.authz.permission.WildcardPermission#implies
public boolean implies(Permission p) {
// By default only supports comparisons with other WildcardPermissions
if (!(p instanceof WildcardPermission)) {
return false;
}
WildcardPermission wp = (WildcardPermission) p;
List<Set<String>> otherParts = wp.getParts();
int i = 0;
for (Set<String> otherPart : otherParts) {
// If this permission has less parts than the other permission, everything after the number of parts contained
// in this permission is automatically implied, so return true
if (getParts().size() - 1 < i) {
return true;
} else {
Set<String> part = getParts().get(i);
if (!part.contains(WILDCARD_TOKEN) && !part.containsAll(otherPart)) {
return false;
}
i++;
}
}
// If this permission has more parts than the other parts, only imply it if all of the other parts are wildcards
for (; i < getParts().size(); i++) {
Set<String> part = getParts().get(i);
if (!part.contains(WILDCARD_TOKEN)) {
return false;
}
}
return true;
}含有(冒號: 逗號, 星號*)會前綴匹配放權(quán),
例如:
A用戶有 sys:role 的權(quán)限,那么 @RequiresPermissions("sys:role:add")對它來說,也可以。
就是 sys:role 擁有了 sys:role:* 的權(quán)限,不管 sys:role:add 還是 sys:role:delete 都無法攔截它。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
從java8升級java17的一些調(diào)整總結(jié)
在過去的幾年里,Java經(jīng)歷了多次版本升級,從JDK8到JDK17,每一次升級都帶來了許多新的特性和改進(jìn),這篇文章主要介紹了從java8升級java17的一些調(diào)整,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-03-03
Java 自定義Spring框架以及Spring框架的基本使用
Spring框架是由于軟件開發(fā)的復(fù)雜性而創(chuàng)建的。Spring使用的是基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅僅限于服務(wù)器端的開發(fā)2021-10-10
Springboot?Mybatis使用pageHelper如何實現(xiàn)分頁查詢
這篇文章主要介紹了Springboot?Mybatis使用pageHelper如何實現(xiàn)分頁查詢問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Java使用try-with-resources實現(xiàn)自動解鎖
項目中使用Redission分布式鎖,每次使用都需要顯示的解鎖,很麻煩,Java 提供了 try-with-resources 語法糖,它不僅可以用于自動關(guān)閉流資源,還可以用于實現(xiàn)自動解鎖,本文將介紹如何利用 try-with-resources 實現(xiàn)鎖的自動釋放,需要的朋友可以參考下2025-01-01

