View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.security.authentication;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import org.eclipse.jetty.security.Authenticator;
26  import org.eclipse.jetty.security.IdentityService;
27  import org.eclipse.jetty.security.LoginService;
28  import org.eclipse.jetty.server.session.AbstractSessionManager;
29  
30  public abstract class LoginAuthenticator implements Authenticator
31  {
32      protected final DeferredAuthentication _deferred=new DeferredAuthentication(this);
33      protected LoginService _loginService;
34      protected IdentityService _identityService;
35      private boolean _renewSession;
36  
37      protected LoginAuthenticator()
38      {
39      }
40  
41      public void setConfiguration(AuthConfiguration configuration)
42      {
43          _loginService=configuration.getLoginService();
44          if (_loginService==null)
45              throw new IllegalStateException("No LoginService for "+this+" in "+configuration);
46          _identityService=configuration.getIdentityService();
47          if (_identityService==null)
48              throw new IllegalStateException("No IdentityService for "+this+" in "+configuration);
49          _renewSession=configuration.isSessionRenewedOnAuthentication();
50      }
51      
52      public LoginService getLoginService()
53      {
54          return _loginService;
55      }
56      
57      /** Change the session id.
58       * The session is changed to a new instance with a new ID if and only if:<ul>
59       * <li>A session exists.
60       * <li>The {@link AuthConfiguration#isSessionRenewedOnAuthentication()} returns true.
61       * <li>The session ID has been given to unauthenticated responses
62       * </ul>
63       * @param request
64       * @param response
65       * @return The new session.
66       */
67      protected HttpSession renewSession(HttpServletRequest request, HttpServletResponse response)
68      {
69          HttpSession httpSession = request.getSession(false);
70         
71          //if we should renew sessions, and there is an existing session that may have been seen by non-authenticated users
72          //(indicated by SESSION_SECURED not being set on the session) then we should change id
73          if (_renewSession && httpSession!=null && httpSession.getAttribute(AbstractSessionManager.SESSION_KNOWN_ONLY_TO_AUTHENTICATED)!=Boolean.TRUE)
74          {
75              synchronized (this)
76              {
77                  httpSession = AbstractSessionManager.renewSession(request, httpSession,true);
78              }
79          }
80          return httpSession;
81      }
82  }