android aop 权限检查,AOP简单拦截实现验证权限功能
普通的權限驗證一般都是寫一個方法,然后再執行方法之前檢查一下權限
這樣做的壞處是每個地方都需要修改加權限驗證
而用AOP的方式來做的話就很方便
網上找了一個例子,測試通過,感覺蠻好用的,記錄一下[AttributeUsage(AttributeTargets.All,?AllowMultiple?=?false,?Inherited?=?true)]
public?class?TaskInfo?:?Attribute
{
public?string?Name?{?get;?set;?}
public?string?Description?{?get;?set;?}
public?TaskInfo()?{?}
public?TaskInfo(string?name,?string?description)
{
this.Name?=?name;
this.Description?=?description;
}
}
//特性定義,用于 Consumer??? [AttributeUsage(AttributeTargets.Class)]??? public class PermissionCheckAttribute : ContextAttribute??? {??????? public PermissionCheckAttribute()??????????? : base("PermissionCheck")??????? { }??????? public override void GetPropertiesForNewContext(IConstructionCallMessage ccm)??????? {??????????? ccm.ContextProperties.Add(new PermissionCheckProperty());??????? }??? }
internal class SecurityAspect : IMessageSink??? {??????? private IMessageSink m_next;??????? internal SecurityAspect(IMessageSink next)??????? {??????????? m_next = next;??????? }??????? #region -- IMessageSink? --??????? public IMessageSink NextSink??????? {??????????? get { return m_next; }??????? }??????? public IMessage SyncProcessMessage(IMessage msg)??????? {??????????? Preprocess(msg);??????????? IMessage returnMethod = m_next.SyncProcessMessage(msg);??????????? return returnMethod;??????? }??????? public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)??????? {??????????? throw new NotImplementedException();??????? }??????? #endregion??????? #region --自定義的 AOP 方法--??????? private void Preprocess(IMessage msg)??????? {??????????? //只處理方法調用??????????? if (!(msg is IMethodMessage)) return;??????????? //獲取方法中定義的 Task 屬性,交給權限檢查類去檢查??????????? IMethodMessage call = msg as IMethodMessage;??????????? MethodBase mb = call.MethodBase;??????????? object[] attrObj = mb.GetCustomAttributes(typeof(TaskInfo), false);??????????? if (attrObj != null)??????????? {??????????????? TaskInfo attr = (TaskInfo)attrObj[0];??????????????? if (!string.IsNullOrEmpty(attr.Name))??????????????????? PowerHelper.PermissionCheck(attr.Name);??????????? }??????? } ??????? #endregion ??? }
public class PermissionCheckProperty : IContextProperty, IContributeObjectSink??? {??????? #region?? IContributeObjectSink 實現,將 AOP 類加入消息處理鏈??????? public IMessageSink GetObjectSink(MarshalByRefObject o, IMessageSink next)??????? {??????????? return new SecurityAspect(next);??????? }??????? #endregion??????? #region???? IContextProperty 實現??????? public string Name??????? {??????????? get { return "PermissionCheckProperty"; }??????? }??????? public void Freeze(Context newContext)??????? {??????? }??????? public bool IsNewContextOK(Context newCtx)??????? {??????????? return true;??????? }??????? #endregion??? }
public class PowerHelper??? {??????? public static void PermissionCheck(string taskName)??????? {??????????? if (HttpContext.Current != null)??????????? {??????????????? //此處做權限驗證??????????????? //用戶,角色等自由操作??????????????? if (HttpContext.Current.Session["user"] != null && HttpContext.Current.Session["user"] == "ysuhy")??????????????? {??????????????????? //擁有權限,正常?????????????????? ???????????????? }??????????????? else??????????????? {??????????????????? //沒有權限??????????????????? throw new UnauthorizedAccessException("訪問被拒絕,當前用戶不具有操作此功能的權限!");??????????????? } ??????????? } ??????? } ??? }
普通業務類方法??? [PermissionCheck()]??? public class ItemManager : ContextBoundObject??? {??????? [TaskInfo("AddItem", "增加")]??????? public void AddItem()??????? { ??????????? Console.WriteLine("執行增加");??????????? //...??????? } ??? }
調用protected void Page_Load(object sender, EventArgs e)??????? {??????????? Session["user"] = "ysuhy";??????????? ItemManager itemManager = new ItemManager();??????????? itemManager.AddItem();??????? }
總結
以上是生活随笔為你收集整理的android aop 权限检查,AOP简单拦截实现验证权限功能的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Power Bi:零售数据可视化
 - 下一篇: ios 桥接文件找不到文件_电脑文件搜索