Class SpringAccessPathChecker

java.lang.Object
com.vaadin.flow.spring.security.SpringAccessPathChecker
All Implemented Interfaces:
AccessPathChecker, Serializable, org.springframework.beans.factory.Aware, org.springframework.web.context.ServletContextAware

public class SpringAccessPathChecker extends Object implements AccessPathChecker, org.springframework.web.context.ServletContextAware
A Spring specific route path access checker that delegates the check to Spring Security.

It is used in combination with RoutePathAccessChecker to provide path-based security to Flow NavigationAccessControl.

To enable it, define a NavigationAccessControlConfigurer bean, configured using NavigationAccessControlConfigurer.withRoutePathAccessChecker() method.

The current authentication is read from a SecurityContextHolderStrategy. When the principal passed to hasAccess(String, Principal, Predicate) is null, the check is done for an anonymous user, with the anonymous authentication configured for the security filter chain that would handle a request to the path. When the filter chain proxy is not given, or no chain matches the path, an anonymous authentication from the strategy is kept and any other authentication is replaced by a default ROLE_ANONYMOUS one.

The filter chain proxy is read on each check and the anonymous authentication of a chain is computed on the first check it handles. The chains are matched against a request bound to the ServletContext set with setServletContext(ServletContext), which Spring calls when the checker is a bean of a web application context. The proxy and the servlet context are not serialized with the checker: after deserialization the default anonymous authentication is used.

 
 @Bean
 NavigationAccessControlConfigurer navigationAccessControlConfigurer() {
     return new NavigationAccessControlConfigurer()
             .withRoutePathAccessChecker().withLoginView(LoginView.class);
 }
 
 
