Class ViewAccessChecker
- java.lang.Object
-
- com.vaadin.flow.server.auth.ViewAccessChecker
-
- All Implemented Interfaces:
BeforeEnterListener,BeforeEnterHandler,Serializable
public class ViewAccessChecker extends Object implements BeforeEnterListener
Checks access to views using anAccessAnnotationChecker.An instance of this class should be added as a
BeforeEnterListenerto theUIof interest.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static StringSESSION_STORED_REDIRECTstatic StringSESSION_STORED_REDIRECT_ABSOLUTE
-
Constructor Summary
Constructors Modifier Constructor Description ViewAccessChecker()Creates an instance.ViewAccessChecker(boolean enabled)Creates an instance and enables access checker depending on the given flag.protectedViewAccessChecker(AccessAnnotationChecker accessAnnotationChecker)Creates an instance using the given checker.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeforeEnter(BeforeEnterEvent beforeEnterEvent)Callback executed before navigation to attaching Component chain is made.voidenable()Enables the access checker.protected PrincipalgetPrincipal(VaadinRequest request)Gets the principal for the currently logged in user.protected Function<String,Boolean>getRolesChecker(VaadinRequest request)Gets a function for checking roles for the currently logged in user.voidsetLoginView(Class<? extends Component> loginView)Sets the Flow login view to use.voidsetLoginView(String loginUrl)Sets the frontend login view to use.
-
-
-
Constructor Detail
-
ViewAccessChecker
public ViewAccessChecker()
Creates an instance.Note that the access checker is enabled by default. If this isn't desired, one can use
ViewAccessChecker(boolean)withenabled=falseand callenable()later on whenever appropriate.
-
ViewAccessChecker
public ViewAccessChecker(boolean enabled)
Creates an instance and enables access checker depending on the given flag.- Parameters:
enabled-falsefor disabling the access checker,truefor enabling the access checker.
-
ViewAccessChecker
protected ViewAccessChecker(AccessAnnotationChecker accessAnnotationChecker)
Creates an instance using the given checker.Note that the access checker is disabled by default and can be enabled using
enable(). You should also set the login view to use usingsetLoginView(Class)orsetLoginView(String)- Parameters:
accessAnnotationChecker- the checker to use
-
-
Method Detail
-
enable
public void enable()
Enables the access checker.This must be called for the access checker to perform any checks. By default the access checker is disabled.
-
setLoginView
public void setLoginView(Class<? extends Component> loginView)
Sets the Flow login view to use.The login view can only be set once and cannot be changed afterwards.
Note that the access checker needs to be separately enabled using
enable()- Parameters:
loginView- the Flow view to use as login view
-
setLoginView
public void setLoginView(String loginUrl)
Sets the frontend login view to use.The login view can only be set once and cannot be changed afterwards.
Note that the access checker needs to be separately enabled using
enable()- Parameters:
loginUrl- the frontend view to use as login view
-
beforeEnter
public void beforeEnter(BeforeEnterEvent beforeEnterEvent)
Description copied from interface:BeforeEnterHandlerCallback executed before navigation to attaching Component chain is made.- Specified by:
beforeEnterin interfaceBeforeEnterHandler- Parameters:
beforeEnterEvent- before navigation event with event details
-
getRolesChecker
protected Function<String,Boolean> getRolesChecker(VaadinRequest request)
Gets a function for checking roles for the currently logged in user.- Parameters:
request- the current request ornullif no request is in progress (e.g. in a background thread)- Returns:
- a function which takes a role name and returns
trueif the user is included in that role
-
getPrincipal
protected Principal getPrincipal(VaadinRequest request)
Gets the principal for the currently logged in user.- Parameters:
request- the current request ornullif no request is in progress (e.g. in a background thread)- Returns:
- a representation of the currently logged in user or
nullif no user is currently logged in
-
-