Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion articles/building-apps/security/protect-views.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class CustomAccessChecker implements NavigationAccessChecker {
----
<1> Registers the navigation access checker as a singleton Spring bean.

The [classname]`NavigationContext` object contains information about where you're trying to navigate, such as the view class, route parameters, and query parameters. It also contains the principal of the current user.
The [classname]`NavigationContext` object contains information about where you're trying to navigate, such as the view class, route parameters, and query parameters. It also contains the principal of the current user, except during the <<{articles}/flow/security/access-control/navigation-access-control#public-route-check-phase,public route check>>.

Since the access checker is a Spring bean, you inject other beans into it. For example, you may want to lookup more information to make the access decision.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,46 @@ public AccessCheckResult check(NavigationContext context) {
----


[role="since:com.vaadin:vaadin@V25.4"]
=== Public Route Check Phase

A checker is called for different reasons. [methodname]`NavigationContext.getPhase()` tells which one, as a value of the [classname]`NavigationContext.Phase` enum:

[cols="1,2"]
|===
| Phase | Reason for the Check

| `NAVIGATION`
| The current user navigates to the target view.

| `EVALUATION`
| The access of the current user to the target view is checked outside of a navigation, for example to populate the menu.

| `PUBLIC_ROUTE_CHECK`
| The target view is checked for anonymous access, to find out if its route is public.
|===

The check during error handling, described in the earlier section, has the `NAVIGATION` phase. Use [methodname]`isErrorHandling()` to tell it apart from a regular navigation.

In a Spring application, the request matcher for public routes runs the public route check. This matcher is part of [methodname]`defaultPermitMatcher()`, described in <<../spring/security-configuration#request-matchers,Request Matchers>>. Spring Security calls it for a request to a route path before the request reaches Vaadin. The matcher permits the request without authentication only if the final decision is `ALLOW`. Navigation access control still checks the access of the actual user when the view is opened.

In this phase, the context has no user: [methodname]`getPrincipal()` returns `null`, and [methodname]`hasRole()` returns `false` for every role. The request that triggers the check can come from a signed-in user. The result mustn't depend on who sent it. Therefore, a checker shouldn't look up the user anywhere else, such as in the security context of the request. A checker whose decision depends on the user can return a `NEUTRAL` result, and leave it to the other checkers to decide whether the view is public.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 The section doesn't say what happens when a checker denies during the public route check.

The guidance is phrased as an option: "A checker whose decision depends on the user can return a NEUTRAL result". A reader who instead denies on an @AnonymousAllowed view gets a mixed result (AnnotatedViewAccessChecker allows, the custom checker denies), which DefaultAccessCheckDecisionResolver turns into REJECT (DefaultAccessCheckDecisionResolver.java:141). NavigationAccessControl.checkAccess then throws IllegalStateException outside production mode (NavigationAccessControl.java:304-307), and because the call comes from the Spring Security request matcher, the request fails before it reaches Vaadin.

Consider adding one sentence after the NEUTRAL advice:

A DENY from a checker makes the route non-public. If another checker allows it, the mixed result is a REJECT, which throws an exception in development mode.

navigation-access-control.adoc:165 · clarity

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the sentence after the NEUTRAL advice. It says that DENY makes the route non-public. If another checker allows the view, for example AnnotatedViewAccessChecker for an @AnonymousAllowed view, the mixed result is a REJECT, as described in the Decision Resolver section. Outside production mode, this throws an exception and the request fails before it reaches Vaadin. I checked this against DefaultAccessCheckDecisionResolver, NavigationAccessControl.checkAccess() and RequestUtil.isAnonymousRoute(), which passes the real production-mode flag.


Returning `DENY` in this phase makes the route non-public. If another checker allows the view, the results are mixed, and the decision is `REJECT`, as described in <<#decision-resolver,Decision Resolver>>. For example, [classname]`AnnotatedViewAccessChecker` allows a view annotated with [annotationname]`@AnonymousAllowed`. Outside production mode, this throws an exception, and the request fails before it reaches Vaadin.

.Neutral Result on Public Route Check
[source,java]
----
@Override
public AccessCheckResult check(NavigationContext context) {
if (context.getPhase() == NavigationContext.Phase.PUBLIC_ROUTE_CHECK) {
return AccessCheckResult.neutral();
}
....
}
----


=== Custom Decision Resolver

If the default implementation doesn't fit the requirements of the project, you can implement your own [interfacename]`AccessCheckDecisionResolver`. The interface defines a single method that takes as input the results computed by the navigation access checker and the current navigation context. Its output is an [classname]`AccessCheckResult`.
Expand Down
Loading