View Javadoc

    1   /*
    2    * Licensed to the University Corporation for Advanced Internet Development,
    3    * Inc. (UCAID) under one or more contributor license agreements.  See the
    4    * NOTICE file distributed with this work for additional information regarding
    5    * copyright ownership. The UCAID licenses this file to You under the Apache
    6    * License, Version 2.0 (the "License"); you may not use this file except in
    7    * compliance with the License.  You may obtain a copy of the License at
    8    *
    9    *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package edu.internet2.middleware.shibboleth.idp.util;
    19
    20  import java.io.IOException;
    21
    22  import javax.servlet.Filter;
    23  import javax.servlet.FilterChain;
    24  import javax.servlet.FilterConfig;
    25  import javax.servlet.ServletException;
    26  import javax.servlet.ServletRequest;
    27  import javax.servlet.ServletResponse;
    28  import javax.servlet.http.HttpServletResponse;
    29
    30  /**
    31   * An HTTP filter that adds the following headers/values to the {@link HttpServletResponse} and thus, hopefully,
    32   * prevents caching of the response on all browser.
    33   * <ul>
    34   * <li>Expires: 0</li>
    35   * <li>Cache-Control: no-cache, no-store, must-revalidate, max-age=0
    36   * <li>
    37   * <li>Pragma: no-cache</li>
    38   * </ul>
    39   */
    40  public class NoCacheFilter implements Filter {
    41
    42      /** {@inheritDoc} */
    43      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
    44              ServletException {
    45          HttpServletResponse httpResponse = (HttpServletResponse) response;
    46
    47          httpResponse.setHeader("Expires", "0");
    48          httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0");
    49          httpResponse.setHeader("Pragma", "no-cache");
    50          chain.doFilter(request, response);
    51      }
    52
    53      /** {@inheritDoc} */
    54      public void init(FilterConfig filterConfig) throws ServletException {
    55          // nothing to do here
    56      }
    57
    58      /** {@inheritDoc} */
    59      public void destroy() {
    60          // nothing to do here
    61      }
    62  }