filter實現權限控制

我AOP的設計理念在軟件開發中的應用愈來愈普遍,這不是一個高大上的東西,而是每一個程序員都應該熟知的一個東西。由於它方便的就是咱們程序員。使用AOP,咱們能夠專一於邏輯代碼的編寫,將那些系統功能統一交給AOP框架去管理,在運行時自動耦合起來。

當咱們訪問URL頁面時,好比A能夠瀏覽全部頁面。B只能夠瀏覽一部分頁面,若是沒有一個統一的權限控制,只要URL地址正確,你們均可以訪問。這樣就沒權限控制可言了。因此在訪問頁面中以前,咱們先去自動執行我寫的權限判斷。

具體知道我要幹什麼了,那麼怎麼實現呢?

  我自定義了一個filter——AuthAttribute。

一、假如我要執行以下這個Controller下的Action。這個Controller和Action都是你們本身編寫的,這裏只是個實例。
php

  1. namespace ITOO.BasicPlaceClient.Controllers java

     


  2. 程序員

     


  3.  

     

    //控制器類,繼承了Controllers web

     


  4.  

     


  5.  

     

    public class MyController : Controller 緩存

     


  6.  

     

    框架

     


  7.  

     


  8.  

     

     

     

     

     

    public ActionResult Index() ide

     


  9.  

     

     

     

     

     

    學習

     


  10.  

     

     

     

     

     

     

     

    return View(); ui

     


  11.  

     

     

     

     

     

    spa

     


  12.  

     

     


  13.  

複製代碼

二、在執行以前,先進行權限判斷,執行我寫的這個自定義的filter。在這裏,咱們會取出你簡要訪問的Controller和Action,在緩存了取出你所擁有的訪問權限。在這裏判斷你是否有訪問該Controller裏的Action的權限。沒有權限,直接給出友好提示,你沒有權限。嘿嘿,還算是友好吧。可是你要是擁有權限,他會繼續執行你要訪問的Action,將你想要見到的頁面呈現給您。

  1. using System; 

     


  2. using System.Collections.Generic; 

     


  3. using System.Linq; 

     


  4. using System.Web; 

     


  5. using System.Web.Mvc; 

     


  6. using ITOO.Library.Core.Memcache; 

     


  7. using System.Collections; 

     


  8.  

     


  9. namespace ITOO.BasicPlaceClient.Controllers.Attribute 

     


  10.  


  11.  

     

    ///  

     


  12.  

     

    /// ActionFilterAttribute是Action過濾類,該屬於會在執行一個action以前先執行.而ActionFilterAttribute是 MVC的一個專門處理action過濾的類.基於這個原理作的一個權限限制。 

     


  13.  

     

    ///  

     


  14.  

     

    public class AuthAttribute : ActionFilterAttribute 

     


  15.  

     

     


  16.  

     

     

     

     

     

    ///  

     


  17.  

     

     

     

     

     

    /// 在執行操做方法以前由 ASP.NET MVC 框架調用 

     


  18.  

     

     

     

     

     

    ///  

     


  19.  

     

     

     

     

     

    ///  

     


  20.  

     

     

     

     

     

    public override void OnActionExecuting(ActionExecutingContext filterContext) 

     


  21.  

     

     

     

     

     

     


  22.  

     

     

     

     

     

     

     

    ////獲取 controllerName 名稱 

     


  23.  

     

     

     

     

     

     

     

    var controllerName = filterContext.RouteData.Values["Controller"].ToString(); 

     


  24.  

     


  25.  

     

     

     

     

     

     

     

    ///獲取你將要執行的Action的域名 

     


  26.  

     

     

     

     

     

     

     

    var actionName = HttpContext.Current.Request.RequestContext.RouteData.Values["Action"].ToString(); 

     


  27.  

     


  28.  

     

     

     

     

     

     

     

    Guid selfGuid1 = Guid.NewGuid();//申請了一個模擬的GUID 

     


  29.  

     

     

     

     

     

     

     

    Guid selfGuid2 = Guid.NewGuid();//申請了一個模擬的GUID 

     


  30.  

     


  31.  

     

     

     

     

     

     

     

    MemcacheHelper.Add(selfGuid1.ToString(), "QueryBed", DateTime.Now.AddMinutes(20)); 

     

    //Controller存入緩存 

     


  32.  

     

     

     

     

     

     

     

    MemcacheHelper.Add(selfGuid2.ToString(), "Index", DateTime.Now.AddMinutes(20)); 

     

    //Action存入緩存 

     


  33.  

     


  34.  

     

     

     

     

     

     

     

    //建立一個List集合 

     


  35.  

     

     

     

     

     

     

     

    List guids1 = new List(); 

     


  36.  

     


  37.  

     

     

     

     

     

     

     

    //將緩存裏取出的key值存放到List裏 

     


  38.  

     

     

     

     

     

     

     

    guids1.Add(selfGuid1.ToString()); 

     


  39.  

     

     

     

     

     

     

     

    guids1.Add(selfGuid2.ToString()); 

     


  40.  

     


  41.  

     

     

     

     

     

     

     

    //建立數據字典getkey對象 

     


  42.  

     

     

     

     

     

     

     

    IDictionary getkey = new Dictionary(); 

     


  43.  

     


  44.  

     

     

     

     

     

     

     

    //獲取一組緩存 

     


  45.  

     

     

     

     

     

     

     

    getkey = MemcacheHelper.Get(guids1); 

     


  46.  

     


  47.  

     

     

     

     

     

     

     

    //驗證權限,先驗證Controller 

     

     

     

     

     


  48.  

     

     

     

     

     

     

     

    foreach (KeyValuePair kvp in getkey) 

     


  49.  

     

     

     

     

     

     

     

     


  50.  

     

     

     

     

     

     

     

     

     

    //若是有將要訪問的Controller的權限 

     


  51.  

     

     

     

     

     

     

     

     

     

    if (kvp.Value.ToString() == controllerName) 

     


  52.  

     

     

     

     

     

     

     

     

     

     

     

     

     

    //若是有將要訪問的Acting的權限 

     


  53.  

     

     

     

     

     

     

     

     

     

     

     

     

     

    foreach (KeyValuePair kvp1 in getkey) 

     


  54.  

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    if (kvp1.Value.ToString() == actionName) 

     


  55.  

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     


  56.  

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    //所有經過,則存在訪問將要訪問的Controller下的Action 

     


  57.  

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    return; 

     


  58.  

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     


  59.  

     

     

     

     

     

     

     

     


  60.  

     

     

     

     

     

     

     

    //沒有權限,驗證不經過 

     


  61.  

     

     

     

     

     

     

     

    ContentResult Content = new ContentResult(); 

     


  62.  

     

     

     

     

     

     

     

    Content.Content = "

  63.  

     

     

     

     

     

     

     

    //執行結果爲權限不經過 

     


  64.  

     

     

     

     

     

     

     

    filterContext.Result = Content; 

     


  65.  

     

     

     

     

     

     


  66.  

     


  67.  

     

     


  68.  

複製代碼

這是權限判斷的代碼。在使用它以前,咱們要在Global裏面的RegisterGlobalFilters進行註冊。不然在方法執行以前是不會執行這段代碼的。

三、註冊:

  1. filters.Add(new AuthAttribute()); 

     

複製代碼

   這樣,就實現了一個簡單的權限控制。技術淺薄,就寫成這樣了,有什麼不對的你們互相交流。

 

 

更多java學習 web框架學習 http://techfoxbbs.com

相關文章
相關標籤/搜索