blob: 052d995eb74a31b56a7b6d7104c1f487421c5c98 [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>AuthorizationFilter.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 :: Web</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.web.filter.authz</a> &gt; <span class="el_source">AuthorizationFilter.java</span></div><h1>AuthorizationFilter.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.subject.Subject;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.WebUtils;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Superclass for authorization-related filters. If an request is unauthorized, response handling is delegated to the
* {@link #onAccessDenied(javax.servlet.ServletRequest, javax.servlet.ServletResponse) onAccessDenied} method, which
* provides reasonable handling for most applications.
*
* @see #onAccessDenied(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
* @since 0.9
*/
<span class="fc" id="L39">public abstract class AuthorizationFilter extends AccessControlFilter {</span>
/**
* The URL to which users should be redirected if they are denied access to an underlying path or resource,
* {@code null} by default which will issue a raw {@link HttpServletResponse#SC_UNAUTHORIZED} response
* (401 Unauthorized).
*/
private String unauthorizedUrl;
/**
* Returns the URL to which users should be redirected if they are denied access to an underlying path or resource,
* or {@code null} if a raw {@link HttpServletResponse#SC_UNAUTHORIZED} response should be issued (401 Unauthorized).
* &lt;p/&gt;
* The default is {@code null}, ensuring default web server behavior. Override this default by calling the
* {@link #setUnauthorizedUrl(String) setUnauthorizedUrl} method with a meaningful path within your application
* if you would like to show the user a 'nice' page in the event of unauthorized access.
*
* @return the URL to which users should be redirected if they are denied access to an underlying path or resource,
* or {@code null} if a raw {@link HttpServletResponse#SC_UNAUTHORIZED} response should be issued (401 Unauthorized).
*/
public String getUnauthorizedUrl() {
<span class="fc" id="L60"> return unauthorizedUrl;</span>
}
/**
* Sets the URL to which users should be redirected if they are denied access to an underlying path or resource.
* &lt;p/&gt;
* If the value is {@code null} a raw {@link HttpServletResponse#SC_UNAUTHORIZED} response will
* be issued (401 Unauthorized), retaining default web server behavior.
* &lt;p/&gt;
* Unless overridden by calling this method, the default value is {@code null}. If desired, you can specify a
* meaningful path within your application if you would like to show the user a 'nice' page in the event of
* unauthorized access.
*
* @param unauthorizedUrl the URL to which users should be redirected if they are denied access to an underlying
* path or resource, or {@code null} to a ensure raw {@link HttpServletResponse#SC_UNAUTHORIZED} response is
* issued (401 Unauthorized).
*/
public void setUnauthorizedUrl(String unauthorizedUrl) {
<span class="fc" id="L78"> this.unauthorizedUrl = unauthorizedUrl;</span>
<span class="fc" id="L79"> }</span>
/**
* Handles the response when access has been denied. It behaves as follows:
* &lt;ul&gt;
* &lt;li&gt;If the {@code Subject} is unknown&lt;sup&gt;&lt;a href=&quot;#known&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt;:
* &lt;ol&gt;&lt;li&gt;The incoming request will be saved and they will be redirected to the login page for authentication
* (via the {@link #saveRequestAndRedirectToLogin(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}
* method).&lt;/li&gt;
* &lt;li&gt;Once successfully authenticated, they will be redirected back to the originally attempted page.&lt;/li&gt;&lt;/ol&gt;
* &lt;/li&gt;
* &lt;li&gt;If the Subject is known:&lt;/li&gt;
* &lt;ol&gt;
* &lt;li&gt;The HTTP {@link HttpServletResponse#SC_UNAUTHORIZED} header will be set (401 Unauthorized)&lt;/li&gt;
* &lt;li&gt;If the {@link #getUnauthorizedUrl() unauthorizedUrl} has been configured, a redirect will be issued to that
* URL. Otherwise the 401 response is rendered normally&lt;/li&gt;
* &lt;/ul&gt;
* &lt;code&gt;&lt;a name=&quot;known&quot;&gt;[1]&lt;/a&gt;&lt;/code&gt;: A {@code Subject} is 'known' when
* &lt;code&gt;subject.{@link org.apache.shiro.subject.Subject#getPrincipal() getPrincipal()}&lt;/code&gt; is not {@code null},
* which implicitly means that the subject is either currently authenticated or they have been remembered via
* 'remember me' services.
*
* @param request the incoming &lt;code&gt;ServletRequest&lt;/code&gt;
* @param response the outgoing &lt;code&gt;ServletResponse&lt;/code&gt;
* @return {@code false} always for this implementation.
* @throws IOException if there is any servlet error.
*/
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
<span class="fc" id="L108"> Subject subject = getSubject(request, response);</span>
// If the subject isn't identified, redirect to login URL
<span class="pc bpc" id="L110" title="1 of 2 branches missed."> if (subject.getPrincipal() == null) {</span>
<span class="nc" id="L111"> saveRequestAndRedirectToLogin(request, response);</span>
} else {
// If subject is known but not authorized, redirect to the unauthorized URL if there is one
// If no unauthorized URL is specified, just return an unauthorized HTTP status code
<span class="fc" id="L115"> String unauthorizedUrl = getUnauthorizedUrl();</span>
//SHIRO-142 - ensure that redirect _or_ error code occurs - both cannot happen due to response commit:
<span class="fc bfc" id="L117" title="All 2 branches covered."> if (StringUtils.hasText(unauthorizedUrl)) {</span>
<span class="fc" id="L118"> WebUtils.issueRedirect(request, response, unauthorizedUrl);</span>
} else {
<span class="fc" id="L120"> WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);</span>
}
}
<span class="fc" id="L123"> 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>