com.vaadin.flow.server.auth.

Class RoutePathAccessChecker

java.lang.Object
com.vaadin.flow.server.auth.RoutePathAccessChecker

All Implemented Interfaces:

NavigationAccessChecker, Serializable

public class RoutePathAccessChecker extends Object implements NavigationAccessChecker

Checks if a user has access to a given route path.

The check is performed by a pluggable AccessPathChecker on the actual event location path, without considering if it is the route main path or an alias. Therefore, the provided AccessPathChecker should be configured to handle both route main paths and aliases.

An instance of this class should be provided to a NavigationAccessControl added as a BeforeEnterListener to the UI of interest.

See Also:

  • Constructor Details

    • RoutePathAccessChecker

      public RoutePathAccessChecker(AccessPathChecker accessPathChecker)

      Creates an instance for the given checker.

  • Method Details

    • check

      public AccessCheckResult check(NavigationContext context)

      Description copied from interface: NavigationAccessChecker

      Checks if the current user is allowed to access a target view.

      Details about the navigation target and user are provided by the NavigationContext object.

      The path is relative to the Vaadin application and does not contain container specific details such as context path or servlet path.

      The checker may grant access, deny it, or abstain from taking a decision, by returning an appropriate AccessCheckResult object.

      
       public AccessCheckResult check(NavigationContext context) {
           if (canHandleNavigationRequest(context)) {
               if (hasAccess(context)) {
                   return AccessCheckResult.allow();
               } else {
                   return AccessCheckResult.deny("Access denied");
               }
           }
           return AccessCheckResult.neutral();
       }
       
       
      A special case of deny is rejection; a AccessCheckDecision.REJECT result should be returned if there are misconfiguration in security setup or critical unexpected runtime that prevent the NavigationAccessChecker from taking the access decision.
      
       public AccessCheckResult check(NavigationContext context) {
           try {
               if (hasAccess(context)) {
                   return AccessCheckResult.allow();
               } else {
                   return AccessCheckResult.deny("Access denied");
               }
           } catch (Exception ex) {
               return AccessCheckResult
                       .reject("Cannot determine if access can be granted: "
                               + ex.getMessage());
           }
       }
       
       
      Result object can also be created using NavigationContext helpers NavigationContext.allow(), NavigationContext.deny(String), NavigationContext.reject(String) and NavigationContext.neutral().

      The check is performed for both regular navigation and during error handling rerouting. The current phase can be checked with the NavigationContext.isErrorHandling() flag. The checker implementation can decide to ignore the error handling phase, by returning a NavigationContext.neutral() result.

      Method implementation is not supposed to throw any kind of exception.

      Specified by:

      check in interface NavigationAccessChecker

      Parameters:

      context - the current navigation context

      Returns:

      a result indicating weather the access to target view should be granted or not, never null.