JAAS and the HTTP Session Life Cycleby Rajesh Shah AbstractThe J2EE security model provides a robust and easily configurable approach to securing Web applications. In a typical interaction with a Web application, a user provides security credentials and the Web application then starts an authenticated session. The user is granted access to secured resources based on his or her assigned security role, and after logging out or having the session expire, the user is returned to the login page. This happens transparently, and an appropriately configured application would always expect a user to be logged on using this mechanism. However, sometimes an application needs to know that a currently logged on user's session had expired in the past. This article examines the inner workings of the BEA WebLogic Server 8.1 J2EE security framework in relation to this HTTP session behavior. In particular, it looks at how a Web application can capture an expired session event. A Web application and source code is distributed with this article. IntroductionThe WebLogic Server security model for a Web application is based on Java Authentication and Authorization Services (JAAS), and provides a powerful declarative security model for Web resources. If you are new to JAAS, this white paper gives a good introduction to basic concepts and terminology. WebLogic Server supports three different types of J2EE authentications mechanisms: BASIC, FORM, and CLIENT-CERT. The discussion below assumes a FORM-based authentication scenario but can also apply to other authentication mechanisms. The key entities that interact in this security model are:
This article examines the interplay between these components, and then looks at how you can use an internal WebLogic Server API to capture JAAS events in the event of an HTTP session expiration. WebLogic Security FrameworkImagine a Web user requests a secured JSP page. If the user is not authenticated, the WebLogic Security Framework redirects the user to a login page. When a user attempts to log onto the Web application, the container calls to the Security Framework, accessing the authentication provider. A successful login results in the creation of a JAAS subject, which contains one or more authenticated principals. This JAAS subject is stored within an internal HTTP session object. Any subsequent requests from the user to a secured resource are authorized by locating the subject from the session. The flow chart below shows in detail the sequence of events that occur during this process.
What Happens When an HTTP Session Expires?When an HTTP session expires and the client makes a request to any secured resource, the JAAS subject will not be found for authorization. At this point, the security framework creates a new HTTP session, stores the target URL value in the session, and then redirects the user to the login page. After a successful login process, the user is forwarded back to the target page. The consequence of this behavior is that the application will never be able to detect that the session expired as it will always have a valid session available. Some Web applications may need to capture these session expiration events and show some custom message to the user. These may also be required for audit purposes. So how can you make your application aware of these events? Introducing the weblogic.servlet.security.AuthFilterThe /** Called just before Authentication and Authorization occurs Note that when authentication and authorization fails, the originally requested
URL can still be found in the session under the key To install such a filter, it should be referenced from the <weblogic-web-app> Capturing Session Expired AttributeThe plan should now be clear. You need to create a class,
public void doPreAuth(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response) {
// get the session
HttpSession session = ((HttpServletRequest)request).getSession(false);
if(session == null) {
// Create a new session
session = ((HttpServletRequest)request).getSession(true);
//set the session expiration attribute
if (((HttpServletRequest)request).getRequestedSessionId() != null) {
session.setAttribute(Constants.SESSION_EXPIRED, "true");
}
}
}
Now you can use this <% You may be wondering, "Why not use an HTTP session listener to capture the session expiration event?" The HTTP session listener does not allow you to create a new session, so you need to store the information that the previous session expired. DownloadYou can download the following items:
ConclusionCapturing an expired session is just one of the The WebLogic Server documentation says that Additional Reading
Return to dev2dev. Showing messages 1 through 1 of 1.
|
Article Tools Related Products Check out the products mentioned in this article:Bookmark Article
|