For custom authentication, the client application passes credentials to the server without relying on the browser. Although you apply security constraints to a destination, you actually log in and log out of the channels associated with the destination. Therefore, to send authentication credentials to a destination that uses custom authentication, you specify a user name and password as arguments to the ChannelSet.login() method. You remove credentials by calling the ChannelSet.logout() method.
ChannnelSet.login() and ChannelSet.logout() methods are the preferred methods for setting and removing credentials. You can also send credentials to a destination by calling the setCredentials() method of a component such as RemoteObject, Producer, Consumer, WebService, or HTTPService. However, the setCredentials() method does not actually pass the credentials to the server until the first attempt by the component to connect to the server. Therefore, if the component issues a fault event, you can not be certain whether the fault happened because of an authentication error, or for another reason. The ChannelSet.login() method connects to the server when you call it so that you can handle an authentication issue immediately. Similarly, you can remove credentials from a component, such as RemoteObject, Producer, Consumer, WebService, or HTTPService, with the component’s logout() method. However, this method only sends a logout request to the server if the client is connected and authenticated. If these conditions are not met, the behavior for this method is to do nothing other than clear any credentials that have been cached for use in automatic reconnects. The ChannelSet.logout() method removes credentials immediately.Because multiple destinations can use the same channels, and corresponding ChannelSet object, logging in to one destination logs the user in to any other destination that uses the same channel or channels. If two components apply different credentials to the same ChannelSet object, the last credentials applied are used. If multiple components use the same authenticated ChannelSet object, calling the logout() method logs all components out of the destinations.
The login() and logout() methods return an AsyncToken object. Assign event handlers to the AsyncToken object for the result event to handle a successful call, and for the fault event to handle a failure.
How the server processes the logout() method depends on the setting of the per-client-authentication property:
The following example uses the ChannelSet.login() and ChannelSet.logout() methods with a RemoteObject control. This application performs the following actions:
<?xml version="1.0"?>
<!-- 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="remoting_AMF_SecurityConstraint_Custom"
result="resultHandler(event);"
fault="faultHandler(event);"/>
</mx:Application>
Perform the following configuration steps to use custom authentication with Tomcat:
${catalina.home}/lib/blazeds/*.jar
<Valve className="flex.messaging.security.TomcatValve"/>
You can now authenticate against the current Tomcat realm. Typically user information is in the conf/tomcat-users.xml file. For more information, see the Tomcat documentation.
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/blazeds/1/blazeds_devguide/services_security_5.html
Comments
No screen name said on Sep 28, 2009 at 4:31 PM :