View Javadoc

    1   /* Copyright 2004, 2005 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.wrapper;
    17
    18  import java.io.IOException;
    19
    20  import javax.servlet.Filter;
    21  import javax.servlet.FilterChain;
    22  import javax.servlet.FilterConfig;
    23  import javax.servlet.ServletException;
    24  import javax.servlet.ServletRequest;
    25  import javax.servlet.ServletResponse;
    26  import javax.servlet.http.HttpServletRequest;
    27
    28
    29  /***
    30   * A <code>Filter</code> which populates the <code>ServletRequest</code> with
    31   * an {@link SecurityContextHolderAwareRequestWrapper}.
    32   *
    33   * @author Orlando Garcia Carmona
    34   * @version $Id: SecurityContextHolderAwareRequestFilter.java,v 1.2 2005/11/17 00:56:30 benalex Exp $
    35   */
    36  public class SecurityContextHolderAwareRequestFilter implements Filter {
    37      //~ Methods ================================================================
    38
    39      public void destroy() {}
    40
    41      public void doFilter(ServletRequest servletRequest,
    42          ServletResponse servletResponse, FilterChain filterChain)
    43          throws IOException, ServletException {
    44          HttpServletRequest request = (HttpServletRequest) servletRequest;
    45
    46          if (!(request instanceof SecurityContextHolderAwareRequestWrapper)) {
    47              request = new SecurityContextHolderAwareRequestWrapper(request);
    48          }
    49
    50          filterChain.doFilter(request, servletResponse);
    51      }
    52
    53      public void init(FilterConfig filterConfig) throws ServletException {}
    54  }