public class FormAuthenticationFilter extends AuthenticatingFilter
loginUrl
you configure.
This filter constructs a UsernamePasswordToken
with the values found in
username
, password
,
and rememberMe
request parameters. It then calls
Subject.login(usernamePasswordToken)
,
effectively automatically performing a login attempt. Note that the login attempt will only occur when the
isLoginSubmission(request,response)
is true
, which by default occurs when the request is for the loginUrl
and
is a POST request.
If the login attempt fails, the resulting AuthenticationException
fully qualified class name will
be set as a request attribute under the failureKeyAttribute
key. 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
PassThruAuthenticationFilter
instead, which allows requests to the
AccessControlFilter.loginUrl
to pass through to your application's code directly.
PassThruAuthenticationFilter
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ERROR_KEY_ATTRIBUTE_NAME |
static String |
DEFAULT_PASSWORD_PARAM |
static String |
DEFAULT_REMEMBER_ME_PARAM |
static String |
DEFAULT_USERNAME_PARAM |
PERMISSIVE
DEFAULT_SUCCESS_URL
DEFAULT_LOGIN_URL, GET_METHOD, POST_METHOD
appliedPaths, pathMatcher
ALREADY_FILTERED_SUFFIX
filterConfig
Constructor and Description |
---|
FormAuthenticationFilter() |
Modifier and Type | Method and Description |
---|---|
protected AuthenticationToken |
createToken(ServletRequest request,
ServletResponse response) |
String |
getFailureKeyAttribute() |
protected String |
getPassword(ServletRequest request) |
String |
getPasswordParam() |
String |
getRememberMeParam() |
protected String |
getUsername(ServletRequest request) |
String |
getUsernameParam() |
protected boolean |
isLoginSubmission(ServletRequest request,
ServletResponse response)
This default implementation merely returns
true if the request is an HTTP POST ,
false otherwise. |
protected boolean |
isRememberMe(ServletRequest request)
Returns
true if "rememberMe" should be enabled for the login attempt associated with the
current request , false otherwise. |
protected boolean |
onAccessDenied(ServletRequest request,
ServletResponse response)
Processes requests where the subject was denied access as determined by the
isAccessAllowed
method. |
protected boolean |
onLoginFailure(AuthenticationToken token,
AuthenticationException e,
ServletRequest request,
ServletResponse response) |
protected boolean |
onLoginSuccess(AuthenticationToken token,
Subject subject,
ServletRequest request,
ServletResponse response) |
protected void |
setFailureAttribute(ServletRequest request,
AuthenticationException ae) |
void |
setFailureKeyAttribute(String failureKeyAttribute) |
void |
setLoginUrl(String loginUrl)
Sets the login URL used to authenticate a user.
|
void |
setPasswordParam(String passwordParam)
Sets the request parameter name to look for when acquiring the password.
|
void |
setRememberMeParam(String rememberMeParam)
Sets the request parameter name to look for when acquiring the rememberMe boolean value.
|
void |
setUsernameParam(String usernameParam)
Sets the request parameter name to look for when acquiring the username.
|
cleanup, createToken, createToken, executeLogin, getHost, isAccessAllowed, isPermissive
getSuccessUrl, issueSuccessRedirect, setSuccessUrl
getLoginUrl, getSubject, isLoginRequest, onAccessDenied, onPreHandle, redirectToLogin, saveRequest, saveRequestAndRedirectToLogin
getPathWithinApplication, isEnabled, pathsMatch, pathsMatch, preHandle, processPathConfig
afterCompletion, doFilterInternal, executeChain, postHandle
doFilter, getAlreadyFilteredAttributeName, isEnabled, isEnabled, isFilterOncePerRequest, setEnabled, setFilterOncePerRequest, shouldNotFilter
getName, setName, toStringBuilder
destroy, getFilterConfig, getInitParam, init, onFilterConfigSet, setFilterConfig
getContextAttribute, getContextInitParam, getServletContext, removeContextAttribute, setContextAttribute, setServletContext, toString
public static final String DEFAULT_ERROR_KEY_ATTRIBUTE_NAME
public static final String DEFAULT_USERNAME_PARAM
public static final String DEFAULT_PASSWORD_PARAM
public static final String DEFAULT_REMEMBER_ME_PARAM
public FormAuthenticationFilter()
public void setLoginUrl(String loginUrl)
AccessControlFilter
DEFAULT_LOGIN_URL
is assumed.setLoginUrl
in class AccessControlFilter
loginUrl
- the login URL used to authenticate a user, used when redirecting users if authentication is required.public String getUsernameParam()
public void setUsernameParam(String usernameParam)
username
.usernameParam
- the name of the request param to check for acquiring the username.public String getPasswordParam()
public void setPasswordParam(String passwordParam)
password
.passwordParam
- the name of the request param to check for acquiring the password.public String getRememberMeParam()
public void setRememberMeParam(String rememberMeParam)
rememberMe
.
RememberMe will be true
if the parameter value equals any of those supported by
WebUtils.isTrue(request,value)
, false
otherwise.rememberMeParam
- the name of the request param to check for acquiring the rememberMe boolean value.public String getFailureKeyAttribute()
public void setFailureKeyAttribute(String failureKeyAttribute)
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception
AccessControlFilter
isAccessAllowed
method.onAccessDenied
in class AccessControlFilter
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.protected boolean isLoginSubmission(ServletRequest request, ServletResponse response)
true
if the request is an HTTP POST
,
false
otherwise. Can be overridden by subclasses for custom login submission detection behavior.request
- the incoming ServletRequestresponse
- the outgoing ServletResponse.true
if the request is an HTTP POST
, false
otherwise.protected AuthenticationToken createToken(ServletRequest request, ServletResponse response)
createToken
in class AuthenticatingFilter
protected boolean isRememberMe(ServletRequest request)
AuthenticatingFilter
true
if "rememberMe" should be enabled for the login attempt associated with the
current request
, false
otherwise.
This implementation always returns false
and is provided as a template hook to subclasses that
support rememberMe
logins and wish to determine rememberMe
in a custom mannner
based on the current request
.isRememberMe
in class AuthenticatingFilter
request
- the incoming ServletRequesttrue
if "rememberMe" should be enabled for the login attempt associated with the
current request
, false
otherwise.protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception
onLoginSuccess
in class AuthenticatingFilter
Exception
protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response)
onLoginFailure
in class AuthenticatingFilter
protected void setFailureAttribute(ServletRequest request, AuthenticationException ae)
protected String getUsername(ServletRequest request)
protected String getPassword(ServletRequest request)
Copyright © 2004–2023 The Apache Software Foundation. All rights reserved.