shiro驗證權限方式一種是基於url配置文件:
例如:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <!-- 登錄頁面 ,用戶 登錄不成功自動 返回該頁面 --> <property name="loginUrl" value="/login"/> <!-- 登錄成功頁面,登錄成功后跳轉到該頁面 --> <property name="successUrl" value="/index"/> <!-- 無權訪問跳轉頁面 --> <property name="unauthorizedUrl" value="permNo"/> <!-- 自定義權限頁面設置url的訪問權限。anon表示不用驗證, 都可以訪問。anthc:authc filter 監聽,不登陸不能訪問。logout:logout filter監聽。 沒有列出的常用配置:perms["remote:invoke"] :需要角色romote 和權限invoke才能訪問。roles["admin"]需要角色admin才能訪問。設置可用“,”隔開, 如:/admin/test = authc,roles[admin] --> <property name="filterChainDefinitions"> <value> <!-- 無參,表示需認證才能使用 --> /home=authc /resources/**=anon </value> </property> </bean>
另外一種是基於注解:
例如:
RequiresAuthentication注解
RequiresAuthentication注解要求在訪問或調用被注解的類/實例/方法時,Subject在當前的session中已經被驗證。
@RequiresAuthentication public void updateAccount(Account userAccount) { //this method will only be invoked by a //Subject that is guaranteed authenticated ... }
RequiresGuest注解
RequiresGuest注解要求當前Subject是一個“訪客”,也就是,在訪問或調用被注解的類/實例/方法時,他們沒有被認證或者在被前一個Session記住。
@RequiresGuest public void signUp(User newUser) { //this method will only be invoked by a //Subject that is unknown/anonymous ... }
RequiresPermissions 注解
RequiresPermissions 注解要求當前Subject在執行被注解的方法時具備一個或多個對應的權限。
@RequiresPermissions("account:create") public void createAccount(Account account) { //this method will only be invoked by a Subject //that is permitted to create an account ... }
RequiresRoles 注解
RequiresPermissions 注解要求當前Subject在執行被注解的方法時具備所有的角色,否則將拋出AuthorizationException異常。
@RequiresRoles("administrator") public void deleteUser(User user) { //this method will only be invoked by an administrator ... }
如果在Controller中如果直接使用上面標簽是不起作用的,需要開啟shiro注解
bean id="myRealm" class="com.controller.MyRealm"/>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"/> </bean> <!--========================-如果使用注解方式驗證將下面代碼放開===============================--> <!-- 保證實現了Shiro內部lifecycle函數的bean執行 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <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> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!--登錄--> <prop key="org.apache.shiro.authz.UnauthenticatedException"> redirect:/login </prop> <!--授權--> <prop key="org.apache.shiro.authz.UnauthorizedException"> redirect:/admin/common/exceptionLog </prop> </props> </property> <property name="defaultErrorView" value="error/genericView"/> </bean>
其中com.controller.MyRealm類是我自定義的繼承自AuthorizingRealm的類