Annotation Interface JSentinelAnnotation


@Retention(RUNTIME) @Target(ANNOTATION_TYPE) public @interface JSentinelAnnotation
This annotation is to be placed on other annotations, marking them as restriction-annotations and assigning evaluator classes to them. Existing Vaadin-oriented annotations may use AccessEvaluator; adapter-neutral annotations may use AuthorizationEvaluator. Take for example the case that a certain route-target is only to be accessed by users that have the role 'administrator'. Now the first step would be to create an annotation called VisibleTo and annotate it with JSentinelAnnotation.
    @Retention(RUNTIME)
    @JSentinelAnnotation(RoleBasedAccessEvaluator.class)
    public @interface VisibleTo {
        UserRoleDescription value();
    }

The RoleBasedAccessEvaluator is an AccessEvaluator that could look something like the following. Note that the generic type for this AccessEvaluator is the type of the annotation and the annotation is the last parameter of evaluate.

class RoleBasedAccessEvaluator implements AccessEvaluator<VisibleTo> {

    Supplier<UserRoleDescription> userRoleProvider;

    @Override
    public AccessDecision evaluate(AccessContext context, VisibleTo annotation) {
        final boolean hasRole = annotation.value().equals(userRoleProvider.get());

        return hasRole ? AccessDecision.granted()
                       : AccessDecision.deniedWithError(UserNotInRoleException.class, null);
    }
}

VisibleTo can then be used to prevent users that don't have the required role to enter the route-target by just annotating the respective class

    @Route("adminview")
    @VisibleTo(UserRole.Admin)
    public class AdminView extends Div {
}
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The evaluator class that is to be assigned to the annotation.
  • Element Details

    • value

      Class<?> value
      The evaluator class that is to be assigned to the annotation.
      Returns:
      the evaluator class