Since:
24.3
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    SpringAccessPathChecker(org.springframework.security.web.access.WebInvocationPrivilegeEvaluator evaluator)
    Creates a new instance that uses the given WebInvocationPrivilegeEvaluator to check path permissions.
    SpringAccessPathChecker(org.springframework.security.web.access.WebInvocationPrivilegeEvaluator evaluator, String urlMapping)
    Creates a new instance that uses the given WebInvocationPrivilegeEvaluator to check path permissions.
    SpringAccessPathChecker(org.springframework.security.web.access.WebInvocationPrivilegeEvaluator evaluator, String urlMapping, org.springframework.security.core.context.SecurityContextHolderStrategy securityContextHolderStrategy)
    Creates a new instance that uses the given WebInvocationPrivilegeEvaluator to check path permissions.
    SpringAccessPathChecker(org.springframework.security.web.access.WebInvocationPrivilegeEvaluator evaluator, String urlMapping, org.springframework.security.core.context.SecurityContextHolderStrategy securityContextHolderStrategy, SerializableSupplier<org.springframework.security.web.FilterChainProxy> filterChainProxy, org.springframework.security.web.access.AuthorizationManagerWebInvocationPrivilegeEvaluator.HttpServletRequestTransformer requestTransformer)
    Creates a new instance that uses the given WebInvocationPrivilegeEvaluator to check path permissions.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    hasAccess(String path, Principal principal, Predicate<String> roleChecker)
    Checks if the user defined by the given Principal and role checker has access to the given path.
    void
    setServletContext(jakarta.servlet.ServletContext servletContext)
    Sets the servlet context of the request built for the path, as the privilege evaluator does.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SpringAccessPathChecker

      public SpringAccessPathChecker(org.springframework.security.web.access.WebInvocationPrivilegeEvaluator evaluator)
      Creates a new instance that uses the given WebInvocationPrivilegeEvaluator to check path permissions.

      The current authentication is read from the strategy installed in SecurityContextHolder.

      Parameters:
      evaluator - evaluator to check path permissions.
    • SpringAccessPathChecker

      public SpringAccessPathChecker(org.springframework.security.web.access.WebInvocationPrivilegeEvaluator evaluator, String urlMapping)
      Creates a new instance that uses the given WebInvocationPrivilegeEvaluator to check path permissions. It applies the given Vaadin servlet url mapping to the input path before delegating the check to the evaluator. The current authentication is read from the strategy installed in SecurityContextHolder.
      Parameters:
      evaluator - evaluator to check path permissions.
      urlMapping - Vaadin servlet url mapping
    • SpringAccessPathChecker

      public SpringAccessPathChecker(org.springframework.security.web.access.WebInvocationPrivilegeEvaluator evaluator, String urlMapping, org.springframework.security.core.context.SecurityContextHolderStrategy securityContextHolderStrategy)
      Creates a new instance that uses the given WebInvocationPrivilegeEvaluator to check path permissions. It applies the given Vaadin servlet url mapping to the input path before delegating the check to the evaluator. The current authentication is read from the given SecurityContextHolderStrategy instead of the one installed in SecurityContextHolder.
      Parameters:
      evaluator - evaluator to check path permissions.
      urlMapping - Vaadin servlet url mapping
      securityContextHolderStrategy - the strategy to read the current authentication from, not null
      Since:
      25.4
    • SpringAccessPathChecker

      public SpringAccessPathChecker(org.springframework.security.web.access.WebInvocationPrivilegeEvaluator evaluator, String urlMapping, org.springframework.security.core.context.SecurityContextHolderStrategy securityContextHolderStrategy, SerializableSupplier<org.springframework.security.web.FilterChainProxy> filterChainProxy, org.springframework.security.web.access.AuthorizationManagerWebInvocationPrivilegeEvaluator.HttpServletRequestTransformer requestTransformer)
      Creates a new instance that uses the given WebInvocationPrivilegeEvaluator to check path permissions. It applies the given Vaadin servlet url mapping to the input path before delegating the check to the evaluator. The current authentication is read from the given SecurityContextHolderStrategy instead of the one installed in SecurityContextHolder. A check with a null principal uses the anonymous authentication configured for the security filter chain that would handle a request to the path, so that anonymous authorities set with http.anonymous(...) are honoured also when a logged-in user is bound to the current thread.

      The filter chain proxy is taken from the supplier on the first check, so the checker can be created before the security filter chains are built. The request used to select the chain is transformed with the given transformer, which should be the one the evaluator uses.

      Parameters:
      evaluator - evaluator to check path permissions.
      urlMapping - Vaadin servlet url mapping
      securityContextHolderStrategy - the strategy to read the current authentication from, not null
      filterChainProxy - supplies the filter chain proxy to read the configured anonymous authentication from, not null
      requestTransformer - transforms the request built for the path before matching the filter chains, not null
      Since:
      25.4
  • Method Details

    • setServletContext

      public void setServletContext(jakarta.servlet.ServletContext servletContext)
      Sets the servlet context of the request built for the path, as the privilege evaluator does. Spring calls this method when the checker is a bean of a web application context. Set it when creating the checker by hand if a filter chain uses a request matcher that reads the servlet context, like Spring Boot's EndpointRequest and PathRequest matchers.
      Specified by:
      setServletContext in interface org.springframework.web.context.ServletContextAware
      Parameters:
      servletContext - the servlet context
      Since:
      25.4
    • hasAccess

      public boolean hasAccess(String path, Principal principal, Predicate<String> roleChecker)
      Description copied from interface: AccessPathChecker
      Checks if the user defined by the given Principal and role checker has access to the given path.

      The path is relative to the Vaadin application and does not contain any container specific details such as context or servlet path.

      The path is never null and never starts with a "/" character.

      Specified by:
      hasAccess in interface AccessPathChecker
      Parameters:
      path - the path to check access to
      principal - the principal of the user, or null if the check must be done for an anonymous user. A null value must be treated as anonymous even if an authenticated user is bound to the current thread, e.g. when checking whether a path is public
      roleChecker - a function that can answer if a user has a given role
      Returns:
      true if the user has access to the given path, false otherwise.