EsbAOP应用--权限管理
2005-09-28 12:01
330 查看
前文介绍了异常关闭器的AOP实现,而权限管理也是一个AOP可以大展拳脚的地方,下面就来看看如何使用EsbAOP实现权限管理。
首先要解决的问题是如何判断用户是否拥有调用某个操作的权限,这个判断是由各个应用程序自己实现了,为了统一对权限的判断,所以我们指定了接口IPermissionVerifier:
/// <summary>
/// IPermissionVerifier 用于验证当前用户对目标类的目标方法的调用权限是否足够。
/// 如果需要使用Aop来进行权限管理,则要实现IPermissionVerifier接口。
/// </summary>
public interface IPermissionVerifier
{
bool QualifiedToOperation(object permissionNeeded ,string destClassFullName ,string destMethodName) ;
} 其中,permissionNeeded通过应用于方法的特性的参数指定(后面可以看到),destClassFullName表示目标方法所在class的全称,destMethodName是目标方法名。
当用户的权限不满足条件时,对目标方法的调用将抛出PermissionLimittedException异常,该异常定义如下:
/// <summary>
/// 当没有权限的用户调用某项操作时,将抛出此异常
/// </summary>
[Serializable]
public class PermissionLimittedException : Exception
{
public PermissionLimittedException()
{
}
public PermissionLimittedException(string msg):base(msg)
{
}
public PermissionLimittedException(string msg ,Exception innerException) : base(msg ,innerException)
{
}
} 注意,自定义异常必须是可序列化的,这样就可以保证异常通过remoting传递。
接下来,就可以实现权限方面PermissionAspect了。
/// <summary>
/// PermissionAspect 权限方面,如果当前用户不够权限调用某个方法,则抛出PermissionLimittedException。
/// aspectClassArgument :实现了IPermissionVerifier接口的类型
/// aspectMethodArgument:操作所需的权限,由用户自定义
/// </summary>
public class PermissionAspect :IAspect
{
public PermissionAspect()
{
}
#region IAspect 成员
public void PreProcess(IMethodCallMessage requestMsg, object aspectClassArgument, object aspectMethodArgument)
{
Type verifierType = (Type)aspectClassArgument ;
Type destType = typeof(IPermissionVerifier) ;
if(! destType.IsAssignableFrom(verifierType))
{
throw new Exception("the PermissionVerifierType is invalid !") ;
}
IPermissionVerifier pmVerifier = (IPermissionVerifier)Activator.CreateInstance(verifierType) ;
if(pmVerifier == null)
{
throw new Exception("the PermissionVerifierType is invalid !") ;
}
object pmNeeded = aspectMethodArgument ;
string className = AopHelper.GetFullClassName(requestMsg) ;
string methodName = AopHelper.GetMethodName(requestMsg) ;
bool qualified = pmVerifier.QualifiedToOperation(pmNeeded ,className ,methodName) ;
if(! qualified)
{
throw new PermissionLimittedException(string.Format("Current user have no permission to call dest method : {0}.{1}!" ,className ,methodName)) ;
}
}
public void PostProcess(IMethodCallMessage requestMsg, ref IMethodReturnMessage respond, object aspectClassArgument, object aspectMethodArgument)
{
// TODO: 添加 PermissionAspect.EnterpriseServerBase.Aop.ComplexAop.IAspect.PostProcess 实现
}
#endregion
} 关于aspectClassArgument 和aspectMethodArgument的含义注释中已经给出了,如果要知道这两个参数是如何使用的,可以回顾EsbAOP实现中的说明。权限方面在前处理的时候,根据IPermissionVerifier判断用户是否满足权限要求,如果不满足,则引发PermissionLimittedException异常。
最后给出一个示例:
public class class1
{
[STAThread]
static void Main(string[] args)
{
try
{
Example exa = new Example() ;
exa.SayHello("sky") ;
exa.SayByebye("sky") ;
}
catch(Exception ee)
{
Console.WriteLine(ee.Message) ;
}
Console.Read() ;
}
}
[Aspect(typeof(PermissionAspectWrap))]
public class Example :ContextBoundObject
{
[AspectSwitcher(typeof(PermissionAspectWrap) ,true ,Permission.Common)]
public void SayHello(string name)
{
Console.WriteLine("Hello ," + name) ;
}
[AspectSwitcher(typeof(PermissionAspectWrap) ,true ,Permission.Super)] //调用此方法需要Permission.Super权限
public void SayByebye(string name)
{
Console.WriteLine("Byebye ," + name) ;
}
}
#region PermissionAspectWrap
public class PermissionAspectWrap :IAspectProcessorWrap
{
#region IAspectProcessorWrap 成员
public Type AspectProcessorType
{
get
{
return typeof(PermissionAspect);
}
}
public object AspectClassArgument
{
get
{
return typeof(PermissionVerifier) ;
}
}
public AspectSwitcherState DefaultAspectSwitcherState
{
get
{
return AspectSwitcherState.On ;
}
}
#endregion
}
#endregion
#region PermissionVerifier ,Permission ,Logger
public class PermissionVerifier :IPermissionVerifier
{
private static int curPermission = Permission.Common ;
#region IPermissionVerifier 成员
public bool QualifiedToOperation(object permissionNeeded, string destClassFullName, string destMethodName)
{
int destPermission = (int)permissionNeeded ;
if(PermissionVerifier.curPermission > destPermission)
{
return true ;
}
return false;
}
#endregion
}
public class Permission
{
public const int Common = 0 ;
public const int Super = 1 ;
}
#endregion 由于当前用户级别为Permission.Common,所以在调用SayByebye的时候会抛出异常,而在调用SayHello的时候却可以正常进行。
首先要解决的问题是如何判断用户是否拥有调用某个操作的权限,这个判断是由各个应用程序自己实现了,为了统一对权限的判断,所以我们指定了接口IPermissionVerifier:
/// <summary>
/// IPermissionVerifier 用于验证当前用户对目标类的目标方法的调用权限是否足够。
/// 如果需要使用Aop来进行权限管理,则要实现IPermissionVerifier接口。
/// </summary>
public interface IPermissionVerifier
{
bool QualifiedToOperation(object permissionNeeded ,string destClassFullName ,string destMethodName) ;
} 其中,permissionNeeded通过应用于方法的特性的参数指定(后面可以看到),destClassFullName表示目标方法所在class的全称,destMethodName是目标方法名。
当用户的权限不满足条件时,对目标方法的调用将抛出PermissionLimittedException异常,该异常定义如下:
/// <summary>
/// 当没有权限的用户调用某项操作时,将抛出此异常
/// </summary>
[Serializable]
public class PermissionLimittedException : Exception
{
public PermissionLimittedException()
{
}
public PermissionLimittedException(string msg):base(msg)
{
}
public PermissionLimittedException(string msg ,Exception innerException) : base(msg ,innerException)
{
}
} 注意,自定义异常必须是可序列化的,这样就可以保证异常通过remoting传递。
接下来,就可以实现权限方面PermissionAspect了。
/// <summary>
/// PermissionAspect 权限方面,如果当前用户不够权限调用某个方法,则抛出PermissionLimittedException。
/// aspectClassArgument :实现了IPermissionVerifier接口的类型
/// aspectMethodArgument:操作所需的权限,由用户自定义
/// </summary>
public class PermissionAspect :IAspect
{
public PermissionAspect()
{
}
#region IAspect 成员
public void PreProcess(IMethodCallMessage requestMsg, object aspectClassArgument, object aspectMethodArgument)
{
Type verifierType = (Type)aspectClassArgument ;
Type destType = typeof(IPermissionVerifier) ;
if(! destType.IsAssignableFrom(verifierType))
{
throw new Exception("the PermissionVerifierType is invalid !") ;
}
IPermissionVerifier pmVerifier = (IPermissionVerifier)Activator.CreateInstance(verifierType) ;
if(pmVerifier == null)
{
throw new Exception("the PermissionVerifierType is invalid !") ;
}
object pmNeeded = aspectMethodArgument ;
string className = AopHelper.GetFullClassName(requestMsg) ;
string methodName = AopHelper.GetMethodName(requestMsg) ;
bool qualified = pmVerifier.QualifiedToOperation(pmNeeded ,className ,methodName) ;
if(! qualified)
{
throw new PermissionLimittedException(string.Format("Current user have no permission to call dest method : {0}.{1}!" ,className ,methodName)) ;
}
}
public void PostProcess(IMethodCallMessage requestMsg, ref IMethodReturnMessage respond, object aspectClassArgument, object aspectMethodArgument)
{
// TODO: 添加 PermissionAspect.EnterpriseServerBase.Aop.ComplexAop.IAspect.PostProcess 实现
}
#endregion
} 关于aspectClassArgument 和aspectMethodArgument的含义注释中已经给出了,如果要知道这两个参数是如何使用的,可以回顾EsbAOP实现中的说明。权限方面在前处理的时候,根据IPermissionVerifier判断用户是否满足权限要求,如果不满足,则引发PermissionLimittedException异常。
最后给出一个示例:
public class class1
{
[STAThread]
static void Main(string[] args)
{
try
{
Example exa = new Example() ;
exa.SayHello("sky") ;
exa.SayByebye("sky") ;
}
catch(Exception ee)
{
Console.WriteLine(ee.Message) ;
}
Console.Read() ;
}
}
[Aspect(typeof(PermissionAspectWrap))]
public class Example :ContextBoundObject
{
[AspectSwitcher(typeof(PermissionAspectWrap) ,true ,Permission.Common)]
public void SayHello(string name)
{
Console.WriteLine("Hello ," + name) ;
}
[AspectSwitcher(typeof(PermissionAspectWrap) ,true ,Permission.Super)] //调用此方法需要Permission.Super权限
public void SayByebye(string name)
{
Console.WriteLine("Byebye ," + name) ;
}
}
#region PermissionAspectWrap
public class PermissionAspectWrap :IAspectProcessorWrap
{
#region IAspectProcessorWrap 成员
public Type AspectProcessorType
{
get
{
return typeof(PermissionAspect);
}
}
public object AspectClassArgument
{
get
{
return typeof(PermissionVerifier) ;
}
}
public AspectSwitcherState DefaultAspectSwitcherState
{
get
{
return AspectSwitcherState.On ;
}
}
#endregion
}
#endregion
#region PermissionVerifier ,Permission ,Logger
public class PermissionVerifier :IPermissionVerifier
{
private static int curPermission = Permission.Common ;
#region IPermissionVerifier 成员
public bool QualifiedToOperation(object permissionNeeded, string destClassFullName, string destMethodName)
{
int destPermission = (int)permissionNeeded ;
if(PermissionVerifier.curPermission > destPermission)
{
return true ;
}
return false;
}
#endregion
}
public class Permission
{
public const int Common = 0 ;
public const int Super = 1 ;
}
#endregion 由于当前用户级别为Permission.Common,所以在调用SayByebye的时候会抛出异常,而在调用SayHello的时候却可以正常进行。
相关文章推荐
- EsbAOP应用--权限管理
- Web应用中基于组的用户权限管理在Spring框架下的实现
- Android 从应用程序跳到系统应用管理和应用的权限管理页面
- Filter(过滤器)常见应用(三)——权限管理系统(二)
- Android应用权限管理总结
- Windows XP安全管理------权限管理应用篇
- 学焊接专业的编程爱好者应用好通用权限管理系统写出了自己工作中需要的小软件
- 分享一个基于ligerui的系统应用案例ligerRM V2(权限管理系统)(提供下载)
- 小米手机上,打开自启动管理,打开本app应用权限管理页面
- C#.NET中的 sender Tag 功能在支持多语言的通用权限管理系统组件中的实际应用范例
- windows资源文件管理应用图标与管理员权限
- 反射技术在通用权限管理系统组件源码中的实际应用参考【附源码】
- 通用权限管理系统组件 (GPM - General Permissions Manager) 在跨国直销企业电子商务项目中的成功应用
- 乐视手机调用自启动管理, 乐视手机调用应用权限管理
- Linux服务器权限管理之sudo应用
- Filter(过滤器)常见应用(三)——权限管理系统(三)
- 深入理解Java的Annotation系列-第四部分 注解的应用-使用注解实现权限管理
- RBAC权限管理技术应用
- Windows 10 手机版性功能 统一管理应用获取地理位置的权限
- Vue + Element UI 实现权限管理系统 前端篇(八):管理应用状态