Adobe BlazeDS of Custom Security Configuration

首先你要做的就是, 修改一部分的 Tomcat Server,
1. 將 flex-tomcat-common.jar, flex-tomcat-server.jar 兩個檔案複製到 apache-tomcat-6.0.18/lib/ 目錄下
2. 如果剛剛上述的 第一點是有另外建立子目錄的, 記得要修改  conf/ 目錄下的 catalina.properties 檔案
3. apache-tomcat-6.0.18/conf/  目錄下的 context.xml檔案, 記得加上, 順帶一提的是, 這個 TomcatValve 加在專案中的 META-INF/context.xml 是沒有用的
    <!– Adobe Flex Custom Security –>
    <Valve className="flex.messaging.security.TomcatValve"/>
 
開始修改 Flex 專案 (也就是 Flex Client ) 吧!!…
1. 將 services-config.xml 檔案中的  security-constraint 的 auth-method 改為 Custom
    <security>
        <login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
        <security-constraint id="sample-users">
            <auth-method>Custom</auth-method>
            <roles>
                <role>admins</role>
                <role>managers</role>
            </roles>
        </security-constraint>
    </security>
 
2. 加入一個測試的 Flex 檔案
<?xml version="1.0" encoding="utf-8"?>
<!– security/SecurityConstraintCustom.mxml –>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"
    creationComplete="creationCompleteHandler();">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.messaging.config.ServerConfig;
            import mx.rpc.AsyncToken;
            import mx.rpc.AsyncResponder;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.messaging.ChannelSet;
            // Define a ChannelSet object.
            public var cs:ChannelSet;
           
            // Define an AsyncToken object.
            public var token:AsyncToken;
            // Initialize ChannelSet object based on the
            // destination of the RemoteObject component.
            private function creationCompleteHandler():void {
                if (cs == null)
                    cs = ServerConfig.getChannelSet(remoteObject.destination);                   
            }
            // Login and handle authentication success or failure.
            private function ROLogin():void {
                // Make sure that the user is not already logged in.
                if (cs.authenticated == false) {
                    token = cs.login("sampleuser", "samplepassword");
                    // Add result and fault handlers.
                    token.addResponder(new AsyncResponder(LoginResultEvent, LoginFaultEvent));
                }
            }
            // Handle successful login.
            private function LoginResultEvent(event:ResultEvent, token:Object=null):void  {
                switch(event.result) {
                    case "success":
                        authenticatedCB.selected = true;
                    break;
                    default:
                }
            }
           
            // Handle login failure.
            private function LoginFaultEvent(event:FaultEvent, token:Object=null):void {
                switch(event.fault.faultCode) {
                    case "Client.Authentication":
                        default:
                            authenticatedCB.selected = false;
                            Alert.show("Login failure: " + event.fault.faultString);
                }
            }
            // Logout and handle success or failure.
            private function ROLogout():void {
                // Add result and fault handlers.
                token = cs.logout();
                token.addResponder(new AsyncResponder(LogoutResultEvent,LogoutFaultEvent));
            }
            // Handle successful logout.
            private function LogoutResultEvent(event:ResultEvent, token:Object=null):void {
                switch (event.result) {
                    case "success":
                        authenticatedCB.selected = false;
                        break;
                    default:
                }
            }
            // Handle logout failure.
            private function LogoutFaultEvent(event:FaultEvent, token:Object=null):void {
                Alert.show("Logout failure: " + event.fault.faultString);
            }
            // Handle message recevied by RemoteObject component.
            private function resultHandler(event:ResultEvent):void {
                ta.text += "Server responded: "+ event.result + "\n";
            }
            // Handle fault from RemoteObject component.
            private function faultHandler(event:FaultEvent):void {
                ta.text += "Received fault: " + event.fault + "\n";
            }
        ]]>
    </mx:Script>
    <mx:HBox>
        <mx:Label text="Enter a text for the server to echo"/>
        <mx:TextInput id="ti" text="Hello World!"/>
        <mx:Button label="Login"
            click="ROLogin();"/>
        <mx:Button label="Echo" 
            enabled="{authenticatedCB.selected}"
            click="remoteObject.echo(ti.text);"/>
        <mx:Button label="Logout"
            click="ROLogout();"/>
        <mx:CheckBox id="authenticatedCB"
            label="Authenticated?"
            enabled="false"/>
    </mx:HBox>
    <mx:TextArea id="ta" width="100%" height="100%"/>
    <mx:RemoteObject id="remoteObject"
        destination="hello"
        result="resultHandler(event);"
        fault="faultHandler(event);"/>
</mx:Application>
3. 測試
廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s