android aop 权限检查,AOP简单拦截实现验证权限功能
普通的權(quán)限驗(yàn)證一般都是寫一個(gè)方法,然后再執(zhí)行方法之前檢查一下權(quán)限
這樣做的壞處是每個(gè)地方都需要修改加權(quán)限驗(yàn)證
而用AOP的方式來(lái)做的話就很方便
網(wǎng)上找了一個(gè)例子,測(cè)試通過(guò),感覺(jué)蠻好用的,記錄一下[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)??????? {??????????? //只處理方法調(diào)用??????????? if (!(msg is IMethodMessage)) return;??????????? //獲取方法中定義的 Task 屬性,交給權(quán)限檢查類去檢查??????????? 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 實(shí)現(xiàn),將 AOP 類加入消息處理鏈??????? public IMessageSink GetObjectSink(MarshalByRefObject o, IMessageSink next)??????? {??????????? return new SecurityAspect(next);??????? }??????? #endregion??????? #region???? IContextProperty 實(shí)現(xiàn)??????? 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)??????????? {??????????????? //此處做權(quán)限驗(yàn)證??????????????? //用戶,角色等自由操作??????????????? if (HttpContext.Current.Session["user"] != null && HttpContext.Current.Session["user"] == "ysuhy")??????????????? {??????????????????? //擁有權(quán)限,正常?????????????????? ???????????????? }??????????????? else??????????????? {??????????????????? //沒(méi)有權(quán)限??????????????????? throw new UnauthorizedAccessException("訪問(wèn)被拒絕,當(dāng)前用戶不具有操作此功能的權(quán)限!");??????????????? } ??????????? } ??????? } ??? }
普通業(yè)務(wù)類方法??? [PermissionCheck()]??? public class ItemManager : ContextBoundObject??? {??????? [TaskInfo("AddItem", "增加")]??????? public void AddItem()??????? { ??????????? Console.WriteLine("執(zhí)行增加");??????????? //...??????? } ??? }
調(diào)用protected void Page_Load(object sender, EventArgs e)??????? {??????????? Session["user"] = "ysuhy";??????????? ItemManager itemManager = new ItemManager();??????????? itemManager.AddItem();??????? }
總結(jié)
以上是生活随笔為你收集整理的android aop 权限检查,AOP简单拦截实现验证权限功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Power Bi:零售数据可视化
- 下一篇: ios 桥接文件找不到文件_电脑文件搜索