In my Vaadin 24.5 application with keycloak as oauth2 provider, most views should be visible without restriction, and only one view should be visible only to logged-in users of a certain role.
@Route("foo")
@AnonymousAllowed
public class FooView extends VerticalLayout
@Route("bar")
@RolesAllowed("ADMIN")
public class BarView extends VerticalLayout
Since the HTML link to BarView is not in a menu or similar where an AccessChecker checks, I could use the annotation @RolesAllowed. However, since Vaadin itself has no mechanism of its own for displaying the login page / keyclock only when required, I want to take the spring way via the requestMatcher.
However, the documentation recommends:
Vaadin strongly recommends not to mix Springâs URL-pattern-based HTTP security and this view-based access control mechanism targeting the same views.
So I removed all the view access annotations and only use Springâs own security mechanisms:
http.authorizeHttpRequests(auth -> auth
.requestMatchers(antMatchers("/shoppingcart")).hasRole("USER")
.requestMatchers(antMatchers("/*")).anonymous())
The application starts, the default route (â/â) is displayed without login. But when I click on the link that points to â/shoppingcartâ, I get the error page:
Access to 'shoppingcart' is denied by security rules.
here the logs
Checking access for view ShoppingCartView
00:25:49.679 | DEBUG | c.v.f.s.auth.RoutePathAccessChecker | Access to view 'ShoppingCartView' with path 'shoppingcart' is denied
00:25:49.679 | DEBUG | f.s.a.DefaultAccessCheckDecisionResolver | Access to view 'ShoppingCartView' with path 'shoppingcart' denied by 1 out of 1 navigation checkers (0 neutral).
00:25:49.679 | DEBUG | c.v.f.s.auth.NavigationAccessControl | Decision against 1 checker results: Access decision: DENY. Access to 'shoppingcart' is denied by security rules.
Declaring an ExceptionHandling does not help either, although I am not sure if this is correct
.exceptionHandling(c -> c.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/oauth2/authorization/keycloak")))
A SavedRequestAwareCache or something like that must surely be added here so that the originally requested route becomes available again after the login and can be called again.
What do I have to do to make some views in my Vaadin application accessible anonymously without login, and only show the keycloak login for some others when starting the corresponding route?
Kind regards
Dominik