1. 程式人生 > >shiro框架的四中許可權控制方式

shiro框架的四中許可權控制方式

一.在自定義的realm中進行許可權控制

  在applicationContext.xml檔案中新增  /areaAction_pageQuery.action = perms["area"] 

複製程式碼

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 注入shiro框架核心物件,安全管理器 -->
        <property name="securityManager" ref="securityManager"/>
        <!--private String loginUrl;登入頁面
               private String successUrl;登入成功後跳轉頁面
               private String unauthorizedUrl;許可權不足時的提示頁面-->
         <property name="loginUrl" value="/login.html"/>
         <property name="successUrl" value="/index.html"/>
         <property name="unauthorizedUrl" value="/unauthorized.html"/>
         <!-- 指定URL攔截規則 -->
         <property name="filterChainDefinitions">
             <!--authc:代表shiro框架提供的一個過濾器,這個過濾器用於判斷當前使用者是否已經完成認證,
                         如果當前使用者已經認證,就放行,如果當前使用者沒有認證,跳轉到登入頁面
                 anon:代表shiro框架提供的一個過濾器,允許匿名訪問-->
             <value>
                 /css/* = anon
                 /images/* = anon
                 /js/** = anon
                 /validatecode.jsp* = anon
                 /userAction_login.action = anon
                 /areaAction_pageQuery.action = perms["area"]
                 /** = authc
             </value>
         </property>
    </bean>

複製程式碼

  此時訪問areaAction_pageQuery.action是頁面不會查詢到資料,須要為使用者授權

  在自定義realm中為使用者授權

複製程式碼

@Override
    /**
     * 授權方法
     */
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //為使用者授權,只需將使用者的許可權新增info中即可
        info.addStringPermission("area");
        return info;
    }

複製程式碼

二.使用shiro的方法註解為使用者授權

  1.在spring配置檔案applicationContext.xml中配置開啟shiro註解支援

複製程式碼

<!-- 基於spring自動代理方式為service建立代理物件,實現許可權控制 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
        <!-- 強制使用cglibdaili -->
        <property name="proxyTargetClass" value="true"></property>
    </bean>
    <!-- 配置切面 -->
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"></property>
    </bean>

複製程式碼

  2.配置事物註解,強制使用cglib代理

<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

  3.在service上配置註解

複製程式碼

 1 @RequiresPermissions("courier:delete")
 2     public void deleteBatch(String ids) {
 3         //判斷是否為空
 4         if(StringUtils.isNoneBlank(ids)){
 5             String[] idsArrays = ids.split(",");
 6             for (String id : idsArrays) {
 7                 Integer courierid = Integer.parseInt(id);
 8                 dao.deleteCourier(courierid);
 9             }
10         }
11     }

複製程式碼

三.使用shiro標籤進行許可權控制

  1.在jsp頁面中引入shiro標籤庫

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

  2.在頁面中使用標籤

複製程式碼

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <!-- 判斷當前使用者是否已經認證,已認證就可以看到標籤中的內容 -->
    <shiro:authenticated>
        看到內容就說明你已經認證成功了!
    </shiro:authenticated>
    
    <br>
    
    <!-- 判斷當前使用者是否擁有指定的許可權 -->
    <shiro:hasPermission name="area">
        <input value="這是判斷許可權的按鈕">
    </shiro:hasPermission>
    
    <br>
    
    <!-- 判斷當前使用者是否擁有指定的角色 -->
    <shiro:hasRole name="admin">
        <input value="這是判斷角色的按鈕">
    </shiro:hasRole>
</body>
</html>

複製程式碼

四.程式設計方式實現使用者許可權控制(瞭解)