public abstract class AccessControlFilter extends org.apache.shiro.web.servlet.AdviceFilter
saveRequestAndRedirectToLogin(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
which is used by many subclasses as the behavior when a user is unauthenticated.
This class and the subclasses that are used as Shiro's built-in filters were copied from Shiro 1.1.0
and modified locally to implement same behavior as specified in https://issues.apache.org/jira/browse/SHIRO-256
We'll revert to using Shiro's filters if the feature gets implemented in Shiro 2.xModifier and Type | Field and Description |
---|---|
static String |
GET_METHOD
Constant representing the HTTP 'GET' request method, equal to
GET . |
static String |
LOGIN_URL |
protected org.apache.shiro.util.PatternMatcher |
pathMatcher |
static String |
POST_METHOD
Constant representing the HTTP 'POST' request method, equal to
POST . |
static boolean |
REDIRECT_TO_SAVED_URL |
static String |
SUCCESS_URL |
static String |
TAPESTRY_VERSION |
static String |
UNAUTHORIZED_URL |
Constructor and Description |
---|
AccessControlFilter(LoginContextService loginContextService) |
Modifier and Type | Method and Description |
---|---|
void |
addConfig(String config) |
protected LoginContextService |
getLoginContextService() |
String |
getLoginUrl()
Returns the login URL used to authenticate a user.
|
protected org.apache.shiro.subject.Subject |
getSubject(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response)
Convenience method that acquires the Subject associated with the request.
|
String |
getSuccessUrl()
Returns the success url to use as the default location a user is sent after logging in.
|
String |
getUnauthorizedUrl() |
protected abstract boolean |
isAccessAllowed(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
Object mappedValue)
Returns
true if the request is allowed to proceed through the filter normally, or false
if the request should be handled by the
onAccessDenied(request,response,mappedValue)
method instead. |
protected boolean |
isLoginRequest(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response)
Returns
true if the incoming request is a login request, false otherwise. |
boolean |
isRedirectToSavedUrl() |
protected abstract boolean |
onAccessDenied(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response)
Processes requests where the subject was denied access as determined by the
isAccessAllowed
method. |
protected boolean |
onAccessDenied(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
Object mappedValue)
Processes requests where the subject was denied access as determined by the
isAccessAllowed
method, retaining the mappedValue that was used during configuration. |
boolean |
onPreHandle(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
Object mappedValue)
Returns
true if
isAccessAllowed(Request,Response,Object) ,
otherwise returns the result of
onAccessDenied(Request,Response,Object) . |
protected boolean |
preHandle(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response) |
protected void |
redirectToLogin(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response)
Convenience method for subclasses that merely acquires the
getLoginUrl and redirects
the request to that url. |
protected void |
saveRequest(javax.servlet.ServletRequest request)
Convenience method merely delegates to
WebUtils.saveRequest(request) to save the request
state for reuse later. |
protected void |
saveRequestAndRedirectToLogin(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response)
Convenience method for subclasses to use when a login redirect is required.
|
void |
setConfig(String config) |
void |
setLoginUrl(String loginUrl)
Sets the login URL used to authenticate a user.
|
void |
setRedirectToSavedUrl(boolean redirectToSavedUrl) |
void |
setSuccessUrl(String successUrl)
Sets the default/fallback success url to use as the default location a user is sent after logging in.
|
void |
setUnauthorizedUrl(String unauthorizedUrl) |
afterCompletion, cleanup, doFilterInternal, executeChain, postHandle
doFilter, getAlreadyFilteredAttributeName, isEnabled, isEnabled, setEnabled, shouldNotFilter
getName, setName, toStringBuilder
destroy, getFilterConfig, getInitParam, init, onFilterConfigSet, setFilterConfig
public static String TAPESTRY_VERSION
public static String SUCCESS_URL
public static String UNAUTHORIZED_URL
public static boolean REDIRECT_TO_SAVED_URL
protected org.apache.shiro.util.PatternMatcher pathMatcher
public static final String GET_METHOD
GET
.public static final String POST_METHOD
POST
.public AccessControlFilter(LoginContextService loginContextService)
public String getSuccessUrl()
#DEFAULT_SUCCESS_URL
.public void setSuccessUrl(String successUrl)
#DEFAULT_SUCCESS_URL
.successUrl
- the success URL to redirect the user to after a successful login.public String getLoginUrl()
DEFAULT_LOGIN_URL
is assumed, which can be overridden via
setLoginUrl
.public void setLoginUrl(String loginUrl)
DEFAULT_LOGIN_URL
is assumed.loginUrl
- the login URL used to authenticate a user, used when redirecting users if authentication is required.public String getUnauthorizedUrl()
public void setUnauthorizedUrl(String unauthorizedUrl)
protected org.apache.shiro.subject.Subject getSubject(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)
SecurityUtils.getSubject()
.request
- the incoming ServletRequest
response
- the outgoing ServletResponse
protected abstract boolean isAccessAllowed(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Object mappedValue) throws Exception
true
if the request is allowed to proceed through the filter normally, or false
if the request should be handled by the
onAccessDenied(request,response,mappedValue)
method instead.request
- the incoming ServletRequest
response
- the outgoing ServletResponse
mappedValue
- the filter-specific config value mapped to this filter in the URL rules mappings.true
if the request should proceed through the filter normally, false
if the
request should be processed by this filter's
onAccessDenied(ServletRequest,ServletResponse,Object)
method instead.Exception
- if an error occurs during processing.protected boolean onAccessDenied(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Object mappedValue) throws Exception
isAccessAllowed
method, retaining the mappedValue
that was used during configuration.
This method immediately delegates to onAccessDenied(ServletRequest,ServletResponse)
as a
convenience in that most post-denial behavior does not need the mapped config again.request
- the incoming ServletRequest
response
- the outgoing ServletResponse
mappedValue
- the config specified for the filter in the matching request's filter chain.true
if the request should continue to be processed; false if the subclass will
handle/render the response directly.Exception
- if there is an error processing the request.protected abstract boolean onAccessDenied(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws Exception
isAccessAllowed
method.request
- the incoming ServletRequest
response
- the outgoing ServletResponse
true
if the request should continue to be processed; false if the subclass will
handle/render the response directly.Exception
- if there is an error processing the request.public boolean onPreHandle(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Object mappedValue) throws Exception
true
if
isAccessAllowed(Request,Response,Object)
,
otherwise returns the result of
onAccessDenied(Request,Response,Object)
.true
if
isAccessAllowed
,
otherwise returns the result of
onAccessDenied
.Exception
- if an error occurs.protected boolean preHandle(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws Exception
preHandle
in class org.apache.shiro.web.servlet.AdviceFilter
Exception
protected boolean isLoginRequest(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)
true
if the incoming request is a login request, false
otherwise.
The default implementation merely returns true
if the incoming request matches the configured
loginUrl
by calling
pathsMatch(loginUrl, request)
.request
- the incoming ServletRequest
response
- the outgoing ServletResponse
true
if the incoming request is a login request, false
otherwise.protected void saveRequestAndRedirectToLogin(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws IOException
saveRequest(request)
and then redirectToLogin(request,response)
.request
- the incoming ServletRequest
response
- the outgoing ServletResponse
IOException
- if an error occurs.protected void saveRequest(javax.servlet.ServletRequest request)
WebUtils.saveRequest(request)
to save the request
state for reuse later. This is mostly used to retain user request state when a redirect is issued to
return the user to their originally requested url/resource.
If you need to save and then immediately redirect the user to login, consider using
saveRequestAndRedirectToLogin(request,response)
directly.request
- the incoming ServletRequest to save for re-use later (for example, after a redirect).protected void redirectToLogin(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws IOException
getLoginUrl
and redirects
the request to that url.
N.B. If you want to issue a redirect with the intention of allowing the user to then return to their
originally requested URL, don't use this method directly. Instead you should call
saveRequestAndRedirectToLogin(request,response)
, which will save the current request state so that it can
be reconstructed and re-used after a successful login.request
- the incoming ServletRequest
response
- the outgoing ServletResponse
IOException
- if an error occurs.public boolean isRedirectToSavedUrl()
public void setRedirectToSavedUrl(boolean redirectToSavedUrl)
protected LoginContextService getLoginContextService()
Copyright © 2004–2015. All rights reserved.