使用前請先開啓Shiro的controller層註解,若是已經設置請下滑繞過spring
要在spring-mvc.xml中寫。apache
<!--下面的用於開啓shiro的權限註解--> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass" value="true"/> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean>
若是在是springboot中spring-mvc
/** * 下面2個支持controller層註解實現權限控制 * * @return */ @Bean(name = "advisorAutoProxyCreator") public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() { DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); advisorAutoProxyCreator.setProxyTargetClass(true); return advisorAutoProxyCreator; } @Bean(name = "authorizationAttributeSourceAdvisor") public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) { AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; }
———————————————————進入正題———————————————————————springboot
@RequiresAuthenticationmvc
驗證用戶是否登陸,等同於方法subject.isAuthenticated() 結果爲true時。ui
@RequiresUserspa
驗證用戶是否被記憶,user有兩種含義:.net
一種是成功登陸的(subject.isAuthenticated() 結果爲true);code
另一種是被記憶的(subject.isRemembered()結果爲true)。xml
@RequiresGuest
驗證是不是一個guest的請求,與@RequiresUser徹底相反。
換言之,RequiresUser == !RequiresGuest。
此時subject.getPrincipal() 結果爲null.
@RequiresRoles
例如:@RequiresRoles("aRoleName");
void someMethod();
若是subject中有aRoleName角色才能夠訪問方法someMethod。若是沒有這個權限則會拋出異常AuthorizationException。
@RequiresPermissions
例如: @RequiresPermissions({"file:read", "write:aFile.txt"} )
void someMethod();
要求subject中必須同時含有file:read和write:aFile.txt的權限才能執行方法someMethod()。不然拋出異常AuthorizationException。--------------------- 原文:https://blog.csdn.net/anmoyyh/article/details/74742772