Binyy Wuhan

i love Flex i come from Wuhan

导航

Flex AS【原创】小弟对于类权限的一种授权验证思路

   

  最近接到一个需要对特定的as类行进授权验证的需求,防止用户非法实例化。

  我的思路是在每个需要授权验证的类加入静态代码块,执行验证方法,经过授权就成功实例化该类,否则就为非法实例化该类,弹出蒙板对话框。

  其实这样实质上用户还是实例化了该类,只是在操作界面进行了阻止。

  另一方面,我觉得也可以在构造方法中进行验证并且抛异常 throw new Error("非法实例化"); 只是这样的用户体验非常差,如果选择这钟方法,那你最好还是try catch。

  比如:

 1 public class YourClass
2 {
3
4 {
5 Validator.doValidate("YourClass");
6 }
7
8 public function YourClass():void{
9
10 }
11 }

 Validator类为验证器类,你可以自定义这个类,在里面写你的验证逻辑。

  比如:(这是我的实际需求中的验证方法)

 1         /**
2 * 模块验证license (air)
3 * @param moduleName:String 模块名称(类名)
4 * @param filePath:String license文件路径
5 * **/
6 public static function doValidate(moduleName:String = "",filePath:String = ""):void{
7 trace("moduleName ------------------- " + moduleName);
8 validatorModel.moduleName = moduleName;
9 for each(var modulevo:ModuleVo in ValidatorDataModel.getInstance().moduleDatas){
10 if(modulevo.moduleName == moduleName && modulevo.isIegal == true){
11 return;
12 }
13 }
14 Validator.ModuleName = moduleName;
15 if(filePath == ""){
16 filePath = validatorModel.FilePath;
17 }
18 var validateUtil:ValidateUtil = new ValidateUtil(validateSuccessFunction,validateFailFunction);
19 validateUtil.validate(moduleName,filePath);
20 }

  

  validateUtil.validate 方法中描述了我的核心验证逻辑
 1         /**
2 * 验证授权 (air)
3 * @param currentMuoduleName:String 当前模块名称
4 * @param filePath:String 文件路径
5 */
6 public function validate(currentMuoduleName:String,filePath:String):void
7 {
8 this._currentMuoduleName = currentMuoduleName;
9 trace("currentMuoduleName : " + this._currentMuoduleName);
10 this._filePath = filePath;
11 if(Security.sandboxType == Security.APPLICATION){
12 readLicense();
13 }else{
14 if(!this._filePath){
15 validateFailFunction();
16 return;
17 }
18 if(this._filePath == "license.lic"){
19 loadLicense();
20 }else{
21 validateLicense(this._filePath);
22 }
23 }
24 }

  下面贴一张我的验证功能类图:



  

posted on 2012-03-15 20:34  Binyy_Wuhan  阅读(424)  评论(0编辑  收藏  举报