Coverage Report - org.acegisecurity.ui.rememberme.RememberMeProcessingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
RememberMeProcessingFilter
78% 
88% 
2.375
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.ui.rememberme;
 17  
 
 18  
 import org.acegisecurity.Authentication;
 19  
 import org.acegisecurity.AuthenticationException;
 20  
 import org.acegisecurity.AuthenticationManager;
 21  
 
 22  
 import org.acegisecurity.context.SecurityContextHolder;
 23  
 
 24  
 import org.acegisecurity.event.authentication.InteractiveAuthenticationSuccessEvent;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 import org.springframework.beans.factory.InitializingBean;
 30  
 
 31  
 import org.springframework.context.ApplicationEventPublisher;
 32  
 import org.springframework.context.ApplicationEventPublisherAware;
 33  
 
 34  
 import org.springframework.util.Assert;
 35  
 
 36  
 import java.io.IOException;
 37  
 
 38  
 import javax.servlet.Filter;
 39  
 import javax.servlet.FilterChain;
 40  
 import javax.servlet.FilterConfig;
 41  
 import javax.servlet.ServletException;
 42  
 import javax.servlet.ServletRequest;
 43  
 import javax.servlet.ServletResponse;
 44  
 import javax.servlet.http.HttpServletRequest;
 45  
 import javax.servlet.http.HttpServletResponse;
 46  
 
 47  
 
 48  
 /**
 49  
  * Detects if there is no <code>Authentication</code> object in the <code>SecurityContext</code>, and populates it
 50  
  * with a remember-me authentication token if a {@link org.acegisecurity.ui.rememberme.RememberMeServices}
 51  
  * implementation so requests.<p>Concrete <code>RememberMeServices</code> implementations will have their {@link
 52  
  * org.acegisecurity.ui.rememberme.RememberMeServices#autoLogin(HttpServletRequest, HttpServletResponse)} method
 53  
  * called by this filter. The <code>Authentication</code> or <code>null</code> returned by that method will be placed
 54  
  * into the <code>SecurityContext</code>. The <code>AuthenticationManager</code> will be used, so that any concurrent
 55  
  * session management or other authentication-specific behaviour can be achieved. This is the same pattern as with
 56  
  * other authentication mechanisms, which call the <code>AuthenticationManager</code> as part of their contract.</p>
 57  
  *  <p>If authentication is successful, an {@link
 58  
  * org.acegisecurity.event.authentication.InteractiveAuthenticationSuccessEvent} will be published to the application
 59  
  * context. No events will be published if authentication was unsuccessful, because this would generally be recorded
 60  
  * via an <code>AuthenticationManager</code>-specific application event.</p>
 61  
  *  <p><b>Do not use this class directly.</b> Instead configure <code>web.xml</code> to use the {@link
 62  
  * org.acegisecurity.util.FilterToBeanProxy}.</p>
 63  
  *
 64  
  * @author Ben Alex
 65  
  * @version $Id: RememberMeProcessingFilter.java 1948 2007-08-25 00:15:30Z benalex $
 66  
  */
 67  6
 public class RememberMeProcessingFilter implements Filter, InitializingBean, ApplicationEventPublisherAware {
 68  
     //~ Static fields/initializers =====================================================================================
 69  
 
 70  2
     private static final Log logger = LogFactory.getLog(RememberMeProcessingFilter.class);
 71  
 
 72  
     //~ Instance fields ================================================================================================
 73  
 
 74  
     private ApplicationEventPublisher eventPublisher;
 75  
     private AuthenticationManager authenticationManager;
 76  6
     private RememberMeServices rememberMeServices = new NullRememberMeServices();
 77  
 
 78  
     //~ Methods ========================================================================================================
 79  
 
 80  
     public void afterPropertiesSet() throws Exception {
 81  5
                 Assert.notNull(authenticationManager, "authenticationManager must be specified");
 82  4
                 Assert.notNull(this.rememberMeServices);
 83  3
         }
 84  
 
 85  
     /**
 86  
      * Does nothing - we rely on IoC lifecycle services instead.
 87  
      */
 88  2
     public void destroy() {}
 89  
 
 90  
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 91  
         throws IOException, ServletException {
 92  4
         if (!(request instanceof HttpServletRequest)) {
 93  1
             throw new ServletException("Can only process HttpServletRequest");
 94  
         }
 95  
 
 96  3
         if (!(response instanceof HttpServletResponse)) {
 97  1
             throw new ServletException("Can only process HttpServletResponse");
 98  
         }
 99  
 
 100  2
         HttpServletRequest httpRequest = (HttpServletRequest) request;
 101  2
         HttpServletResponse httpResponse = (HttpServletResponse) response;
 102  
 
 103  2
         if (SecurityContextHolder.getContext().getAuthentication() == null) {
 104  1
             Authentication rememberMeAuth = rememberMeServices.autoLogin(httpRequest, httpResponse);
 105  
 
 106  1
             if (rememberMeAuth != null) {
 107  
                 // Attempt authenticaton via AuthenticationManager
 108  
                 try {
 109  1
                         rememberMeAuth = authenticationManager.authenticate(rememberMeAuth);
 110  
 
 111  
                     // Store to SecurityContextHolder
 112  1
                     SecurityContextHolder.getContext().setAuthentication(rememberMeAuth);
 113  
 
 114  1
                     if (logger.isDebugEnabled()) {
 115  0
                         logger.debug("SecurityContextHolder populated with remember-me token: '"
 116  
                             + SecurityContextHolder.getContext().getAuthentication() + "'");
 117  
                     }
 118  
 
 119  
                     // Fire event
 120  1
                     if (this.eventPublisher != null) {
 121  0
                         eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(
 122  
                                 SecurityContextHolder.getContext().getAuthentication(), this.getClass()));
 123  
                     }
 124  0
                 } catch (AuthenticationException authenticationException) {
 125  0
                     if (logger.isDebugEnabled()) {
 126  0
                         logger.debug("SecurityContextHolder not populated with remember-me token, as "
 127  
                                 + "AuthenticationManager rejected Authentication returned by RememberMeServices: '"
 128  
                                 + rememberMeAuth + "'; invalidating remember-me token", authenticationException);
 129  
                     }
 130  
 
 131  0
                     rememberMeServices.loginFail(httpRequest, httpResponse);
 132  1
                 }
 133  
             }
 134  
 
 135  1
             chain.doFilter(request, response);
 136  1
         } else {
 137  1
             if (logger.isDebugEnabled()) {
 138  0
                 logger.debug("SecurityContextHolder not populated with remember-me token, as it already contained: '"
 139  
                     + SecurityContextHolder.getContext().getAuthentication() + "'");
 140  
             }
 141  
 
 142  1
             chain.doFilter(request, response);
 143  
         }
 144  2
     }
 145  
 
 146  
     public RememberMeServices getRememberMeServices() {
 147  2
         return rememberMeServices;
 148  
     }
 149  
 
 150  
     /**
 151  
      * Does nothing - we rely on IoC lifecycle services instead.
 152  
      *
 153  
      * @param ignored not used
 154  
      *
 155  
      * @throws ServletException DOCUMENT ME!
 156  
      */
 157  2
     public void init(FilterConfig ignored) throws ServletException {}
 158  
 
 159  
     public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
 160  0
         this.eventPublisher = eventPublisher;
 161  0
     }
 162  
 
 163  
     public void setAuthenticationManager(AuthenticationManager authenticationManager) {
 164  7
         this.authenticationManager = authenticationManager;
 165  7
     }
 166  
 
 167  
     public void setRememberMeServices(RememberMeServices rememberMeServices) {
 168  4
         this.rememberMeServices = rememberMeServices;
 169  4
     }
 170  
 
 171  
 }