最终效果:
首先把 flex_security.swc 放进你项目的lib文件中。
如图:
四种权限控制方式:
1. styleName
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" viewSourceURL="http://code.google.com/p/flex-security/source/browse/trunk/flex_security_demo/src/helloworld_by_styleName.mxml" creationComplete="creationComplete()"> <s:layout> <s:VerticalLayout horizontalAlign="center"/> </s:layout> <fx:Script> <![CDATA[ import cn.org.rapid_framework.flex_security.SecurityControler; import mx.collections.ArrayCollection; import mx.events.FlexEvent; [Bindable] private var permissionList : Array = ['cc','sq','lb','ta_input']; protected function creationComplete():void { //启动权限控制 SecurityControler.start(null,'visible'); } ]]> </fx:Script> <s:HGroup> <mx:HBox> <mx:ComboBox id="permInput" dataProvider="{permissionList}"/> <mx:Button click="SecurityControler.addPerm(permInput.selectedLabel)" label="添加权限" /> <mx:Button click="SecurityControler.removePerm(permInput.selectedLabel)" label="移除权限" /> <mx:Button click="SecurityControler.addAllPerms(new ArrayCollection(permissionList))" label="添加所有权限" /> <mx:Button click="SecurityControler.removeAllPerms()" label="移除所有权限" /> </mx:HBox> </s:HGroup> <s:HGroup> <!-- 通过styleName增加需要权限保护的组件,没有权限将不能进行相关操作 --> <mx:HBox> <!-- id要和声明的对应,enabled表示显现出来,由于受到权限的控制,不可启用--> <mx:Button label="曹操" id="cc" styleName="security(null,enabled)" /> <!-- 没有声明id,那么在security内要写上 --> <mx:Button label="孙权" styleName="security(sq)"/> <!-- 注意这个remove属性,这里声明了remove,那么下面的组件就不受security的控制而会显现出来了 --> <mx:Button label="刘备" styleName="security(lb,remove)"/> <!-- 这个算是容易点的写法吧 --> <mx:TextArea id="ta_input" styleName="security"/> </mx:HBox> </s:HGroup> </s:Application>
2. manual
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" viewSourceURL="http://code.google.com/p/flex-security/source/browse/trunk/flex_security_demo/src/helloworld_by_manual.mxml" creationComplete="creationComplete()"> <s:layout> <s:VerticalLayout horizontalAlign="center"/> </s:layout> <fx:Script> <![CDATA[ import cn.org.rapid_framework.flex_security.SecurityControler; import mx.collections.ArrayCollection; import mx.events.FlexEvent; [Bindable] private var permissionList : Array = ['cc','sq','lb','ta_input']; protected function creationComplete():void { //启动权限控制 SecurityControler.start(null,'visible'); //增加需要权限保护的UI资源 SecurityControler.addSecurityAction(cc); SecurityControler.addSecurityAction(sq); SecurityControler.addSecurityAction(lb); } ]]> </fx:Script> <s:HGroup> <mx:HBox> <mx:ComboBox id="permInput" dataProvider="{permissionList}"/> <mx:Button click="SecurityControler.addPerm(permInput.selectedLabel)" label="添加权限" /> <mx:Button click="SecurityControler.removePerm(permInput.selectedLabel)" label="移除权限" /> <mx:Button click="SecurityControler.addAllPerms(new ArrayCollection(permissionList))" label="添加所有权限" /> <mx:Button click="SecurityControler.removeAllPerms()" label="移除所有权限" /> </mx:HBox> </s:HGroup> <s:HGroup> <!-- 被权限控制的按钮,没有权限将看不见下面的相关按钮 --> <mx:HBox> <!-- 看见没有,主要还是id和permissionList声明的对应 --> <mx:Button label="曹操" id="cc"/> <mx:Button label="孙权" id="sq"/> <mx:Button label="刘备" id="lb"/> <mx:TextArea id="ta_input"/> </mx:HBox> </s:HGroup> </s:Application>
3. interface
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" viewSourceURL="http://code.google.com/p/flex-security/source/browse/trunk/flex_security_demo/src/helloworld_by_interface.mxml" implements="cn.org.rapid_framework.flex_security.ISecurityMetadata" creationComplete="creationComplete()"> <s:layout> <s:VerticalLayout horizontalAlign="center"/> </s:layout> <fx:Script> <![CDATA[ import cn.org.rapid_framework.flex_security.SecurityAction; import cn.org.rapid_framework.flex_security.SecurityControler; //注意添加上面的实现---implements="cn.org.rapid_framework.flex_security.ISecurityMetadata" import cn.org.rapid_framework.flex_security.ISecurityMetadata; import mx.collections.ArrayCollection; import mx.events.FlexEvent; [Bindable] private var permissionList : Array = ['cc','sq','lb','ta_input']; protected function creationComplete():void { //启动权限控制 SecurityControler.start(null,'visible'); } //这里返回需要权限保护的UI资源 public function getSecurityActions() : Array { return [ new SecurityAction(cc), new SecurityAction(sq), new SecurityAction(lb) ]; } ]]> </fx:Script> <s:HGroup> <mx:HBox> <mx:ComboBox id="permInput" dataProvider="{permissionList}"/> <mx:Button click="SecurityControler.addPerm(permInput.selectedLabel)" label="添加权限" /> <mx:Button click="SecurityControler.removePerm(permInput.selectedLabel)" label="移除权限" /> <mx:Button click="SecurityControler.addAllPerms(new ArrayCollection(permissionList))" label="添加所有权限" /> <mx:Button click="SecurityControler.removeAllPerms()" label="移除所有权限" /> </mx:HBox> </s:HGroup> <s:HGroup> <!-- 被权限控制的按钮,没有权限将看不见下面的相关按钮 --> <mx:HBox> <!-- 看见没有,主要还是id和permissionList声明的对应 --> <mx:Button label="曹操" id="cc"/> <mx:Button label="孙权" id="sq"/> <mx:Button label="刘备" id="lb"/> <mx:TextArea id="ta_input"/> </mx:HBox> </s:HGroup> </s:Application>
4. annotation
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" viewSourceURL="http://code.google.com/p/flex-security/source/browse/trunk/flex_security_demo/src/helloworld_by_annotation.mxml" creationComplete="creationComplete()"> <s:layout> <s:VerticalLayout horizontalAlign="center"/> </s:layout> <!-- 通过元数据[Protected(id='compId',permission='perm',controlBy='enabled')]确定那个UI组件需要权限保护 --> <fx:Metadata> [Protected(id="cc")] [Protected(id="sq")] [Protected(id="lb")] </fx:Metadata> <fx:Script> <![CDATA[ import cn.org.rapid_framework.flex_security.SecurityControler; import mx.collections.ArrayCollection; import mx.events.FlexEvent; [Bindable] private var permissionList : Array = ['cc','sq','lb','ta_input']; protected function creationComplete():void { //启动权限控制 SecurityControler.start(null,'visible'); } ]]> </fx:Script> <s:HGroup> <mx:HBox> <mx:ComboBox id="permInput" dataProvider="{permissionList}"/> <mx:Button click="SecurityControler.addPerm(permInput.selectedLabel)" label="添加权限" /> <mx:Button click="SecurityControler.removePerm(permInput.selectedLabel)" label="移除权限" /> <mx:Button click="SecurityControler.addAllPerms(new ArrayCollection(permissionList))" label="添加所有权限" /> <mx:Button click="SecurityControler.removeAllPerms()" label="移除所有权限" /> </mx:HBox> </s:HGroup> <s:HGroup> <mx:HBox> <mx:Button label="曹操" id="cc"/> <mx:Button label="孙权" id="sq"/> <mx:Button label="刘备" id="lb"/> <mx:TextArea id="ta_input"/> </mx:HBox> </s:HGroup> </s:Application>
按实际情况抉择使用