protected void addDefaultFilters(boolean init) { for (DefaultFilter defaultFilter : DefaultFilter.values()) { addFilter(defaultFilter.name(), defaultFilter.newInstance(), init, false); } } }
public void addFilter(String name, Filter filter, boolean init) { addFilter(name, filter, init, true); }
public void addToChain(String chainName, String filterName) { addToChain(chainName, filterName, null); }
public FilterChain getChain(ServletRequest request, ServletResponse response, FilterChain originalChain) { FilterChainManager filterChainManager = getFilterChainManager(); if (!filterChainManager.hasChains()) { return null; } String requestURI = getPathWithinApplication(request); //the 'chain names' in this implementation are actually path patterns defined by the user. We just use them //as the chain name for the FilterChainManager's requirements for (String pathPattern : filterChainManager.getChainNames()) { // If the path does match, then pass on to the subclass implementation for specific checks: if (pathMatches(pathPattern, requestURI)) { if (log.isTraceEnabled()) { log.trace("Matched path pattern [" + pathPattern + "] for requestURI [" + requestURI + "]. " + "Utilizing corresponding filter chain..."); } return filterChainManager.proxy(originalChain, pathPattern); } } return null; }
protected FilterChainResolver createInstance(Ini ini) { FilterChainResolver filterChainResolver = createDefaultInstance(); if (filterChainResolver instanceof PathMatchingFilterChainResolver) { PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) filterChainResolver; FilterChainManager manager = resolver.getFilterChainManager(); buildChains(manager, ini); } return filterChainResolver; }
protected FilterChainResolver createDefaultInstance() { FilterConfig filterConfig = getFilterConfig(); if (filterConfig != null) { return new PathMatchingFilterChainResolver(filterConfig); } else { return new PathMatchingFilterChainResolver(); } }
public PathMatchingFilterChainResolver() { this.pathMatcher = new AntPathMatcher(); this.filterChainManager = new DefaultFilterChainManager(); }
public void addToChain(String chainName, String filterName, String chainSpecificFilterConfig) { if (!StringUtils.hasText(chainName)) { throw new IllegalArgumentException("chainName cannot be null or empty."); } Filter filter = getFilter(filterName); if (filter == null) { throw new IllegalArgumentException("There is no filter with name '" + filterName + "' to apply to chain [" + chainName + "] in the pool of available Filters. Ensure a " + "filter with that name/path has first been registered with the addFilter method(s)."); } applyChainConfig(chainName, filter, chainSpecificFilterConfig); NamedFilterList chain = ensureChain(chainName); chain.add(filter); }
@Test public void testWithConfig() throws Exception { FilterChainResolver resolver = setupResolver(); HttpServletResponse response = createNiceMock(HttpServletResponse.class); FilterChain chain = createNiceMock(FilterChain.class); HttpServletRequest request = createMockRequest("/index2.html"); FilterChain resolved = resolver.getChain(request, response, chain); assertNotNull(resolved); verify(request); }
protected NamedFilterList ensureChain(String chainName) { NamedFilterList chain = getChain(chainName); if (chain == null) { chain = new SimpleNamedFilterList(chainName); this.filterChains.put(chainName, chain); } return chain; }
public FilterChain proxy(FilterChain original, String chainName) { NamedFilterList configured = getChain(chainName); if (configured == null) { String msg = "There is no configured chain under the name/key [" + chainName + "]."; throw new IllegalArgumentException(msg); } return configured.proxy(original); }
public DefaultFilterChainManager(FilterConfig filterConfig) { this.filters = new LinkedHashMap<String, Filter>(); this.filterChains = new LinkedHashMap<String, NamedFilterList>(); setFilterConfig(filterConfig); addDefaultFilters(true); }
protected void addFilter(String name, Filter filter, boolean init, boolean overwrite) { Filter existing = getFilter(name); if (existing == null || overwrite) { if (filter instanceof Nameable) { ((Nameable) filter).setName(name); } if (init) { initFilter(filter); } this.filters.put(name, filter); } }
@SuppressWarnings({"unchecked"}) @Override protected Map<String, ?> createDefaults(Ini ini, Ini.Section mainSection) { Map defaults = super.createDefaults(ini, mainSection); //add the default filters: Map<String, Filter> defaultFilters = DefaultFilter.createInstanceMap(null); defaults.putAll(defaultFilters); return defaults; } }
/** * Creates a new {@code SimpleNamedFilterList} instance with the specified {@code name} and {@code backingList}. * * @param name the name to assign to this instance. * @param backingList the list instance used to back all of this class's {@link List} method implementations. * @throws IllegalArgumentException if {@code name} is null or empty. * @throws NullPointerException if the backing list is {@code null}. */ public SimpleNamedFilterList(String name, List<Filter> backingList) { if (backingList == null) { throw new NullPointerException("backingList constructor argument cannot be null."); } this.backingList = backingList; setName(name); }
public DefaultFilterChainManager() { this.filters = new LinkedHashMap<String, Filter>(); this.filterChains = new LinkedHashMap<String, NamedFilterList>(); addDefaultFilters(false); }
/** * Returns {@code true} if an incoming request path (the {@code path} argument) * matches a configured filter chain path (the {@code pattern} argument), {@code false} otherwise. * <p/> * Simply delegates to * <b><code>{@link #getPathMatcher() getPathMatcher()}.{@link org.apache.shiro.util.PatternMatcher#matches(String, String) matches(pattern,path)}</code></b>. * Subclass implementors should think carefully before overriding this method, as typically a custom * {@code PathMatcher} should be configured for custom path matching behavior instead. Favor OO composition * rather than inheritance to limit your exposure to Shiro implementation details which may change over time. * * @param pattern the pattern to match against * @param path the value to match with the specified {@code pattern} * @return {@code true} if the request {@code path} matches the specified filter chain url {@code pattern}, * {@code false} otherwise. */ protected boolean pathMatches(String pattern, String path) { PatternMatcher pathMatcher = getPathMatcher(); return pathMatcher.matches(pattern, path); }
/** * Initializes the filter by calling <code>filter.init( {@link #getFilterConfig() getFilterConfig()} );</code>. * * @param filter the filter to initialize with the {@code FilterConfig}. */ protected void initFilter(Filter filter) { FilterConfig filterConfig = getFilterConfig(); if (filterConfig == null) { throw new IllegalStateException("FilterConfig attribute has not been set. This must occur before filter " + "initialization can occur."); } try { filter.init(filterConfig); } catch (ServletException e) { throw new ConfigurationException(e); } }
public PathMatchingFilterChainResolver(FilterConfig filterConfig) { this.pathMatcher = new AntPathMatcher(); this.filterChainManager = new DefaultFilterChainManager(filterConfig); }
public void addFilter(String name, Filter filter) { addFilter(name, filter, false); }