@Override protected Filter[] getServletFilters() { return new Filter[] { new HiddenHttpMethodFilter(), new DelegatingFilterProxy("a"), new DelegatingFilterProxy("b"), new DelegatingFilterProxy("c") }; }
/** * Create a new {@code DelegatingFilterProxy} that will retrieve the named target * bean from the given Spring {@code WebApplicationContext}. * <p>For use in Servlet 3.0+ environments where instance-based registration of * filters is supported. * <p>The target bean must implement the standard Servlet Filter interface. * <p>The given {@code WebApplicationContext} may or may not be refreshed when passed * in. If it has not, and if the context implements {@link ConfigurableApplicationContext}, * a {@link ConfigurableApplicationContext#refresh() refresh()} will be attempted before * retrieving the named target bean. * <p>This proxy's {@code Environment} will be inherited from the given * {@code WebApplicationContext}. * @param targetBeanName name of the target filter bean in the Spring application * context (must not be {@code null}). * @param wac the application context from which the target filter will be retrieved; * if {@code null}, an application context will be looked up from {@code ServletContext} * as a fallback. * @see #findWebApplicationContext() * @see #setEnvironment(org.springframework.core.env.Environment) */ public DelegatingFilterProxy(String targetBeanName, @Nullable WebApplicationContext wac) { Assert.hasText(targetBeanName, "Target Filter bean name must not be null or empty"); this.setTargetBeanName(targetBeanName); this.webApplicationContext = wac; if (wac != null) { this.setEnvironment(wac.getEnvironment()); } }
@Test public void testDelegatingFilterProxyWithFilterDelegateInstance() throws ServletException, IOException { MockFilter targetFilter = new MockFilter(); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(targetFilter); filterProxy.init(new MockFilterConfig(new MockServletContext())); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
@Override protected void initFilterBean() throws ServletException { synchronized (this.delegateMonitor) { if (this.delegate == null) { // If no target bean name specified, use filter name. if (this.targetBeanName == null) { this.targetBeanName = getFilterName(); } // Fetch Spring root application context and initialize the delegate early, // if possible. If the root application context will be started after this // filter proxy, we'll have to resort to lazy initialization. WebApplicationContext wac = findWebApplicationContext(); if (wac != null) { this.delegate = initDelegate(wac); } } } }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException { // Lazily initialize the delegate if necessary. Filter delegateToUse = this.delegate; if (delegateToUse == null) { synchronized (this.delegateMonitor) { delegateToUse = this.delegate; if (delegateToUse == null) { WebApplicationContext wac = findWebApplicationContext(); if (wac == null) { throw new IllegalStateException("No WebApplicationContext found: " + "no ContextLoaderListener or DispatcherServlet registered?"); } delegateToUse = initDelegate(wac); } this.delegate = delegateToUse; } } // Let the delegate perform the actual doFilter operation. invokeDelegate(delegateToUse, request, response, filterChain); }
@Bean("delegatingFilterProxy") public FilterRegistrationBean delegatingFilterProxy(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); DelegatingFilterProxy proxy = new DelegatingFilterProxy(); proxy.setTargetFilterLifecycle(true); proxy.setTargetBeanName("shiroFilter"); filterRegistrationBean.setFilter(proxy); return filterRegistrationBean; } }
@Test public void testDelegatingFilterProxyNotInjectedWacServletAttrPreferred() throws ServletException, IOException { ServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac); StaticWebApplicationContext wacToUse = new StaticWebApplicationContext(); wacToUse.setServletContext(sc); String beanName = "targetFilter"; String attrName = "customAttrName"; wacToUse.registerSingleton(beanName, MockFilter.class); wacToUse.refresh(); sc.setAttribute(attrName, wacToUse); MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName); filterProxy.setContextAttribute(attrName); filterProxy.setServletContext(sc); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
/** * Registers the springSessionRepositoryFilter. * @param servletContext the {@link ServletContext} */ private void insertSessionRepositoryFilter(ServletContext servletContext) { String filterName = DEFAULT_FILTER_NAME; DelegatingFilterProxy springSessionRepositoryFilter = new DelegatingFilterProxy( filterName); String contextAttribute = getWebApplicationContextAttribute(); if (contextAttribute != null) { springSessionRepositoryFilter.setContextAttribute(contextAttribute); } registerFilter(servletContext, true, filterName, springSessionRepositoryFilter); }
@Test(expected = IllegalStateException.class) public void testDelegatingFilterProxyWithTargetBeanNameAndNoApplicationContext() throws ServletException, IOException { MockServletContext sc = new MockServletContext(); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", null); filterProxy.init(new MockFilterConfig(sc)); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); // throws }
protected void registerRestApiServlet(ServletContext servletContext) { CubaRestApiServlet cubaRestApiServlet = new SingleAppRestApiServlet(dependencyJars); try { cubaRestApiServlet.init(new CubaServletConfig("rest_api", servletContext)); } catch (ServletException e) { throw new RuntimeException("An error occurred while initializing dispatcher servlet", e); } ServletRegistration.Dynamic cubaRestApiServletReg = servletContext.addServlet("rest_api", cubaRestApiServlet); cubaRestApiServletReg.setLoadOnStartup(2); cubaRestApiServletReg.addMapping("/rest/*"); DelegatingFilterProxy restSpringSecurityFilterChain = new DelegatingFilterProxy(); restSpringSecurityFilterChain.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.rest_api"); restSpringSecurityFilterChain.setTargetBeanName("springSecurityFilterChain"); FilterRegistration.Dynamic restSpringSecurityFilterChainReg = servletContext.addFilter("restSpringSecurityFilterChain", restSpringSecurityFilterChain); restSpringSecurityFilterChainReg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/rest/*"); }
@Test public void testDelegatingFilterProxyInjectedPreferred() throws ServletException, IOException { ServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.refresh(); sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac); StaticWebApplicationContext injectedWac = new StaticWebApplicationContext(); injectedWac.setServletContext(sc); String beanName = "targetFilter"; injectedWac.registerSingleton(beanName, MockFilter.class); injectedWac.refresh(); MockFilter targetFilter = (MockFilter) injectedWac.getBean(beanName); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName, injectedWac); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
@Override public void onStartup(final ServletContext servletContext) throws ServletException { super.onStartup(servletContext); DelegatingFilterProxy filter1 = new DelegatingFilterProxy("springSecurityFilterChain"); filter1.setServletContext(servletContext); filter1.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher"); DelegatingFilterProxy filter2 = new DelegatingFilterProxy("corsFilter"); servletContext.addFilter("springSecurityFilterChain", filter1).addMappingForUrlPatterns(null, true, "/*"); servletContext.addFilter("corsFilter",filter2).addMappingForUrlPatterns(null, false, "/*"); }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { try { super.doFilter(servletRequest, servletResponse, filterChain); } catch (RuntimeException e) { // Log unhandled exceptions logger.error(e.getMessage(), e); throw e; } if (servletRequest instanceof HttpServletRequest) { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpSession session = request.getSession(false); if (session != null) { // Log authentication errors if a backend authentication repository is unavailable, for example Throwable ex = (Throwable) session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION); if (ex instanceof AuthenticationServiceException) { logger.error("Authentication error.", ex); } } } } }
protected void initFilterBean() throws ServletException { // If no target bean name specified, use filter name. if (this.targetBeanName == null) { this.targetBeanName = getFilterName(); } // Fetch Spring root application context and initialize the delegate early, // if possible. If the root application context will be started after this // filter proxy, we'll have to resort to lazy initialization. WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); if (wac != null) { this.delegate = initDelegate(wac); } }
/** * If the proxy manager is available, filtering should be instead * delegated to its FilterChainProxy. */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException { WebApplicationContext context = super.findWebApplicationContext(); MotechProxyManager proxyManager = context.getBean(MotechProxyManager.class); if (isAdminMode) { anonymousFilter.doFilter(request, response, filterChain); } else if (proxyManager != null) { proxyManager.getFilterChainProxy().doFilter(request, response, filterChain); } else { super.doFilter(request, response, filterChain); } }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException { // Lazily initialize the delegate if necessary. if (this.delegate == null) { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); this.delegate = initDelegate(wac); } // Let the delegate perform the actual doFilter opeation. this.delegate.doFilter(request, response, filterChain); }
/** * As an optimisation to this approach, once the delegate is initialised we set a flag indicating the filter is * no longer bypassable. That way we don't continue to resolve the {@code WebApplicationContext} unnecessarily * on every request. * * @param wac the resolved {@code WebApplicationContext} * @return the targeted filter * @throws ServletException See documentation for the base class. * @see DelegatingFilterProxy#initDelegate(org.springframework.web.context.WebApplicationContext) */ @Override protected Filter initDelegate(WebApplicationContext wac) throws ServletException { LOG.debug("Filter [{}] is no longer bypassable; the WebApplicationContext is available", getFilterName()); bypassable = false; return super.initDelegate(wac); } }
@Bean public FilterRegistrationBean<DelegatingFilterProxy> delegatingFilterProxy(){ FilterRegistrationBean<DelegatingFilterProxy> filterRegistrationBean = new FilterRegistrationBean<DelegatingFilterProxy>(); DelegatingFilterProxy proxy = new DelegatingFilterProxy(); proxy.setTargetFilterLifecycle(true); proxy.setTargetBeanName("shiroFilter"); filterRegistrationBean.setFilter(proxy); return filterRegistrationBean; } }
@Test public void testDelegatingFilterProxyNotInjectedWithRootPreferred() throws ServletException, IOException { ServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.refresh(); sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac); sc.setAttribute("another", wac); StaticWebApplicationContext wacToUse = new StaticWebApplicationContext(); wacToUse.setServletContext(sc); String beanName = "targetFilter"; wacToUse.registerSingleton(beanName, MockFilter.class); wacToUse.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wacToUse); MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName); filterProxy.setServletContext(sc); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
/** * Registers the springSecurityFilterChain * @param servletContext the {@link ServletContext} */ private void insertSpringSecurityFilterChain(ServletContext servletContext) { String filterName = "springSecurityFilterChain"; DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy(filterName); String contextAttribute = getWebApplicationContextAttribute(); if(contextAttribute != null) { springSecurityFilterChain.setContextAttribute(contextAttribute); } registerFilter(servletContext, true, filterName, springSecurityFilterChain); }