我AOP的设计理念在软件开发中的应用越来越广泛,这不是一个高大上的东西,而是每个程序员都应该熟知的一个东西。因为它方便的就是我们程序员。使用AOP,我们可以专注于逻辑代码的编写,将那些系统功能统一交给AOP框架去管理,在运行时自动耦合起来。

当我们访问URL页面时,比如A可以浏览所有页面。B只可以浏览一部分页面,如果没有一个统一的权限控制,只要URL地址正确,大家都可以访问。这样就没权限控制可言了。所以在访问页面中之前,我们先去自动执行我写的权限判断。

具体知道我要干什么了,那么怎么实现呢?

  我自定义了一个filter——AuthAttribute。

1、假如我要执行如下这个Controller下的Action。这个Controller和Action都是大家自己编写的,这里只是个实例。

  1. namespace ITOO.BasicPlaceClient.Controllers 

     


  2.  


  3.  

     

    //控制器类,继承了Controllers 

     


  4.  

     


  5.  

     

    public class MyController : Controller 

     


  6.  

     

     


  7.  

     


  8.  

     

     

     

     

     

    public ActionResult Index() 

     


  9.  

     

     

     

     

     

     


  10.  

     

     

     

     

     

     

     

    return View(); 

     


  11.  

     

     

     

     

     

     


  12.  

     

     


  13.  

复制代码

2、在执行之前,先进行权限判断,执行我写的这个自定义的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进行注册。否则在方法执行之前是不会执行这段代码的。

3、注册:

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

     

复制代码

   这样,就实现了一个简单的权限控制。技术浅薄,就写成这样了,有什么不对的大家互相交流。

 

 

更多java学习 web框架学习 http://techfoxbbs.com