最近在學習springboot整和shiro中遇到的一個問題一直搞不懂,求大佬們救救孩子
描述:由于使用的是springboot整和的shiro,所以使用的是java類對shiro進行路徑攔截配置,昨天換成注解配置之后一直出現錯誤,錯誤如下:
```
org.apache.shiro.authz.AuthorizationException: Not authorized to invoke method: public java.lang.String com.example.shiro.web.UserController.addUser()
```
但詭異的情況在于使用java配置類的話就不會出現這個情況,這個錯誤最初我以為是因為使用注解時無法跳轉到我所指定的登錄路徑而造成的錯誤,但是經過今天DEBUG在原始碼中走流程的時候突然發現了問題:

這里沒有我要攔截的路徑,
下面是沒有問題的代碼:
```
/**
* 創建ShiroFilterFactoryBean
*/
@Bean(name = "shiroFilterFactoryBean")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
// 獲取ShiroFilterFactoryBean物件
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
// 1.設定安全管理器
shiroFilterFactoryBean.setSecurityManager(securityManager);
// 添加Shiro內置過濾器
/**
* Shiro內置過濾器,可以實作權限相關的攔截
* 常用過濾器:
* anon:無需認證(登錄)就可以訪問
* authc:必須認證(登錄)才可以訪問
* user:如果使用了rememberMe的功能可以直接訪問
* perms:該資源必須得到資源權限才可以訪問
* role:該資源必須得到角色權限才可以訪問
*/
Map<String,String> filterMap = new LinkedHashMap<String, String>();
/*filterMap.put("/add","authc");
filterMap.put("/del","authc");
filterMap.put("/updata","authc");*/
/*
* 授權過濾器,因為linkedHashMap是有序的,所以權限要由順序最好
* role 》perms 》user 》authc 》anon
*/
//filterMap.put("/user/update","perms[user:update]");
//filterMap.put("/user/add","perms[user:add]");
filterMap.put("/user/*","authc");
// 修改跳轉的登錄頁面
shiroFilterFactoryBean.setLoginUrl("../toLogin");
// 設定未授權提示頁面
shiroFilterFactoryBean.setUnauthorizedUrl("../unAuth");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
return shiroFilterFactoryBean;
}
```
而當我把 filterMap.put("/user/*","authc");注釋掉之后,在Conroller層設定注解時,就會出現這個問題
```
@Controller
@RequiresAuthentication
@RequestMapping("user")
public class UserController {
@RequiresAuthentication
@RequiresPermissions(value = {"user:add"})
@GetMapping("add")
public String addUser(){
return "user/add";
}
```
就會出現這個問題,網路上都是說因為AOP沒有啟用的原因,但是我這邊確實啟用的AOP功能
```
# 開啟AOP
spring.aop.proxy-target-class=true
spring.aop.auto=true
----------------------------------------------------------
@SpringBootApplication
@EnableAspectJAutoProxy
public class ShiroApplication {
public static void main(String[] args) {
SpringApplication.run(ShiroApplication.class, args);
}
}
------------------------------------------------------------
/**
* 開啟Shiro注解(如@RequiresRoles,@RequiresPermissions),
* 需借助SpringAOP掃描使用Shiro注解的類,并在必要時進行安全邏輯驗證
* 配置以下兩個bean(DefaultAdvisorAutoProxyCreator和AuthorizationAttributeSourceAdvisor)
*/
@Bean
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
advisorAutoProxyCreator.setProxyTargetClass(true);
return advisorAutoProxyCreator;
}
/**
* 開啟aop注解支持
*/
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
```
求大佬指點一下到底是因為什么會出現這個問題
求大佬們救救孩子把!!!!!!!!!!!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/32666.html
標籤:Java相關
上一篇:[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.1:
