I've been trying authentication on BlazeDS and Tomcat using "flex.messaging.security.TomcatValve".
I succeeded authentication only *once*.
I thought things goes like this,
1. Consumer#setCredentials("user1", "pass1")
2. Do something.
4. Consumer#setCredentials("user2", "pass2") // Relogin as different user.
But at "4", error occured bellow
Error: Credentials cannot be set while authenticating or logging out.
Then I tried *without* calling Consumer#logout(), other error occured.
Error: Credentials cannot be set when already authenticated. Logout must be performed before changing credentials.
It's completely opposite.
So how should I do?
I'm not good at Enlish but hope make sense.
You are calling Consumer.setCredentials("user2", "pass2") before Consumer.logout successfully completes. The real problem is though there is no way to know when Consumer.logout has completed. (Consumer.logout is legacy code and it does not work that well.)
I suggest you use ChannelSet.logout instead. ChannelSet.logout returns an AsyncToken that you can attach result and fault responders to. When ChannelSet.logout returns successfully, you can call Consumer.setCredentials from the result responder.
I haven't compiled this but it should be something like this:
var token:AsyncToken = consumer.channelSet.logout();
function (result:Object, token:Object=null):void
function (error:Object, token:Object=null):void
// Error handling here.
A better approach to handling authentication in BlazeDS is to use the new login() and logout() methods on ChannelSet. These let you cleanly handle any authentication or logout issues, and return a token that you can add a responder to for targeted event handling.
The old setCredentials() method dates back to Flex 1 and is awkard to use because it may or may not be lazy depending on when you call it and when a connection to the server is actually established, and any authentication problems come back as a fault for an unrelated operation that you've invoked. Similarly, the old logout() method on things like Consumer and RemoteObject doesn't provide a direct way to handle success/fault.
You should really prefer the use of the new APIs on ChannelSet.
Mete, your recomendation worked. I've added your two lines to my onComplete event handler of my application to initialize. And I no longer get the null object error.
Does it mean that, adding the <mx:RemoteObject> doesn't initialize the channelSet that your destination is defaulted to use. Anyways, thanks again.