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();        }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/475040.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

COJ 1081 集训队分组

http://acm.csu.edu.cn/OnlineJudge/problem.php?id1081 不是太会写拓扑排序&#xff0c;所以再写一遍。 /*Accepted 880 kb 268 ms C/ 1671 B 2012-07-29 17:08:30*/ #include<cstdio> #include<cstring> #include<cstdlib>const int MAXN …

LeetCode 132. 分割回文串 II(DP)

文章目录1. 题目2. 解题1. 题目 给定一个字符串 s&#xff0c;将 s 分割成一些子串&#xff0c;使每个子串都是回文串。 返回符合要求的最少分割次数。 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s 分割成 ["aa","b"] 这样两个回文…

html无框架,HTML框架技术详例

要将几个.html文件放入一个文件夹//UntitledFrameset-2.html 源码--------------------------------主框架//UntitledFrame-2.html源码------------------------------无标题文档XXXXX系统//UntitledFrame-3.html源码------------------------------无标题文档用户管理用户查询…

LeetCode 第 30 场双周赛(477/2545,前18.7%,第2次全部通过)

文章目录1. 比赛结果2. 题目1. LeetCode 5177. 转变日期格式 easy2. LeetCode 5445. 子数组和排序后的区间和 medium3. LeetCode 5446. 三次操作后最大值与最小值的最小差 medium4. LeetCode 5447. 石子游戏 IV hard&#xff08;博弈DP&#xff09;1. 比赛结果 时隔近5个月&am…

2012TI杯竞赛学习-----------时钟系统篇

因为参加了暑假的TI杯电子设计大赛&#xff0c;所以学习MSP430系列单片机&#xff0c;截止目前学习已有十几天&#xff0c;本想等比赛结束再做总结&#xff0c;后与人交流后发现&#xff0c;及时写下学习心得&#xff0c;与他人进行交流&#xff0c;更有益于今后学习。 430不同…

wp博客链接.html,WordPress博客文章标题链接到自定义网址链接

WordPress 支持多种文章形式&#xff0c;相信不少博主遇到过需要把wordpress文章的标题链接到自定义的URL地址的情况&#xff0c;点击文章链接时不是直接访问文章详情页面&#xff0c;而是直接访问分享的链接网页&#xff0c;就像微博一样。要实现这种效果&#xff0c;可以使用…

One year ago begininginginginging!

One year ago begininginginginging!转载于:https://www.cnblogs.com/gordon-ge/archive/2012/07/30/2615027.html

LeetCode 第 197 场周赛(468/5273,前8.88%)

文章目录1. 比赛结果2. 题目1. LeetCode 5460. 好数对的数目 easy2. LeetCode 5461. 仅含 1 的子串数 medium3. LeetCode 5211. 概率最大的路径 medium&#xff08;Dijkstra&#xff09;4. LeetCode 5463. 服务中心的最佳位置 hard&#xff08;最优化退火算法&#xff09;1. 比…

.html文件没法解析,HTML解析 - 从.html文件

这似乎为我工作(采取了一些试验和错误的)Grab(grouporg.ccil.cowan.tagsoup, moduletagsoup, version1.2)import org.ccil.cowan.tagsoup.*import groovy.xml.*String htmlTxt """SelectMaleFemale"""// Define our TagSoup backed parserdef s…

How to set up native subversion (javahl) with Subclipse on Mac OS X

macPorts 安装教程 Install the javahl binding with MacPorts: sudo port install subversion bash_completion sudo port install subversion-javahlbindings奇怪的是&#xff0c;安装到一半&#xff0c;下载不了了&#xff0c;晕死&#xff0c;先记下来吧。 Run eclipse, a…

LeetCode 174. 地下城游戏(DP)

文章目录1. 题目2. 解题1. 题目 一些坏人抓住了公主&#xff08;P&#xff09;并将她关在了地下城的右下角。 地下城是由 M x N 个房间组成的二维网格。 我们英勇的骑士&#xff08;K&#xff09;最初被安置在左上角的房间里&#xff0c; 他必须穿过地下城并通过对抗坏人来拯救…

计算机考研英语什么时候考试,计算机考研:早期复习三大注意事项

新的一年已经开始&#xff0c;这个时间对于很多考研的考生来说&#xff0c;都还是最初的准备阶段&#xff0c;而对于即将参加考研的考生来说&#xff0c;这个时期开始复习还是有些早&#xff0c;不过想要复习得更为充分&#xff0c;现在就已经可以开始了&#xff0c;今天沪江的…

Windows注册表操作基础代码

Windows注册表操作基础代码Windows下对注册表进行操作使用的一段基础代码Reg.h: #pragma once#include<assert.h>#include<windows.h>class Reg{HKEY hkey;public:void open(HKEY root,char*subKey);//打开注册表键&#xff0c;不存在则创建void del(HKEY root,cha…

[Hands On ML] 4. 训练模型

文章目录1. 线性回归1.1 正规方程求解1.2 时间复杂度1.3 梯度下降1.4 批量梯度下降1.5 随机梯度下降1.6 小批量梯度下降2. 多项式回归3. 线性模型正则化4. 早期停止法&#xff08;Early Stopping&#xff09;本文为《机器学习实战&#xff1a;基于Scikit-Learn和TensorFlow》的…

非常经典的C字符串函数的实现

1. strlen(),计算字符串长度 1 int strlen(const char string) 2 3 { 4 5 int i0; 6 7 while(string[i]) i; 8 9 return i; 10 11 } 2. strcpy(), 字符串拷贝. 1 char *strcpy(char *destination, const char *source) 2 3 { 4 5 while…

LeetCode 895. 最大频率栈(哈希+按频数存储)

文章目录1. 题目2. 解题1. 题目 实现 FreqStack&#xff0c;模拟类似栈的数据结构的操作的一个类。 FreqStack 有两个函数&#xff1a; push(int x)&#xff0c;将整数 x 推入栈中。pop()&#xff0c;它移除并返回栈中出现最频繁的元素。 如果最频繁的元素不只一个&#xff…

七年级上册计算机重点知识点,初一上册数学重点知识点

为了方便大家更好的学习和复习初一上册数学课本内容&#xff0c;现将初一上册数学重要内容整理分享出来。有理数重点知识点(1)定义&#xff1a;由整数和分数组成的数。包括&#xff1a;正整数、0、负整数&#xff0c;正分数、负分数。可以写成两个整之比的形式。(2)数轴&#x…

友元关系

友元关系转载于:https://www.cnblogs.com/LoveFishC/archive/2012/08/01/3846663.html

猪八戒背媳妇用计算机弹出来,猪八戒背媳妇?杭城游泳馆爆笑一幕:浙大学霸果然机智!...

游泳有时候不光比速度&#xff0c;还得比机智。6月19日&#xff0c;2021年“三好杯”游泳比赛在浙大紫金港校区游泳馆举行。这场比赛浙大各院系(学院)共有23支代表队、近220名运动员参赛。游泳项目是浙大的招牌项目之一&#xff0c;前不久的全国大学生阳光组(普通生)游泳比赛中…

html5支持多线程,html5 多线程

html5 多线程版本&#xff1a;HTML5运行者 Worker 接口是Web Workers API 的一部分&#xff0c;代表一个后台任务&#xff0c;它容易被创建并向创建者发回消息。创建一个运行者只要简单的调用Worker()构造函数&#xff0c;指定一个脚本&#xff0c;在工作线程中执行。运行者能够…