001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.web.filter;
020
021import org.apache.shiro.util.AntPathMatcher;
022import org.apache.shiro.util.PatternMatcher;
023import org.apache.shiro.web.servlet.AdviceFilter;
024import org.apache.shiro.web.util.WebUtils;
025import org.owasp.encoder.Encode;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import javax.servlet.Filter;
030import javax.servlet.ServletRequest;
031import javax.servlet.ServletResponse;
032import java.util.LinkedHashMap;
033import java.util.Map;
034
035import static org.apache.shiro.lang.util.StringUtils.split;
036
037/**
038 * <p>Base class for Filters that will process only specified paths and allow all others to pass through.</p>
039 *
040 * @since 0.9
041 */
042public abstract class PathMatchingFilter extends AdviceFilter implements PathConfigProcessor {
043
044    /**
045     * Log available to this class only
046     */
047    private static final Logger LOGGER = LoggerFactory.getLogger(PathMatchingFilter.class);
048
049    private static final String DEFAULT_PATH_SEPARATOR = "/";
050
051    /**
052     * PatternMatcher used in determining which paths to react to for a given request.
053     */
054    protected PatternMatcher pathMatcher = new AntPathMatcher();
055
056    /**
057     * A collection of path-to-config entries where the key is a path which this filter should process and
058     * the value is the (possibly null) configuration element specific to this Filter for that specific path.
059     * <p/>
060     * <p>To put it another way, the keys are the paths (urls) that this Filter will process.
061     * <p>The values are filter-specific data that this Filter should use when processing the corresponding
062     * key (path).  The values can be null if no Filter-specific config was specified for that url.
063     */
064    protected Map<String, Object> appliedPaths = new LinkedHashMap<String, Object>();
065
066    /**
067     * Splits any comma-delimited values that might be found in the <code>config</code> argument and sets the resulting
068     * <code>String[]</code> array on the <code>appliedPaths</code> internal Map.
069     * <p/>
070     * That is:
071     * <pre><code>
072     * String[] values = null;
073     * if (config != null) {
074     *     values = split(config);
075     * }
076     * <p/>
077     * this.{@link #appliedPaths appliedPaths}.put(path, values);
078     * </code></pre>
079     *
080     * @param path   the application context path to match for executing this filter.
081     * @param config the specified for <em>this particular filter only</em> for the given <code>path</code>
082     * @return this configured filter.
083     */
084    public Filter processPathConfig(String path, String config) {
085        String[] values = null;
086        if (config != null) {
087            values = split(config);
088        }
089
090        this.appliedPaths.put(path, values);
091        return this;
092    }
093
094    /**
095     * Returns the context path within the application based on the specified <code>request</code>.
096     * <p/>
097     * This implementation merely delegates to
098     * {@link WebUtils#getPathWithinApplication(javax.servlet.http.HttpServletRequest)
099     *      WebUtils.getPathWithinApplication(request)},
100     * but can be overridden by subclasses for custom logic.
101     *
102     * @param request the incoming <code>ServletRequest</code>
103     * @return the context path within the application.
104     */
105    protected String getPathWithinApplication(ServletRequest request) {
106        return WebUtils.getPathWithinApplication(WebUtils.toHttp(request));
107    }
108
109    /**
110     * Returns <code>true</code> if the incoming <code>request</code> matches the specified <code>path</code> pattern,
111     * <code>false</code> otherwise.
112     * <p/>
113     * The default implementation acquires the <code>request</code>'s path within the application and determines
114     * if that matches:
115     * <p/>
116     * <code>String requestURI = {@link #getPathWithinApplication(ServletRequest) getPathWithinApplication(request)};<br/>
117     * return {@link #pathsMatch(String, String) pathsMatch(path,requestURI)}</code>
118     *
119     * @param path    the configured url pattern to check the incoming request against.
120     * @param request the incoming ServletRequest
121     * @return <code>true</code> if the incoming <code>request</code> matches the specified <code>path</code> pattern,
122     * <code>false</code> otherwise.
123     */
124    protected boolean pathsMatch(String path, ServletRequest request) {
125        String requestURI = getPathWithinApplication(request);
126
127        LOGGER.trace("Attempting to match pattern '{}' with current requestURI '{}'...", path, Encode.forHtml(requestURI));
128        boolean match = pathsMatch(path, requestURI);
129
130        if (!match) {
131            if (requestURI != null && !DEFAULT_PATH_SEPARATOR.equals(requestURI)
132                    && requestURI.endsWith(DEFAULT_PATH_SEPARATOR)) {
133                requestURI = requestURI.substring(0, requestURI.length() - 1);
134            }
135            if (path != null && !DEFAULT_PATH_SEPARATOR.equals(path)
136                    && path.endsWith(DEFAULT_PATH_SEPARATOR)) {
137                path = path.substring(0, path.length() - 1);
138            }
139            LOGGER.trace("Attempting to match pattern '{}' with current requestURI '{}'...", path, Encode.forHtml(requestURI));
140            match = pathsMatch(path, requestURI);
141        }
142
143        return match;
144    }
145
146    /**
147     * Returns <code>true</code> if the <code>path</code> matches the specified <code>pattern</code> string,
148     * <code>false</code> otherwise.
149     * <p/>
150     * Simply delegates to
151     * <b><code>this.pathMatcher.{@link PatternMatcher#matches(String, String) matches(pattern,path)}</code></b>,
152     * but can be overridden by subclasses for custom matching behavior.
153     *
154     * @param pattern the pattern to match against
155     * @param path    the value to match with the specified <code>pattern</code>
156     * @return <code>true</code> if the <code>path</code> matches the specified <code>pattern</code> string,
157     * <code>false</code> otherwise.
158     */
159    protected boolean pathsMatch(String pattern, String path) {
160        boolean matches = pathMatcher.matches(pattern, path);
161        LOGGER.trace("Pattern [{}] matches path [{}] => [{}]", pattern, path, matches);
162        return matches;
163    }
164
165    /**
166     * Implementation that handles path-matching behavior before a request is evaluated.  If the path matches and
167     * the filter
168     * {@link #isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse, String, Object) isEnabled} for
169     * that path/config, the request will be allowed through via the result from
170     * {@link #onPreHandle(javax.servlet.ServletRequest, javax.servlet.ServletResponse, Object) onPreHandle}.  If the
171     * path does not match or the filter is not enabled for that path, this filter will allow passthrough immediately
172     * to allow the {@code FilterChain} to continue executing.
173     * <p/>
174     * In order to retain path-matching functionality, subclasses should not override this method if at all
175     * possible, and instead override
176     * {@link #onPreHandle(javax.servlet.ServletRequest, javax.servlet.ServletResponse, Object) onPreHandle} instead.
177     *
178     * @param request  the incoming ServletRequest
179     * @param response the outgoing ServletResponse
180     * @return {@code true} if the filter chain is allowed to continue to execute, {@code false} if a subclass has
181     * handled the request explicitly.
182     * @throws Exception if an error occurs
183     */
184    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
185
186        if (this.appliedPaths == null || this.appliedPaths.isEmpty()) {
187            if (LOGGER.isTraceEnabled()) {
188                LOGGER.trace("appliedPaths property is null or empty.  This Filter will passthrough immediately.");
189            }
190            return true;
191        }
192
193        for (String path : this.appliedPaths.keySet()) {
194            // If the path does match, then pass on to the subclass implementation for specific checks
195            //(first match 'wins'):
196            if (pathsMatch(path, request)) {
197                LOGGER.trace("Current requestURI matches pattern '{}'.  Determining filter chain execution...", path);
198                Object config = this.appliedPaths.get(path);
199                return isFilterChainContinued(request, response, path, config);
200            }
201        }
202
203        //no path matched, allow the request to go through:
204        return true;
205    }
206
207    /**
208     * Simple method to abstract out logic from the preHandle implementation - it was getting a bit unruly.
209     *
210     * @since 1.2
211     */
212    @SuppressWarnings({"JavaDoc"})
213    private boolean isFilterChainContinued(ServletRequest request, ServletResponse response,
214                                           String path, Object pathConfig) throws Exception {
215
216        //isEnabled check added in 1.2
217        if (isEnabled(request, response, path, pathConfig)) {
218            if (LOGGER.isTraceEnabled()) {
219                LOGGER.trace("Filter '{}' is enabled for the current request under path '{}' with config [{}].  "
220                                + "Delegating to subclass implementation for 'onPreHandle' check.",
221                        getName(), path, pathConfig);
222            }
223            //The filter is enabled for this specific request, so delegate to subclass implementations
224            //so they can decide if the request should continue through the chain or not:
225            return onPreHandle(request, response, pathConfig);
226        }
227
228        if (LOGGER.isTraceEnabled()) {
229            LOGGER.trace("Filter '{}' is disabled for the current request under path '{}' with config [{}].  "
230                            + "The next element in the FilterChain will be called immediately.",
231                    getName(), path, pathConfig);
232        }
233        //This filter is disabled for this specific request,
234        //return 'true' immediately to indicate that the filter will not process the request
235        //and let the request/response to continue through the filter chain:
236        return true;
237    }
238
239    /**
240     * This default implementation always returns {@code true} and should be overridden by subclasses for custom
241     * logic if necessary.
242     *
243     * @param request     the incoming ServletRequest
244     * @param response    the outgoing ServletResponse
245     * @param mappedValue the filter-specific config value mapped to this filter in the URL rules mappings.
246     * @return {@code true} if the request should be able to continue, {@code false} if the filter will
247     * handle the response directly.
248     * @throws Exception if an error occurs
249     * @see #isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse, String, Object)
250     */
251    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
252        return true;
253    }
254
255    @SuppressWarnings("UnusedParameters")
256    /**
257     * Path-matching version of the parent class's
258     * {@link #isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)} method, but additionally allows
259     * for inspection of any path-specific configuration values corresponding to the specified request.  Subclasses
260     * may wish to inspect this additional mapped configuration to determine if the filter is enabled or not.
261     * <p/>
262     * This method's default implementation ignores the {@code path} and {@code mappedValue} arguments and merely
263     * returns the value from a call to {@link #isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}.
264     * It is expected that subclasses override this method if they need to perform enable/disable logic for a specific
265     * request based on any path-specific config for the filter instance.
266     *
267     * @param request     the incoming servlet request
268     * @param response    the outbound servlet response
269     * @param path        the path matched for the incoming servlet request
270     *                    that has been configured with the given {@code mappedValue}.
271     * @param mappedValue the filter-specific config value mapped to
272     *                    this filter in the URL rules mappings for the given {@code path}.
273     * @return {@code true} if this filter should filter the specified request, {@code false} if it should let the
274     * request/response pass through immediately to the next element in the {@code FilterChain}.
275     * @throws Exception in the case of any error
276     * @since 1.2
277     */
278    protected boolean isEnabled(ServletRequest request, ServletResponse response, String path, Object mappedValue)
279            throws Exception {
280        return isEnabled(request, response);
281    }
282}