Accessibility

TechNote (Archived)

Sample for accessing session data in Flex 1.5

Issue


A session servlet is provided in Macromedia Flex 1.0 to allow Flex applications to access the session data from RemoteObject. InFlex 1.5, the session servlet is still provided, but the recommended way of accessing session and other request data is to access the session through getHttpRequest()/getHttpResponse()/getServletConfig(). This TechNote provides an example on how to use them in Flex 1.5.

Solution


In Flex 1.5, a Java object being called from RemoteObject now has access to request, response, and servlet information. From within the Java object, users can call the following three methods:

  • flashgateway.Gateway.getHttpRequest() returns HttpServletRequest for the current request
  • flashgateway.Gateway.getHttpResponse()returns HttpServletResponse for the current request
  • flashgateway.Gateway.getServletConfig() returns ServletConfig for the calling servlet

Therefore one can get access to the session attribute "foo" in a user's Java class by adding something like this:

String fooAttrib = (String)flashgateway.Gateway.getHttpRequest().getSession().getAttribute("foo");

Now, we will modify the security RemoteObject (SecureRemoteObject.mxml) sample that comes with the Flex 1.5 install under flex-install/resources/security/examples, to show how to configure and add code to use flashgateway.Gateway.getHttpRequest().

  1. Copy SecureRemoteObject.mxml and LogonWindow.mxml fromflex-install/resources/security/examples to your Flex application.
  2. Then, modify SecureRemoteObject.mxml to look like the sample SecureRemoteObject.mxml.
  3. Copy all the classes underflex-install/resources/security/examples/classes/examples to your Flex application's WEB-INF/classes/examples directory.
  4. Make a copy of SalaryManager.java under flex install/resources/security/examples/src to your Flex application's WEB-INF/classes directory.
  5. Modify SalaryManager.java to look like the sample SalaryManager.java and recompile it. (Or use the sample SalaryManager.class.)
  6. Modify the named service SampleSalaryRO to be the following in the flex-config.xml:
     <object name="SampleSalaryRO"><source>samples.explorer.SalaryManager</source><type>stateless-class</type><use-custom-authentication>true</use-custom-authentication><roles><role>sampleusers</role></roles></object> 
  7. Verify that you have the following in your web.xml<security-constraint> section:
     <security-constraint><web-resource-collection><web-resource-name>Protected Sample</web-resource-name><url-pattern>/amfgateway/SampleSalaryRO</url-pattern><http-method>GET</http-method><http-method>POST</http-method></web-resource-collection><auth-constraint><role-name>sampleusers</role-name></auth-constraint></security-constraint><login-config><auth-method>BASIC</auth-method></login-config><security-role><role-name>sampleusers</role-name></security-role> 
  8. Add the users and roles on your application server by following the instructions inflex-install/resources/security/examples/addingusers.txt.
  9. Now, run SecureRemoteObject.mxml and you should see the output:

    user Name: sampleuser
    Principal Name: sampleuser

AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Creative Commons License

Search Support

Document Details

ID:9636f34e

Products Affected:

flex