|
| 1 | +/* |
| 2 | + * Copyright (C) 2013 Dominik Schadow, [email protected] |
| 3 | + * |
| 4 | + * This file is part of Java-Web-Security |
| 5 | +. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package de.dominikschadow.webappsecurity.filter; |
| 20 | + |
| 21 | +import org.apache.log4j.Logger; |
| 22 | + |
| 23 | +import javax.servlet.*; |
| 24 | +import javax.servlet.annotation.WebServlet; |
| 25 | +import javax.servlet.http.HttpServlet; |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | +import javax.servlet.http.HttpServletResponse; |
| 28 | +import javax.servlet.http.HttpSession; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.PrintWriter; |
| 31 | + |
| 32 | +/** |
| 33 | + * Filter to add the <code>Strict-Transport-Security</code> header to every response. |
| 34 | + * |
| 35 | + * @author Dominik Schadow |
| 36 | + */ |
| 37 | +public class HSTSFilter implements Filter { |
| 38 | + private static final Logger LOGGER = Logger.getLogger(HSTSFilter.class); |
| 39 | + |
| 40 | + @Override |
| 41 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 42 | + LOGGER.info("HSTSFilter init"); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { |
| 47 | + ((HttpServletResponse) res).setHeader("Strict-Transport-Security", "max-age=12960000; includeSubdomains"); |
| 48 | + LOGGER.info("Added Strict-Transport-Security header to response"); |
| 49 | + |
| 50 | + chain.doFilter(req, res); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void destroy() { |
| 55 | + } |
| 56 | +} |
0 commit comments