Class SecurityTokenHandler
java.lang.Object
fr.paris.lutece.portal.service.security.SecurityTokenHandler
Handles the Security token related to the SecurityTokenFilter.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumWhat happens when an MVC@Actionis requested with another method than POST (a link, a GET fetch, a HEAD request). -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringaddSecurityToken(String strSource, Object model) Adds the hidden field to forms located in the given template data.voidaddSessionTokenValue(jakarta.servlet.http.HttpServletRequest request, Map<String, Object> model) Adds the session security token for confirm type actions (SiteMessageHandler / AdminMessageJspBean).Gets the policy applied to the MVC actions requested with another method than POST.getGetActionPolicy(jakarta.servlet.http.HttpServletRequest request) Gets the policy applied to the action of the request: allow when the controller and the action are listed in lutece.mvc.action.getPolicy.allowed (entries controller:action, the controller being the XPage name or the back office path such as jsp/admin/plugins/x/ManageX.jsp), the configured policy otherwise.voidhandle(jakarta.servlet.http.HttpServletRequest request) Activates the token handling on the request.voidhandleToken(jakarta.servlet.http.HttpServletRequest request, MvcEvent.ControllerInvocationType invocationType, Method method) Handles the generation and storage of the security token.booleanisMvcActionWithoutPost(jakarta.servlet.http.HttpServletRequest request) Checks whether the request calls an MVC@Actionwith another method than POST: the action parameter names an action of the registered controller of the request, and no view of this controller is requested (a view takes precedence over an action in the dispatch).booleanisRefusedActionWithoutPost(jakarta.servlet.http.HttpServletRequest request) Applies the policy to a request whose dispatch has selected an MVC action: when it is requested with another method than POST, writes the warning of the log and refuse policies.booleanisRefusedMvcActionWithoutPost(jakarta.servlet.http.HttpServletRequest request) Applies the policy to a request reaching the security token filter: when it calls a registered MVC action with another method than POST, writes the warning of the log and refuse policies.voidObserves controller invocations and handles token validation for selected invocation types (ACTION, VIEW, DEFAULT_VIEW).voidregisterActions(String strName, Method[] methods) Registers disabled action methods from XPage or MVCAdminJspBean, and the token key of the actions that share their token with other actions (securityTokenAction).voidregisterAdminControllers(jakarta.servlet.ServletContext context) Registers the actions of every back office MVC controller with the security token enabled, at application startup, so that the token is checked from the first request, before the controller has ever been displayed.voidregisterMvcController(String strName, Method[] methods) Registers the views and the actions of an MVC controller (XPage or MVCAdminJspBean), whether its security token is enabled or not, so that the actions requested with another method than POST are found.resolveTokenAction(jakarta.servlet.http.HttpServletRequest request, String strAction) Resolves the key under which the token of the requested action is stored: the securityTokenAction of the action when it declares one, so that several actions posted from the same form (save a step, summary, save...) accept the token generated for that form, the action name otherwise.resolveTokenValue(jakarta.servlet.http.HttpServletRequest request) Resolves security token value from the request.voidSets the policy applied to the MVC actions requested with another method than POST.booleanshouldNotFilter(jakarta.servlet.http.HttpServletRequest request) Checks if the request should validate the security token.booleanValidates the token for the given request.
-
Field Details
-
MARK_CSRF_TOKEN
- See Also:
-
METHOD_POST
- See Also:
-
HEADER_ALLOW
- See Also:
-
-
Constructor Details
-
SecurityTokenHandler
public SecurityTokenHandler()
-
-
Method Details
-
getGetActionPolicy
Gets the policy applied to the MVC actions requested with another method than POST.- Returns:
- The policy
-
setGetActionPolicy
Sets the policy applied to the MVC actions requested with another method than POST.- Parameters:
policy- The policy
-
registerAdminControllers
public void registerAdminControllers(@Observes @Initialized(jakarta.enterprise.context.ApplicationScoped.class) jakarta.servlet.ServletContext context) Registers the actions of every back office MVC controller with the security token enabled, at application startup, so that the token is checked from the first request, before the controller has ever been displayed.- Parameters:
context- The servlet context
-
registerMvcController
Registers the views and the actions of an MVC controller (XPage or MVCAdminJspBean), whether its security token is enabled or not, so that the actions requested with another method than POST are found.- Parameters:
strName- The controller name: the XPage name in front office, the controller path and JSP in back officemethods- The methods of the controller
-
isMvcActionWithoutPost
public boolean isMvcActionWithoutPost(jakarta.servlet.http.HttpServletRequest request) Checks whether the request calls an MVC@Actionwith another method than POST: the action parameter names an action of the registered controller of the request, and no view of this controller is requested (a view takes precedence over an action in the dispatch). The requests of the controllers that do not use the MVC model (a page reading its own action parameter) are never concerned.- Parameters:
request- The request- Returns:
- true if an MVC action is requested with another method than POST
-
getGetActionPolicy
public SecurityTokenHandler.GetActionPolicy getGetActionPolicy(jakarta.servlet.http.HttpServletRequest request) Gets the policy applied to the action of the request: allow when the controller and the action are listed in lutece.mvc.action.getPolicy.allowed (entries controller:action, the controller being the XPage name or the back office path such as jsp/admin/plugins/x/ManageX.jsp), the configured policy otherwise.- Parameters:
request- The request- Returns:
- The policy
-
isRefusedMvcActionWithoutPost
public boolean isRefusedMvcActionWithoutPost(jakarta.servlet.http.HttpServletRequest request) Applies the policy to a request reaching the security token filter: when it calls a registered MVC action with another method than POST, writes the warning of the log and refuse policies.- Parameters:
request- The request- Returns:
- true if the request must be refused with a 405 Method Not Allowed
-
isRefusedActionWithoutPost
public boolean isRefusedActionWithoutPost(jakarta.servlet.http.HttpServletRequest request) Applies the policy to a request whose dispatch has selected an MVC action: when it is requested with another method than POST, writes the warning of the log and refuse policies. The result is kept in the request, so that the filter and the dispatch write a single warning.- Parameters:
request- The request- Returns:
- true if the request must be refused with a 405 Method Not Allowed
-
registerActions
Registers disabled action methods from XPage or MVCAdminJspBean, and the token key of the actions that share their token with other actions (securityTokenAction). -
resolveTokenAction
Resolves the key under which the token of the requested action is stored: the securityTokenAction of the action when it declares one, so that several actions posted from the same form (save a step, summary, save...) accept the token generated for that form, the action name otherwise.- Parameters:
request- The requeststrAction- The MVC action- Returns:
- The token key
-
handle
public void handle(jakarta.servlet.http.HttpServletRequest request) Activates the token handling on the request.- Parameters:
request- The request
-
shouldNotFilter
public boolean shouldNotFilter(jakarta.servlet.http.HttpServletRequest request) Checks if the request should validate the security token.- Parameters:
request- The request- Returns:
- True if the request should be skipped from the SecurityTokenFilter validation, false otherwise.
-
handleToken
public void handleToken(jakarta.servlet.http.HttpServletRequest request, MvcEvent.ControllerInvocationType invocationType, Method method) Handles the generation and storage of the security token.- Parameters:
request- The requestinvocationType- The invocation typemethods- The MVC methods
-
resolveTokenValue
Resolves security token value from the request.- Parameters:
request- The request- Returns:
- The security token value
-
addSessionTokenValue
public void addSessionTokenValue(jakarta.servlet.http.HttpServletRequest request, Map<String, Object> model) Adds the session security token for confirm type actions (SiteMessageHandler / AdminMessageJspBean).- Parameters:
request- The requestmodel- The model
-
validate
Validates the token for the given request. A form may carry several tokens: the token of the page, added to every form, and the token of the action it posts when this action is not the one of the page (a button of a list posting an action); the action is accepted when one of them is valid for it, and only this one is consumed.- Parameters:
request- The requeststrAction- The MVC action- Returns:
- True if the token was valid.
-
addSecurityToken
Adds the hidden field to forms located in the given template data.- Parameters:
strSource- The template data.model- The model- Returns:
- The updated template data.
-
onControllerInvocation
Observes controller invocations and handles token validation for selected invocation types (ACTION, VIEW, DEFAULT_VIEW).- Parameters:
event- the controller event
-