@Test public void testDoFilter() throws IOException, ServletException { req.setMethod("POST"); FilterChain chain = new MockFilterChain(); req.setCookies(new Cookie[] {new Cookie("JSESSIONID", "test5")}); remoteCookieCheckFilter.doFilter(req, res, chain); String attribute = (String) req.getSession().getAttribute("COOKIE_CHECK_REFERER"); Assert.assertNull(attribute); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) request; if (!"POST".equals(httpServletRequest.getMethod())) { boolean cookieFound = false; Cookie[] cookies = httpServletRequest.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase(COOKIE_NAME)) { cookieFound = true; break; } } } String userAgent = ((HttpServletRequest) request).getHeader("User-Agent"); if (!cookieFound && !userAgentInIgnoreList(userAgent)) { final HttpSession session = httpServletRequest.getSession(true); String requestURI = httpServletRequest.getRequestURI(); final String queryString = httpServletRequest.getQueryString(); if (queryString != null) { requestURI += "?" + queryString; } session.setAttribute(REFERER_ATTRIBUTE, requestURI); String url = httpServletRequest.getContextPath() + "/api" + RemoteCookieCheckController.COOKIE_CHECK_REQUEST_MAPPING; ((HttpServletResponse) response).sendRedirect(url); return; } } chain.doFilter(request, response); }
RemoteCookieCheckFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) request; if (!"POST".equals(httpServletRequest.getMethod())) { boolean cookieFound = false; Cookie[] cookies = httpServletRequest.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase(COOKIE_NAME)) { cookieFound = true; break; } } } String userAgent = ((HttpServletRequest) request).getHeader("User-Agent"); if (!cookieFound && !userAgentInIgnoreList(userAgent)) { final HttpSession session = httpServletRequest.getSession(true); String requestURI = httpServletRequest.getRequestURI(); final String queryString = httpServletRequest.getQueryString(); if (queryString != null) { requestURI += "?" + queryString; } session.setAttribute(REFERER_ATTRIBUTE, requestURI); String url = httpServletRequest.getContextPath() + "/api" + RemoteCookieCheckController.COOKIE_CHECK_REQUEST_MAPPING; ((HttpServletResponse) response).sendRedirect(url); return; } } chain.doFilter(request, response); } }
RemoteCookieCheckFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) request; if (!"POST".equals(httpServletRequest.getMethod())) { boolean cookieFound = false; Cookie[] cookies = httpServletRequest.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase(COOKIE_NAME)) { cookieFound = true; break; } } } String userAgent = ((HttpServletRequest) request).getHeader("User-Agent"); if (!cookieFound && !userAgentInIgnoreList(userAgent)) { final HttpSession session = httpServletRequest.getSession(true); String requestURI = httpServletRequest.getRequestURI(); final String queryString = httpServletRequest.getQueryString(); if (queryString != null) { requestURI += "?" + queryString; } session.setAttribute(REFERER_ATTRIBUTE, requestURI); String url = httpServletRequest.getContextPath() + "/api" + RemoteCookieCheckController.COOKIE_CHECK_REQUEST_MAPPING; ((HttpServletResponse) response).sendRedirect(url); return; } } chain.doFilter(request, response); }  }
RemoteCookieCheckFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) request; if (!"POST".equals(httpServletRequest.getMethod())) { boolean cookieFound = false; Cookie[] cookies = httpServletRequest.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase(COOKIE_NAME)) { cookieFound = true; break; } } } String userAgent = ((HttpServletRequest) request).getHeader("User-Agent"); if (!cookieFound && !userAgentInIgnoreList(userAgent)) { final HttpSession session = httpServletRequest.getSession(true); String requestURI = httpServletRequest.getRequestURI(); final String queryString = httpServletRequest.getQueryString(); if (queryString != null) { requestURI += "?" + queryString; } session.setAttribute(REFERER_ATTRIBUTE, requestURI); String url = httpServletRequest.getContextPath() + "/api" + RemoteCookieCheckController.COOKIE_CHECK_REQUEST_MAPPING; ((HttpServletResponse) response).sendRedirect(url); return; } } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy(); void setRegexIgnoredUserAgents(Set<String> regexIgnoredUserAgents); }
RemoteCookieCheckFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) request; if (!"POST".equals(httpServletRequest.getMethod())) { boolean cookieFound = false; Cookie[] cookies = httpServletRequest.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase(COOKIE_NAME)) { cookieFound = true; break; } } } String userAgent = ((HttpServletRequest) request).getHeader("User-Agent"); if (!cookieFound && !userAgentInIgnoreList(userAgent)) { final HttpSession session = httpServletRequest.getSession(true); String requestURI = httpServletRequest.getRequestURI(); final String queryString = httpServletRequest.getQueryString(); if (queryString != null) { requestURI += "?" + queryString; } session.setAttribute(REFERER_ATTRIBUTE, requestURI); String url = httpServletRequest.getContextPath() + "/api" + RemoteCookieCheckController.COOKIE_CHECK_REQUEST_MAPPING; ((HttpServletResponse) response).sendRedirect(url); return; } } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy(); void setRegexIgnoredUserAgents(Set<String> regexIgnoredUserAgents); static final String COOKIE_NAME; static final String REFERER_ATTRIBUTE; }