com.vaadin.flow.server.auth.
Class NavigationAccessControl
All Implemented Interfaces:
Direct Known Subclasses:
A BeforeEnterListener
implementation that contains logic to perform
access checks before entering a view.
Access rules are defined by providing one or more
NavigationAccessChecker
instances, that are responsible for deciding
if a navigation should be allowed or not. The final navigation grant decision
is taken by a pluggable AccessCheckDecisionResolver
component, based
on the results of all access checkers.
If access is allowed, the navigation continues to the target component. Otherwise, for not authenticated requests, a redirect is performed to the login page (if configured) or to the not found error view.
In development mode, the access check failure reason is forwarded to the not found error view, for debugging purpose. In addition, an exception will be thrown if the decision resolver determines the navigation should be rejected because of misconfigurations.
In production mode, for security reasons, the failure message is never exposed and rejection is treated as a normal deny, without any exception being thrown.
Before redirecting to the login page, the route path and its absolute URL are stored in the HTTP session, to allow the authentication logic to access the requested resource (
SESSION_STORED_REDIRECT
,
SESSION_STORED_REDIRECT_ABSOLUTE
).
The default constructor create an instance pre-configured with
AnnotatedViewAccessChecker
.
For internal use only. May be renamed or removed in a future release.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreate a new instance with the default view annotation checker and decision resolver.
protected
NavigationAccessControl
(Collection<NavigationAccessChecker> checkerList) Create a new instance with given checkers and the default decision resolver.
NavigationAccessControl
(Collection<NavigationAccessChecker> checkerList, AccessCheckDecisionResolver decisionResolver) Create a new instance with given checkers and decision resolver.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
beforeEnter
(BeforeEnterEvent event) Callback executed before navigation to attaching Component chain is made.
checkAccess
(NavigationContext context, boolean productionMode) createNavigationContext
(Class<?> navigationTarget, String path, VaadinService vaadinService, VaadinRequest vaadinRequest) Creates a new
NavigationContext
instance based on the given route data and Vaadin service and request.protected Class<? extends RuntimeException>
getAccessDeniedException
(AnnotatedElement securedClass) protected String
Gets the frontend login view.
Gets the Flow login view.
protected Principal
getPrincipal
(VaadinRequest request) Gets the principal for the currently logged in user.
protected String
getRequestURL
(VaadinRequest vaadinRequest) Gets the URL the client used to make the request.
getRolesChecker
(VaadinRequest request) Gets a function for checking roles for the currently logged in user.
boolean
hasAccessChecker
(Class<? extends NavigationAccessChecker> type) Checks if an access checker of the given type is in use.
final boolean
Gets if the access control is enabled or not.
final void
setEnabled
(boolean enabled) Enables or disables the navigation access control.
final void
setLoginView
(Class<? extends Component> loginView) Sets the Flow login view to use.
void
setLoginView
(String loginUrl) Sets the frontend login view to use.
-
Field Details
-
SESSION_STORED_REDIRECT
Attribute name used to store the route path before redirecting to the login view.
-
SESSION_STORED_REDIRECT_ABSOLUTE
Attribute name used to store the route absolute URL before redirecting to the login view.
-
-
Constructor Details
-
NavigationAccessControl
public NavigationAccessControl()Create a new instance with the default view annotation checker and decision resolver.
-
NavigationAccessControl
public NavigationAccessControl(Collection<NavigationAccessChecker> checkerList, AccessCheckDecisionResolver decisionResolver) Create a new instance with given checkers and decision resolver.
Parameters:
checkerList
- collection of navigation access checker.decisionResolver
- the decision resolver. -
NavigationAccessControl
Create a new instance with given checkers and the default decision resolver.
Parameters:
checkerList
- collection of navigation access checker.See Also:
-
-
Method Details
-
setEnabled
public final void setEnabled(boolean enabled) Enables or disables the navigation access control.
By disabling the access control, navigation to any route is allowed.
By default, the access checker is enabled.
Parameters:
enabled
- true to enable access control, false to disable the checks and allow navigation to all routes. -
isEnabled
public final boolean isEnabled()Gets if the access control is enabled or not.
Returns:
true if the access control is enabled, otherwise false.
-
setLoginView
Sets the Flow login view to use.
The login view can only be set once and cannot be changed afterwards.
Parameters:
loginView
- the Flow view to use as login view -
getLoginView
Gets the Flow login view.
Returns:
the Flow login view, or null if not set
-
setLoginView
Sets the frontend login view to use.
The login view can only be set once and cannot be changed afterwards.
Parameters:
loginUrl
- the frontend view to use as login view -
getLoginUrl
Gets the frontend login view.
Returns:
the frontend login view, or null if not set
-
beforeEnter
Description copied from interface:
BeforeEnterHandler
Callback executed before navigation to attaching Component chain is made.
Specified by:
beforeEnter
in interfaceBeforeEnterHandler
Parameters:
event
- before navigation event with event details -
getPrincipal
Gets the principal for the currently logged in user.
Parameters:
request
- the current request ornull
if no request is in progress (e.g. in a background thread)Returns:
a representation of the currently logged in user or
null
if no user is currently logged in -
getRolesChecker
Gets a function for checking roles for the currently logged in user.
Parameters:
request
- the current request ornull
if no request is in progress (e.g. in a background thread)Returns:
a function which takes a role name and returns
true
if the user is included in that role -
getRequestURL
Gets the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters. Returns an empty string if the URL cannot be extracted from the current request.
Parameters:
vaadinRequest
- current requestReturns:
the URL the client used to make the request
-
getAccessDeniedException
-
hasAccessChecker
Checks if an access checker of the given type is in use.
Returns:
true
if a checker is in use,false
otherwise
-