Class HasRoleFilter
- All Implemented Interfaces:
Filter,FilterConfig,Serializable
When the user is not authorized, the default implementation will return a 403 Forbidden error when the user is authenticated but is
missing the required role, and defers to the parent
behavior otherwise.
This filter should be installed after the UserFilter as it relies on HttpServletRequest.isUserInRole(java.lang.String).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringName of the init parameter used to configure the expected user role.Fields inherited from class net.ltgt.oidc.servlet.AbstractAuthorizationFilter
IS_PRIVATE_REQUEST_ATTRIBUTE_NAME -
Constructor Summary
ConstructorsConstructorDescriptionHasRoleFilter(String role) Constructs a filter that checks for the given role. -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidprotected final voidCallssendUnauthorized(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)from the superclass.voidinit()protected final booleanReturns whether the user is authorized.protected voidThis method is called whenever the user is not authorized and the request is a safe navigation request.protected voidThis method is called whenever the user is authenticated but not authorized.protected voidThis method is called whenever is not authorized and the request is not a safe navigation request.Methods inherited from class net.ltgt.oidc.servlet.AbstractAuthorizationFilter
configureAuthenticationRequest, doFilter, isCallbackServletMethods inherited from class jakarta.servlet.http.HttpFilter
doFilterMethods inherited from class jakarta.servlet.GenericFilter
getFilterConfig, getFilterName, getInitParameter, getInitParameterNames, getServletContext, init
-
Field Details
-
ROLE
Name of the init parameter used to configure the expected user role.- See Also:
-
-
Constructor Details
-
HasRoleFilter
public HasRoleFilter() -
HasRoleFilter
Constructs a filter that checks for the given role.When this constructor is used, the
ROLEinit parameter won't be read.
-
-
Method Details
-
init
- Overrides:
initin classAbstractAuthorizationFilter- Throws:
ServletException
-
isAuthorized
Description copied from class:AbstractAuthorizationFilterReturns whether the user is authorized.Implementations should only use the requests
getUserPrincipal()and/orisUserInRole().- Specified by:
isAuthorizedin classAbstractAuthorizationFilter
-
redirectToAuthenticationEndpoint
@ForOverride protected void redirectToAuthenticationEndpoint(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException This method is called whenever the user is not authorized and the request is a safe navigation request.This implementation calls
sendForbidden(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)whenever the user is authenticated, and defers to the parent behavior otherwise.- Overrides:
redirectToAuthenticationEndpointin classAbstractAuthorizationFilter- Throws:
IOExceptionServletException- See Also:
-
doRedirectToAuthenticationEndpoint
protected final void doRedirectToAuthenticationEndpoint(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException CallsredirectToAuthenticationEndpoint(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)from the superclass. This is a hook allowing to bypass this class' override's implementation.- Throws:
IOExceptionServletException
-
sendUnauthorized
@ForOverride protected void sendUnauthorized(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException This method is called whenever is not authorized and the request is not a safe navigation request.This implementation calls
sendForbidden(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)whenever the user is authenticated, and defers to the parent behavior otherwise.- Overrides:
sendUnauthorizedin classAbstractAuthorizationFilter- Throws:
IOExceptionServletException- See Also:
-
doSendUnauthorized
protected final void doSendUnauthorized(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException CallssendUnauthorized(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)from the superclass. This is a hook allowing to bypass this class' override's implementation.- Throws:
IOExceptionServletException
-
sendForbidden
@ForOverride protected void sendForbidden(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException This method is called whenever the user is authenticated but not authorized.The default implementation simply calls
res.sendError(SC_FORBIDDEN).- Throws:
IOExceptionServletException
-