Class AnnotatedViewAccessChecker

java.lang.Object
com.vaadin.flow.server.auth.AnnotatedViewAccessChecker
All Implemented Interfaces:
NavigationAccessChecker, Serializable

public class AnnotatedViewAccessChecker extends Object implements NavigationAccessChecker
Checks access to views using an AccessAnnotationChecker.

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

See Also:
  • Constructor Details

    • AnnotatedViewAccessChecker

      public AnnotatedViewAccessChecker()
      Creates an instance using the given checker.
    • AnnotatedViewAccessChecker

      public AnnotatedViewAccessChecker(AccessAnnotationChecker accessAnnotationChecker)
      Creates an instance using the given checker.
      Parameters:
      accessAnnotationChecker - the checker to use
  • 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.