blob: 65f2b877d3815e0ddcca3257ed6a7988ce19b9d7 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>PortFilter.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Test Coverage</a> &gt; <a href="../index.html" class="el_bundle">shiro-web</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.web.filter.authz</a> &gt; <span class="el_source">PortFilter.java</span></div><h1>PortFilter.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.web.filter.authz;
import org.apache.shiro.config.ConfigurationException;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.util.WebUtils;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* A Filter that requires the request to be on a specific port, and if not, redirects to the same URL on that port.
* &lt;p/&gt;
* Example config:
* &lt;pre&gt;
* [filters]
* port.port = 80
* &lt;p/&gt;
* [urls]
* /some/path/** = port
* # override for just this path:
* /another/path/** = port[8080]
* &lt;/pre&gt;
*
* @since 1.0
*/
<span class="fc" id="L46">public class PortFilter extends AuthorizationFilter {</span>
public static final int DEFAULT_HTTP_PORT = 80;
public static final String HTTP_SCHEME = &quot;http&quot;;
<span class="fc" id="L51"> private int port = DEFAULT_HTTP_PORT;</span>
public int getPort() {
<span class="fc" id="L54"> return port;</span>
}
public void setPort(int port) {
<span class="fc" id="L58"> this.port = port;</span>
<span class="fc" id="L59"> }</span>
protected int toPort(Object mappedValue) {
<span class="fc" id="L62"> String[] ports = (String[]) mappedValue;</span>
<span class="pc bpc" id="L63" title="3 of 4 branches missed."> if (ports == null || ports.length == 0) {</span>
<span class="fc" id="L64"> return getPort();</span>
}
<span class="nc bnc" id="L66" title="All 2 branches missed."> if (ports.length &gt; 1) {</span>
<span class="nc" id="L67"> throw new ConfigurationException(&quot;PortFilter can only be configured with a single port. You have &quot; +</span>
<span class="nc" id="L68"> &quot;configured &quot; + ports.length + &quot;: &quot; + StringUtils.toString(ports));</span>
}
<span class="nc" id="L70"> return Integer.parseInt(ports[0]);</span>
}
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
<span class="nc" id="L74"> int requiredPort = toPort(mappedValue);</span>
<span class="nc" id="L75"> int requestPort = request.getServerPort();</span>
<span class="nc bnc" id="L76" title="All 2 branches missed."> return requiredPort == requestPort;</span>
}
protected String getScheme(String requestScheme, int port) {
<span class="fc bfc" id="L80" title="All 2 branches covered."> if (port == DEFAULT_HTTP_PORT) {</span>
<span class="fc" id="L81"> return HTTP_SCHEME;</span>
<span class="pc bpc" id="L82" title="1 of 2 branches missed."> } else if (port == SslFilter.DEFAULT_HTTPS_PORT) {</span>
<span class="nc" id="L83"> return SslFilter.HTTPS_SCHEME;</span>
} else {
<span class="fc" id="L85"> return requestScheme;</span>
}
}
/**
* Redirects the request to the same exact incoming URL, but with the port listed in the filter's configuration.
*
* @param request the incoming &lt;code&gt;ServletRequest&lt;/code&gt;
* @param response the outgoing &lt;code&gt;ServletResponse&lt;/code&gt;
* @param mappedValue the config specified for the filter in the matching request's filter chain.
* @return {@code false} always to force a redirect.
*/
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {
//just redirect to the specified port:
<span class="fc" id="L101"> int port = toPort(mappedValue);</span>
<span class="fc" id="L103"> String scheme = getScheme(request.getScheme(), port);</span>
<span class="fc" id="L105"> StringBuilder sb = new StringBuilder();</span>
<span class="fc" id="L106"> sb.append(scheme).append(&quot;://&quot;);</span>
<span class="fc" id="L107"> sb.append(request.getServerName());</span>
<span class="pc bpc" id="L108" title="1 of 4 branches missed."> if (port != DEFAULT_HTTP_PORT &amp;&amp; port != SslFilter.DEFAULT_HTTPS_PORT) {</span>
<span class="fc" id="L109"> sb.append(&quot;:&quot;);</span>
<span class="fc" id="L110"> sb.append(port);</span>
}
<span class="pc bpc" id="L112" title="1 of 2 branches missed."> if (request instanceof HttpServletRequest) {</span>
<span class="fc" id="L113"> sb.append(WebUtils.toHttp(request).getRequestURI());</span>
<span class="fc" id="L114"> String query = WebUtils.toHttp(request).getQueryString();</span>
<span class="pc bpc" id="L115" title="1 of 2 branches missed."> if (query != null) {</span>
<span class="nc" id="L116"> sb.append(&quot;?&quot;).append(query);</span>
}
}
<span class="fc" id="L120"> WebUtils.issueRedirect(request, response, sb.toString());</span>
<span class="fc" id="L122"> return false;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html>