View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.security.authentication;
15  
16  import javax.servlet.ServletRequest;
17  import javax.servlet.ServletResponse;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpSession;
20  
21  import org.eclipse.jetty.security.Authentication;
22  import org.eclipse.jetty.security.Authenticator;
23  import org.eclipse.jetty.security.DefaultAuthentication;
24  import org.eclipse.jetty.security.ServerAuthException;
25  
26  /**
27   * @version $Rev: 4793 $ $Date: 2009-03-19 00:00:01 +0100 (Thu, 19 Mar 2009) $
28   */
29  public class SessionCachingAuthenticator extends DelegateAuthenticator
30  {
31      public final static String __J_AUTHENTICATED = "org.eclipse.jetty.server.Auth";
32  
33  
34      public SessionCachingAuthenticator(Authenticator delegate)
35      {
36          super(delegate);
37      }
38  
39      public Authentication validateRequest(ServletRequest request, ServletResponse response, boolean mandatory) throws ServerAuthException
40      {
41          HttpSession session = ((HttpServletRequest)request).getSession(mandatory);
42          // not mandatory and not authenticated
43          if (session == null) 
44              return DefaultAuthentication.SUCCESS_UNAUTH_RESULTS;
45  
46          Authentication authentication = (Authentication) session.getAttribute(__J_AUTHENTICATED);
47          if (authentication != null) 
48              return authentication;
49  
50          authentication = _delegate.validateRequest(request, response, mandatory);
51          if (authentication != null && authentication.getUserIdentity().getSubject() != null)
52          {
53              Authentication next=new DefaultAuthentication(Authentication.Status.SUCCESS,authentication.getAuthMethod(),authentication.getUserIdentity());
54              session.setAttribute(__J_AUTHENTICATED, next);
55          }
56          return authentication;
57      }
58  
59  }