|
| 1 | +/* |
| 2 | + * Copyright 2011 ArcBees Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.gwtplatform.dispatch.rpc.server; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import javax.servlet.Filter; |
| 22 | +import javax.servlet.FilterChain; |
| 23 | +import javax.servlet.FilterConfig; |
| 24 | +import javax.servlet.ServletException; |
| 25 | +import javax.servlet.ServletRequest; |
| 26 | +import javax.servlet.ServletResponse; |
| 27 | +import javax.servlet.http.Cookie; |
| 28 | +import javax.servlet.http.HttpServletRequest; |
| 29 | +import javax.servlet.http.HttpServletResponse; |
| 30 | +import javax.servlet.http.HttpSession; |
| 31 | + |
| 32 | +/** |
| 33 | + * This filter will automatically inject a security cookie inside the request the first time the page is loaded. This |
| 34 | + * security cookie is based on the {@link HttpSession} and will only work if the session is enabled. To setup this |
| 35 | + * filter, add the following line at before any other {@code serve} call in your own |
| 36 | + * {@link com.google.inject.servlet.ServletModule#configureServlets}: |
| 37 | + * <p/> |
| 38 | + * <pre> |
| 39 | + * filter("*.jsp").through(HttpSessionSecurityCookieFilter.class); |
| 40 | + * </pre> |
| 41 | + * <p/> |
| 42 | + * You also have to use a {@code .jsp} file instead of a {@code .html} as your main GWT file. |
| 43 | + * |
| 44 | + * @deprecated Please use {@link com.gwtplatform.dispatch.rpc.server.AbstractRandomSecurityCookieFilter}. |
| 45 | + * Using the JSESSIONID like this might let an XSS attacker hijack a session. See GitHub issue #484 |
| 46 | + */ |
| 47 | +@Deprecated |
| 48 | +public abstract class AbstractHttpSessionSecurityCookieFilter implements Filter { |
| 49 | + |
| 50 | + private final String securityCookieName; |
| 51 | + |
| 52 | + protected AbstractHttpSessionSecurityCookieFilter(String securityCookieName) { |
| 53 | + this.securityCookieName = securityCookieName; |
| 54 | + } |
| 55 | + |
| 56 | + public void destroy() { |
| 57 | + } |
| 58 | + |
| 59 | + protected abstract HttpSession getSession(); |
| 60 | + |
| 61 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, |
| 62 | + ServletException { |
| 63 | + |
| 64 | + if (request instanceof HttpServletRequest) { |
| 65 | + HttpServletResponse httpResponse = (HttpServletResponse) response; |
| 66 | + Cookie securityCookie = new Cookie(securityCookieName, getSession().getId()); |
| 67 | + securityCookie.setMaxAge(-1); |
| 68 | + securityCookie.setPath("/"); |
| 69 | + httpResponse.addCookie(securityCookie); |
| 70 | + } |
| 71 | + chain.doFilter(request, response); |
| 72 | + } |
| 73 | + |
| 74 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 75 | + } |
| 76 | +} |
0 commit comments