Class FormAuthenticationFilter
- java.lang.Object
-
- org.apache.shiro.web.servlet.ServletContextSupport
-
- org.apache.shiro.web.servlet.AbstractFilter
-
- org.apache.shiro.web.servlet.NameableFilter
-
- org.apache.shiro.web.servlet.OncePerRequestFilter
-
- org.apache.shiro.web.servlet.AdviceFilter
-
- org.apache.shiro.web.filter.PathMatchingFilter
-
- org.apache.shiro.web.filter.AccessControlFilter
-
- org.apache.shiro.web.filter.authc.AuthenticationFilter
-
- org.apache.shiro.web.filter.authc.AuthenticatingFilter
-
- org.apache.shiro.web.filter.authc.FormAuthenticationFilter
-
- All Implemented Interfaces:
javax.servlet.Filter,org.apache.shiro.util.Nameable,PathConfigProcessor
public class FormAuthenticationFilter extends AuthenticatingFilter
Requires the requesting user to be authenticated for the request to continue, and if they are not, forces the user to login via by redirecting them to theloginUrlyou configure.This filter constructs a
UsernamePasswordTokenwith the values found inusername,password, andrememberMerequest parameters. It then callsSubject.login(usernamePasswordToken), effectively automatically performing a login attempt. Note that the login attempt will only occur when theisLoginSubmission(request,response)istrue, which by default occurs when the request is for theloginUrland is a POST request.If the login attempt fails, the resulting
AuthenticationExceptionfully qualified class name will be set as a request attribute under thefailureKeyAttributekey. This FQCN can be used as an i18n key or lookup mechanism to explain to the user why their login attempt failed (e.g. no account, incorrect password, etc).If you would prefer to handle the authentication validation and login in your own code, consider using the
PassThruAuthenticationFilterinstead, which allows requests to theAccessControlFilter.loginUrlto pass through to your application's code directly.- Since:
- 0.9
- See Also:
PassThruAuthenticationFilter
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_ERROR_KEY_ATTRIBUTE_NAMEstatic StringDEFAULT_PASSWORD_PARAMstatic StringDEFAULT_REMEMBER_ME_PARAMstatic StringDEFAULT_USERNAME_PARAM-
Fields inherited from class org.apache.shiro.web.filter.authc.AuthenticatingFilter
PERMISSIVE
-
Fields inherited from class org.apache.shiro.web.filter.authc.AuthenticationFilter
DEFAULT_SUCCESS_URL
-
Fields inherited from class org.apache.shiro.web.filter.AccessControlFilter
DEFAULT_LOGIN_URL, GET_METHOD, POST_METHOD
-
Fields inherited from class org.apache.shiro.web.filter.PathMatchingFilter
appliedPaths, pathMatcher
-
Fields inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
-
Fields inherited from class org.apache.shiro.web.servlet.AbstractFilter
filterConfig
-
-
Constructor Summary
Constructors Constructor Description FormAuthenticationFilter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.apache.shiro.authc.AuthenticationTokencreateToken(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)StringgetFailureKeyAttribute()protected StringgetPassword(javax.servlet.ServletRequest request)StringgetPasswordParam()StringgetRememberMeParam()protected StringgetUsername(javax.servlet.ServletRequest request)StringgetUsernameParam()protected booleanisLoginSubmission(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)This default implementation merely returnstrueif the request is an HTTPPOST,falseotherwise.protected booleanisRememberMe(javax.servlet.ServletRequest request)Returnstrueif "rememberMe" should be enabled for the login attempt associated with the currentrequest,falseotherwise.protected booleanonAccessDenied(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)Processes requests where the subject was denied access as determined by theisAccessAllowedmethod.protected booleanonLoginFailure(org.apache.shiro.authc.AuthenticationToken token, org.apache.shiro.authc.AuthenticationException e, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)protected booleanonLoginSuccess(org.apache.shiro.authc.AuthenticationToken token, org.apache.shiro.subject.Subject subject, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)protected voidsetFailureAttribute(javax.servlet.ServletRequest request, org.apache.shiro.authc.AuthenticationException ae)voidsetFailureKeyAttribute(String failureKeyAttribute)voidsetLoginUrl(String loginUrl)Sets the login URL used to authenticate a user.voidsetPasswordParam(String passwordParam)Sets the request parameter name to look for when acquiring the password.voidsetRememberMeParam(String rememberMeParam)Sets the request parameter name to look for when acquiring the rememberMe boolean value.voidsetUsernameParam(String usernameParam)Sets the request parameter name to look for when acquiring the username.-
Methods inherited from class org.apache.shiro.web.filter.authc.AuthenticatingFilter
cleanup, createToken, createToken, executeLogin, getHost, isAccessAllowed, isPermissive
-
Methods inherited from class org.apache.shiro.web.filter.authc.AuthenticationFilter
getSuccessUrl, issueSuccessRedirect, setSuccessUrl
-
Methods inherited from class org.apache.shiro.web.filter.AccessControlFilter
getLoginUrl, getSubject, isLoginRequest, onAccessDenied, onPreHandle, redirectToLogin, saveRequest, saveRequestAndRedirectToLogin
-
Methods inherited from class org.apache.shiro.web.filter.PathMatchingFilter
getPathWithinApplication, isEnabled, pathsMatch, pathsMatch, preHandle, processPathConfig
-
Methods inherited from class org.apache.shiro.web.servlet.AdviceFilter
afterCompletion, doFilterInternal, executeChain, postHandle
-
Methods inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
doFilter, getAlreadyFilteredAttributeName, isEnabled, isEnabled, isFilterOncePerRequest, setEnabled, setFilterOncePerRequest, shouldNotFilter
-
Methods inherited from class org.apache.shiro.web.servlet.NameableFilter
getName, setName, toStringBuilder
-
Methods inherited from class org.apache.shiro.web.servlet.AbstractFilter
destroy, getFilterConfig, getInitParam, init, onFilterConfigSet, setFilterConfig
-
Methods inherited from class org.apache.shiro.web.servlet.ServletContextSupport
getContextAttribute, getContextInitParam, getServletContext, removeContextAttribute, setContextAttribute, setServletContext, toString
-
-
-
-
Field Detail
-
DEFAULT_ERROR_KEY_ATTRIBUTE_NAME
public static final String DEFAULT_ERROR_KEY_ATTRIBUTE_NAME
- See Also:
- Constant Field Values
-
DEFAULT_USERNAME_PARAM
public static final String DEFAULT_USERNAME_PARAM
- See Also:
- Constant Field Values
-
DEFAULT_PASSWORD_PARAM
public static final String DEFAULT_PASSWORD_PARAM
- See Also:
- Constant Field Values
-
DEFAULT_REMEMBER_ME_PARAM
public static final String DEFAULT_REMEMBER_ME_PARAM
- See Also:
- Constant Field Values
-
-
Method Detail
-
setLoginUrl
public void setLoginUrl(String loginUrl)
Description copied from class:AccessControlFilterSets the login URL used to authenticate a user. Most Shiro filters use this url as the location to redirect a user when the filter requires authentication. Unless overridden, theDEFAULT_LOGIN_URLis assumed.- Overrides:
setLoginUrlin classAccessControlFilter- Parameters:
loginUrl- the login URL used to authenticate a user, used when redirecting users if authentication is required.
-
getUsernameParam
public String getUsernameParam()
-
setUsernameParam
public void setUsernameParam(String usernameParam)
Sets the request parameter name to look for when acquiring the username. Unless overridden by calling this method, the default isusername.- Parameters:
usernameParam- the name of the request param to check for acquiring the username.
-
getPasswordParam
public String getPasswordParam()
-
setPasswordParam
public void setPasswordParam(String passwordParam)
Sets the request parameter name to look for when acquiring the password. Unless overridden by calling this method, the default ispassword.- Parameters:
passwordParam- the name of the request param to check for acquiring the password.
-
getRememberMeParam
public String getRememberMeParam()
-
setRememberMeParam
public void setRememberMeParam(String rememberMeParam)
Sets the request parameter name to look for when acquiring the rememberMe boolean value. Unless overridden by calling this method, the default isrememberMe. RememberMe will betrueif the parameter value equals any of those supported byWebUtils.isTrue(request,value),falseotherwise.- Parameters:
rememberMeParam- the name of the request param to check for acquiring the rememberMe boolean value.
-
getFailureKeyAttribute
public String getFailureKeyAttribute()
-
setFailureKeyAttribute
public void setFailureKeyAttribute(String failureKeyAttribute)
-
onAccessDenied
protected boolean onAccessDenied(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws ExceptionDescription copied from class:AccessControlFilterProcesses requests where the subject was denied access as determined by theisAccessAllowedmethod.- Specified by:
onAccessDeniedin classAccessControlFilter- Parameters:
request- the incomingServletRequestresponse- the outgoingServletResponse- Returns:
trueif the request should continue to be processed; false if the subclass will handle/render the response directly.- Throws:
Exception- if there is an error processing the request.
-
isLoginSubmission
protected boolean isLoginSubmission(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)This default implementation merely returnstrueif the request is an HTTPPOST,falseotherwise. Can be overridden by subclasses for custom login submission detection behavior.- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponse.- Returns:
trueif the request is an HTTPPOST,falseotherwise.
-
createToken
protected org.apache.shiro.authc.AuthenticationToken createToken(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)- Specified by:
createTokenin classAuthenticatingFilter
-
isRememberMe
protected boolean isRememberMe(javax.servlet.ServletRequest request)
Description copied from class:AuthenticatingFilterReturnstrueif "rememberMe" should be enabled for the login attempt associated with the currentrequest,falseotherwise. This implementation always returnsfalseand is provided as a template hook to subclasses that supportrememberMelogins and wish to determinerememberMein a custom mannner based on the currentrequest.- Overrides:
isRememberMein classAuthenticatingFilter- Parameters:
request- the incoming ServletRequest- Returns:
trueif "rememberMe" should be enabled for the login attempt associated with the currentrequest,falseotherwise.
-
onLoginSuccess
protected boolean onLoginSuccess(org.apache.shiro.authc.AuthenticationToken token, org.apache.shiro.subject.Subject subject, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws Exception- Overrides:
onLoginSuccessin classAuthenticatingFilter- Throws:
Exception
-
onLoginFailure
protected boolean onLoginFailure(org.apache.shiro.authc.AuthenticationToken token, org.apache.shiro.authc.AuthenticationException e, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)- Overrides:
onLoginFailurein classAuthenticatingFilter
-
setFailureAttribute
protected void setFailureAttribute(javax.servlet.ServletRequest request, org.apache.shiro.authc.AuthenticationException ae)
-
getUsername
protected String getUsername(javax.servlet.ServletRequest request)
-
getPassword
protected String getPassword(javax.servlet.ServletRequest request)
-
-