View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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 LoginService _loginService;
33      protected IdentityService _identityService;
34      private boolean _renewSession;
35  
36      protected LoginAuthenticator()
37      {
38      }
39  
40      public void setConfiguration(AuthConfiguration configuration)
41      {
42          _loginService=configuration.getLoginService();
43          if (_loginService==null)
44              throw new IllegalStateException("No LoginService for "+this+" in "+configuration);
45          _identityService=configuration.getIdentityService();
46          if (_identityService==null)
47              throw new IllegalStateException("No IdentityService for "+this+" in "+configuration);
48          _renewSession=configuration.isSessionRenewedOnAuthentication();
49      }
50      
51      public LoginService getLoginService()
52      {
53          return _loginService;
54      }
55      
56      /** Change the session id.
57       * The session is changed to a new instance with a new ID if and only if:<ul>
58       * <li>A session exists.
59       * <li>The {@link AuthConfiguration#isSessionRenewedOnAuthentication()} returns true.
60       * <li>The session ID has been given to unauthenticated responses
61       * </ul>
62       * @param request
63       * @param response
64       * @return The new session.
65       */
66      protected HttpSession renewSession(HttpServletRequest request, HttpServletResponse response)
67      {
68          HttpSession httpSession = request.getSession(false);
69         
70          //if we should renew sessions, and there is an existing session that may have been seen by non-authenticated users
71          //(indicated by SESSION_SECURED not being set on the session) then we should change id
72          if (_renewSession && httpSession!=null && httpSession.getAttribute(AbstractSessionManager.SESSION_KNOWN_ONLY_TO_AUTHENTICATED)!=Boolean.TRUE)
73          {
74              synchronized (this)
75              {
76                  httpSession = AbstractSessionManager.renewSession(request, httpSession,true);
77              }
78          }
79          return httpSession;
80      }
81  }