Class SecurityTokenHandler

java.lang.Object
fr.paris.lutece.portal.service.security.SecurityTokenHandler

@ApplicationScoped public class SecurityTokenHandler extends Object
Handles the Security token related to the SecurityTokenFilter.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    What happens when an MVC @Action is requested with another method than POST (a link, a GET fetch, a HEAD request).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    static final String
     
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    addSecurityToken(String strSource, Object model)
    Adds the hidden field to forms located in the given template data.
    void
    addSessionTokenValue(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.
    void
    handle(jakarta.servlet.http.HttpServletRequest request)
    Activates the token handling on the request.
    void
    handleToken(jakarta.servlet.http.HttpServletRequest request, MvcEvent.ControllerInvocationType invocationType, Method method)
    Handles the generation and storage of the security token.
    boolean
    isMvcActionWithoutPost(jakarta.servlet.http.HttpServletRequest request)
    Checks whether the request calls an MVC @Action with 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).
    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.
    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.
    void
    Observes controller invocations and handles token validation for selected invocation types (ACTION, VIEW, DEFAULT_VIEW).
    void
    registerActions(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).
    void
    registerAdminControllers(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.
    void
    registerMvcController(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.
    void
    Sets the policy applied to the MVC actions requested with another method than POST.
    boolean
    shouldNotFilter(jakarta.servlet.http.HttpServletRequest request)
    Checks if the request should validate the security token.
    boolean
    validate(jakarta.servlet.http.HttpServletRequest request, String strAction)
    Validates the token for the given request.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • SecurityTokenHandler

      public SecurityTokenHandler()
  • Method Details

    • getGetActionPolicy

      public SecurityTokenHandler.GetActionPolicy getGetActionPolicy()
      Gets the policy applied to the MVC actions requested with another method than POST.
      Returns:
      The policy
    • setGetActionPolicy

      public void setGetActionPolicy(SecurityTokenHandler.GetActionPolicy policy)
      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

      public void registerMvcController(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.
      Parameters:
      strName - The controller name: the XPage name in front office, the controller path and JSP in back office
      methods - The methods of the controller
    • isMvcActionWithoutPost

      public boolean isMvcActionWithoutPost(jakarta.servlet.http.HttpServletRequest request)
      Checks whether the request calls an MVC @Action with 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

      public void registerActions(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).
    • resolveTokenAction

      public String 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.
      Parameters:
      request - The request
      strAction - 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 request
      invocationType - The invocation type
      methods - The MVC methods
    • resolveTokenValue

      public String resolveTokenValue(jakarta.servlet.http.HttpServletRequest request)
      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 request
      model - The model
    • validate

      public boolean validate(jakarta.servlet.http.HttpServletRequest request, String strAction)
      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 request
      strAction - The MVC action
      Returns:
      True if the token was valid.
    • addSecurityToken

      public static String addSecurityToken(String strSource, Object model)
      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

      public void onControllerInvocation(@Observes BeforeControllerEvent event)
      Observes controller invocations and handles token validation for selected invocation types (ACTION, VIEW, DEFAULT_VIEW).
      Parameters:
      event - the controller event