Package org.tynamo.security.shiro.authc
Class BasicHttpAuthenticationFilter
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.tynamo.security.shiro.AccessControlFilter
org.tynamo.security.shiro.authc.AuthenticationFilter
org.tynamo.security.shiro.authc.AuthenticatingFilter
org.tynamo.security.shiro.authc.BasicHttpAuthenticationFilter
- All Implemented Interfaces:
jakarta.servlet.Filter,org.apache.shiro.lang.util.Nameable
Requires the requesting user to be
authenticated for the
request to continue, and if they're not, requires the user to login via the HTTP Basic protocol-specific challenge.
Upon successful login, they're allowed to continue on to the requested resource/url.
This implementation is a 'clean room' Java implementation of Basic HTTP Authentication specification per
RFC 2617.
Basic authentication functions as follows:
- A request comes in for a resource that requires authentication.
- The server replies with a 401 response status, sets the
WWW-Authenticateheader, and the contents of a page informing the user that the incoming resource requires authentication. - Upon receiving this
WWW-Authenticatechallenge from the server, the client then takes a username and a password and puts them in the following format:username:password - This token is then base 64 encoded.
- The client then sends another request for the same resource with the following header:
Authorization: Basic Base64_encoded_username_and_password
onAccessDenied(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse) method will
only be called if the subject making the request is not
authenticated- Since:
- 0.4.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final StringHTTP Authentication header, equal toWWW-Authenticateprotected static final StringHTTP Authorization header, equal toAuthorizationFields inherited from class org.tynamo.security.shiro.AccessControlFilter
GET_METHOD, LOGIN_URL, pathMatcher, POST_METHOD, REDIRECT_TO_SAVED_URL, SUCCESS_URL, TAPESTRY_VERSION, UNAUTHORIZED_URLFields inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
ALREADY_FILTERED_SUFFIXFields inherited from class org.apache.shiro.web.servlet.AbstractFilter
filterConfig -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected org.apache.shiro.authc.AuthenticationTokencreateToken(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) Creates an AuthenticationToken for use during login attempt with the provided credentials in the http header.Returns the name to use in the ServletResponse'sWWW-Authenticateheader.Returns the HTTPWWW-Authenticateheader scheme that this filter will use when sending the HTTP Basic challenge response.protected StringgetAuthzHeader(jakarta.servlet.ServletRequest request) Returns theAUTHORIZATION_HEADERfrom the specified ServletRequest.Returns the HTTPAuthorizationheader value that this filter will respond to as indicating a login request.protected String[]getPrincipalsAndCredentials(String authorizationHeader, jakarta.servlet.ServletRequest request) Returns the username obtained from theauthorizationHeader.protected String[]getPrincipalsAndCredentials(String scheme, String encoded) Returns the username and password pair based on the specifiedencodedString obtained from the request's authorization header.protected booleanisLoginAttempt(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) Determines whether the incoming request is an attempt to log in.protected booleanisLoginAttempt(String authzHeader) Default implementation that returnstrueif the specifiedauthzHeaderstarts with the same (case-insensitive) characters specified by theauthzScheme,falseotherwise.protected booleanonAccessDenied(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) Processes unauthenticated requests.protected booleansendChallenge(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) Builds the challenge for authorization by setting a HTTP401(Unauthorized) status as well as the response'sAUTHENTICATE_HEADER.voidsetApplicationName(String applicationName) Sets the name to use in the ServletResponse'sWWW-Authenticateheader.voidsetAuthcScheme(String authcScheme) Sets the HTTPWWW-Authenticateheader scheme that this filter will use when sending the HTTP Basic challenge response.voidsetAuthzScheme(String authzScheme) Sets the HTTPAuthorizationheader value that this filter will respond to as indicating a login request.Methods inherited from class org.tynamo.security.shiro.authc.AuthenticatingFilter
createToken, createToken, executeLogin, getHost, isRememberMe, onLoginFailure, onLoginSuccessMethods inherited from class org.tynamo.security.shiro.authc.AuthenticationFilter
isAccessAllowed, issueSuccessRedirectMethods inherited from class org.tynamo.security.shiro.AccessControlFilter
addConfig, getLoginContextService, getLoginUrl, getSubject, getSuccessUrl, getUnauthorizedUrl, isLoginRequest, isRedirectToSavedUrl, onAccessDenied, onPreHandle, preHandle, redirectToLogin, saveRequest, saveRequestAndRedirectToLogin, setConfig, setLoginUrl, setRedirectToSavedUrl, setSuccessUrl, setUnauthorizedUrlMethods inherited from class org.apache.shiro.web.servlet.AdviceFilter
afterCompletion, cleanup, doFilterInternal, executeChain, postHandleMethods inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
doFilter, getAlreadyFilteredAttributeName, isEnabled, isEnabled, isFilterOncePerRequest, setEnabled, setFilterOncePerRequest, shouldNotFilterMethods inherited from class org.apache.shiro.web.servlet.NameableFilter
getName, setName, toStringBuilderMethods inherited from class org.apache.shiro.web.servlet.AbstractFilter
destroy, getFilterConfig, getInitParam, init, onFilterConfigSet, setFilterConfigMethods inherited from class org.apache.shiro.web.servlet.ServletContextSupport
getContextAttribute, getContextInitParam, getServletContext, removeContextAttribute, setContextAttribute, setServletContext, toString
-
Field Details
-
AUTHORIZATION_HEADER
HTTP Authorization header, equal toAuthorization- See Also:
-
AUTHENTICATE_HEADER
HTTP Authentication header, equal toWWW-Authenticate- See Also:
-
-
Constructor Details
-
BasicHttpAuthenticationFilter
-
-
Method Details
-
getApplicationName
Returns the name to use in the ServletResponse'sWWW-Authenticateheader. Per RFC 2617, this name name is displayed to the end user when they are asked to authenticate. Unless overridden by thesetApplicationName(String)method, the default value is 'application'. Please seesetApplicationName(String)for an example of how this functions.- Returns:
- the name to use in the ServletResponse's 'WWW-Authenticate' header.
-
setApplicationName
Sets the name to use in the ServletResponse'sWWW-Authenticateheader. Per RFC 2617, this name name is displayed to the end user when they are asked to authenticate. Unless overridden by this method, the default value is "application" For example, setting this property to the valueAwesome Webappwill result in the following header:WWW-Authenticate: Basic realm="Awesome Webapp"Side note: As you can see from the header text, the HTTP Basic specification calls this the authentication 'realm', but we call this the 'applicationName' instead to avoid confusion with Shiro's Realm constructs.- Parameters:
applicationName- the name to use in the ServletResponse's 'WWW-Authenticate' header.
-
getAuthzScheme
Returns the HTTPAuthorizationheader value that this filter will respond to as indicating a login request. Unless overridden by thesetAuthzScheme(String)method, the default value isBASIC.- Returns:
- the Http 'Authorization' header value that this filter will respond to as indicating a login request
-
setAuthzScheme
Sets the HTTPAuthorizationheader value that this filter will respond to as indicating a login request. Unless overridden by this method, the default value isBASIC- Parameters:
authzScheme- the HTTPAuthorizationheader value that this filter will respond to as indicating a login request.
-
getAuthcScheme
Returns the HTTPWWW-Authenticateheader scheme that this filter will use when sending the HTTP Basic challenge response. The default value isBASIC.- Returns:
- the HTTP
WWW-Authenticateheader scheme that this filter will use when sending the HTTP Basic challenge response. - See Also:
-
setAuthcScheme
Sets the HTTPWWW-Authenticateheader scheme that this filter will use when sending the HTTP Basic challenge response. The default value isBASIC.- Parameters:
authcScheme- the HTTPWWW-Authenticateheader scheme that this filter will use when sending the Http Basic challenge response.- See Also:
-
onAccessDenied
protected boolean onAccessDenied(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) throws Exception Processes unauthenticated requests. It handles the two-stage request/challenge authentication protocol.- Specified by:
onAccessDeniedin classAccessControlFilter- Parameters:
request- incoming ServletRequestresponse- outgoing ServletResponse- Returns:
- true if the request should be processed; false if the request should not continue to be processed
- Throws:
Exception- if there is an error processing the request.
-
isLoginAttempt
protected boolean isLoginAttempt(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) Determines whether the incoming request is an attempt to log in. The default implementation obtains the value of the request'sAUTHORIZATION_HEADER, and if it is notnull, delegates toisLoginAttempt(authzHeaderValue). If the header isnull,falseis returned.- Parameters:
request- incoming ServletRequestresponse- outgoing ServletResponse- Returns:
- true if the incoming request is an attempt to log in based, false otherwise
-
getAuthzHeader
Returns theAUTHORIZATION_HEADERfrom the specified ServletRequest. This implementation merely casts the request to anHttpServletRequestand returns the header:HttpServletRequest httpRequest =toHttp(reaquest);
return httpRequest.getHeader(AUTHORIZATION_HEADER);- Parameters:
request- the incomingServletRequest- Returns:
- the
Authorizationheader's value.
-
isLoginAttempt
Default implementation that returnstrueif the specifiedauthzHeaderstarts with the same (case-insensitive) characters specified by theauthzScheme,falseotherwise. That is:String authzScheme = getAuthzScheme().toLowerCase();
return authzHeader.toLowerCase().startsWith(authzScheme);- Parameters:
authzHeader- the 'Authorization' header value (guaranteed to be non-null if theisLoginAttempt(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)method is not overriden).- Returns:
trueif the authzHeader value matches that configured as defined by theauthzScheme.
-
sendChallenge
protected boolean sendChallenge(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) Builds the challenge for authorization by setting a HTTP401(Unauthorized) status as well as the response'sAUTHENTICATE_HEADER. The header value constructed is equal to:getAuthcScheme()+ " realm=\"" +getApplicationName()+ "\"";- Parameters:
request- incoming ServletRequest, ignored by this implementationresponse- outgoing ServletResponse- Returns:
- false - this sends the challenge to be sent back
-
createToken
protected org.apache.shiro.authc.AuthenticationToken createToken(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) Creates an AuthenticationToken for use during login attempt with the provided credentials in the http header. This implementation:- acquires the username and password based on the request's
authorization headervia thegetPrincipalsAndCredentialsmethod - The return value of that method is converted to an
AuthenticationTokenvia thecreateTokenmethod - The created
AuthenticationTokenis returned.
- Specified by:
createTokenin classAuthenticatingFilter- Parameters:
request- incoming ServletRequestresponse- outgoing ServletResponse- Returns:
- the AuthenticationToken used to execute the login attempt
- acquires the username and password based on the request's
-
getPrincipalsAndCredentials
protected String[] getPrincipalsAndCredentials(String authorizationHeader, jakarta.servlet.ServletRequest request) Returns the username obtained from theauthorizationHeader. Once theauthzHeaderis split per the RFC (based on the space character ' '), the resulting split tokens are translated into the username/password pair by thegetPrincipalsAndCredentials(scheme,encoded)method.- Parameters:
authorizationHeader- the authorization header obtained from the request.request- the incoming ServletRequest- Returns:
- the username (index 0)/password pair (index 1) submitted by the user for the given header value and request.
- See Also:
-
getPrincipalsAndCredentials
Returns the username and password pair based on the specifiedencodedString obtained from the request's authorization header. Per RFC 2617, the default implementation first Base64 decodes the string and then splits the resulting decoded string into two based on the ":" character. That is:String decoded = Base64.decodeToString(encoded);
return decoded.split(":");- Parameters:
scheme- theauthcSchemefound in the requestauthzHeader. It is ignored by this implementation, but available to overriding implementations should they find it useful.encoded- the Base64-encoded username:password value found after the scheme in the header- Returns:
- the username (index 0)/password (index 1) pair obtained from the encoded header data.
-