Class VaadinWebSecurity
- java.lang.Object
-
- com.vaadin.flow.spring.security.VaadinWebSecurity
-
public abstract class VaadinWebSecurity extends Object
Provides basic Vaadin component-based security configuration for the project.Sets up security rules for a Vaadin application and restricts all URLs except for public resources and internal Vaadin URLs to authenticated user.
The default behavior can be altered by extending the public/protected methods in the class.
Provides default bean implementations for
SecurityFilterChainandWebSecurityCustomizer.To use this, create your own web security class by extending this class and annotate it with
@EnableWebSecurityand@Configuration.For example:
@EnableWebSecurity @Configuration public class MyWebSecurity extends VaadinWebSecurity { }
-
-
Constructor Summary
Constructors Constructor Description VaadinWebSecurity()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddLogoutHandlers(Consumer<org.springframework.security.web.authentication.logout.LogoutHandler> registry)Sets additionalLogoutHandlers that will participate in logout process.protected StringapplyUrlMapping(String path)Helper method to prepend configured servlet path to the given path.protected voidconfigure(org.springframework.security.config.annotation.web.builders.HttpSecurity http)Applies Vaadin default configuration toHttpSecurity.protected voidconfigure(org.springframework.security.config.annotation.web.builders.WebSecurity web)org.springframework.security.web.SecurityFilterChainfilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http)Registers defaultSecurityFilterChainbean.AuthenticationContextgetAuthenticationContext()Gets the default authentication-context bean.static org.springframework.security.web.util.matcher.RequestMatchergetDefaultHttpSecurityPermitMatcher()Matcher for framework internal requests.static org.springframework.security.web.util.matcher.RequestMatchergetDefaultHttpSecurityPermitMatcher(String urlMapping)Matcher for framework internal requests, with Vaadin servlet mapped on the given path.static org.springframework.security.web.util.matcher.RequestMatchergetDefaultWebSecurityIgnoreMatcher()Matcher for Vaadin static (public) resources.static org.springframework.security.web.util.matcher.RequestMatchergetDefaultWebSecurityIgnoreMatcher(String urlMapping)Matcher for Vaadin static (public) resources, with Vaadin servlet mapped on the given path.protected ViewAccessCheckergetViewAccessChecker()Vaadin views access checker bean.protected voidsetLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, Class<? extends Component> flowLoginView)Sets up login for the application using the given Flow login view.protected voidsetLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, Class<? extends Component> flowLoginView, String logoutSuccessUrl)Sets up login for the application using the given Flow login view.protected voidsetLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String hillaLoginViewPath)Sets up login for the application using form login with the given path for the login view.protected voidsetLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String hillaLoginViewPath, String logoutSuccessUrl)Sets up login for the application using form login with the given path for the login view.protected voidsetOAuth2LoginPage(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String oauth2LoginPage)Sets up the login page URI of the OAuth2 provider on the specified HttpSecurity instance.protected voidsetStatelessAuthentication(org.springframework.security.config.annotation.web.builders.HttpSecurity http, SecretKey secretKey, String issuer)Sets up stateless JWT authentication using cookies.protected voidsetStatelessAuthentication(org.springframework.security.config.annotation.web.builders.HttpSecurity http, SecretKey secretKey, String issuer, long expiresIn)Sets up stateless JWT authentication using cookies.org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizerwebSecurityCustomizer()Registers defaultWebSecurityCustomizerbean.
-
-
-
Method Detail
-
filterChain
@Bean(name="VaadinSecurityFilterChainBean") public org.springframework.security.web.SecurityFilterChain filterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http) throws ExceptionRegisters defaultSecurityFilterChainbean.Defines a filter chain which is capable of being matched against an
HttpServletRequest. in order to decide whether it applies to that request.HttpSecurityconfiguration can be customized by overridingconfigure(HttpSecurity).- Throws:
Exception
-
getAuthenticationContext
@Bean(name="VaadinAuthenticationContext") public AuthenticationContext getAuthenticationContext()
Gets the default authentication-context bean.- Returns:
- the authentication-context bean
-
configure
protected void configure(org.springframework.security.config.annotation.web.builders.HttpSecurity http) throws ExceptionApplies Vaadin default configuration toHttpSecurity. Typically, subclasses should call super to apply default Vaadin configuration in addition to custom rules.- Parameters:
http- theHttpSecurityto modify- Throws:
Exception- if an error occurs
-
webSecurityCustomizer
@Bean(name="VaadinWebSecurityCustomizerBean") public org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer webSecurityCustomizer()
Registers defaultWebSecurityCustomizerbean.Beans of this type will automatically be used by
WebSecurityConfigurationto customizeWebSecurity.WebSecurityconfiguration can be customized by overridingconfigure(WebSecurity)Default no
WebSecuritycustomization is performed.
-
configure
protected void configure(org.springframework.security.config.annotation.web.builders.WebSecurity web) throws Exception- Throws:
Exception
-
getDefaultHttpSecurityPermitMatcher
public static org.springframework.security.web.util.matcher.RequestMatcher getDefaultHttpSecurityPermitMatcher()
Matcher for framework internal requests. Assumes Vaadin servlet to be mapped on root path (/*).- Returns:
- default
HttpSecuritybypass matcher
-
getDefaultHttpSecurityPermitMatcher
public static org.springframework.security.web.util.matcher.RequestMatcher getDefaultHttpSecurityPermitMatcher(String urlMapping)
Matcher for framework internal requests, with Vaadin servlet mapped on the given path.- Parameters:
urlMapping- url mapping for the Vaadin servlet.- Returns:
- default
HttpSecuritybypass matcher
-
getDefaultWebSecurityIgnoreMatcher
public static org.springframework.security.web.util.matcher.RequestMatcher getDefaultWebSecurityIgnoreMatcher()
Matcher for Vaadin static (public) resources. Assumes Vaadin servlet to be mapped on root path (/*).- Returns:
- default
WebSecurityignore matcher
-
getDefaultWebSecurityIgnoreMatcher
public static org.springframework.security.web.util.matcher.RequestMatcher getDefaultWebSecurityIgnoreMatcher(String urlMapping)
Matcher for Vaadin static (public) resources, with Vaadin servlet mapped on the given path. Assumes Vaadin servlet to be mapped on root path (/*).- Parameters:
urlMapping- the url mapping for the Vaadin servlet- Returns:
- default
WebSecurityignore matcher
-
setLoginView
protected void setLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String hillaLoginViewPath) throws ExceptionSets up login for the application using form login with the given path for the login view.This is used when your application uses a Hilla based login view available at the given path. NOTE: if the login path points to a Flow view, the corresponding java class must be annotated with @
AnonymousAllowedto ensure that the view is always accessible.- Parameters:
http- the http security fromfilterChain(HttpSecurity)hillaLoginViewPath- the path to the login view- Throws:
Exception- if something goes wrong
-
setLoginView
protected void setLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String hillaLoginViewPath, String logoutSuccessUrl) throws ExceptionSets up login for the application using form login with the given path for the login view.This is used when your application uses a Hilla based login view available at the given path. NOTE: if the login path points to a Flow view, the corresponding java class must be annotated with @
AnonymousAllowedto ensure that the view is always accessible.- Parameters:
http- the http security fromfilterChain(HttpSecurity)hillaLoginViewPath- the path to the login viewlogoutSuccessUrl- the URL to redirect the user to after logging out- Throws:
Exception- if something goes wrong
-
setLoginView
protected void setLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, Class<? extends Component> flowLoginView) throws ExceptionSets up login for the application using the given Flow login view.- Parameters:
http- the http security fromfilterChain(HttpSecurity)flowLoginView- the login view to use- Throws:
Exception- if something goes wrong
-
setLoginView
protected void setLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, Class<? extends Component> flowLoginView, String logoutSuccessUrl) throws ExceptionSets up login for the application using the given Flow login view.- Parameters:
http- the http security fromfilterChain(HttpSecurity)flowLoginView- the login view to uselogoutSuccessUrl- the URL to redirect the user to after logging out- Throws:
Exception- if something goes wrong
-
setOAuth2LoginPage
protected void setOAuth2LoginPage(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String oauth2LoginPage) throws ExceptionSets up the login page URI of the OAuth2 provider on the specified HttpSecurity instance.- Parameters:
http- the http security fromfilterChain(HttpSecurity)oauth2LoginPage- the login page of the OAuth2 provider. This Specifies the URL to send users to if login is required.- Throws:
Exception- Re-throws the possible exceptions while activating OAuth2LoginConfigurer
-
setStatelessAuthentication
protected void setStatelessAuthentication(org.springframework.security.config.annotation.web.builders.HttpSecurity http, SecretKey secretKey, String issuer) throws ExceptionSets up stateless JWT authentication using cookies.- Parameters:
http- the http security fromfilterChain(HttpSecurity)secretKey- the secret key for encoding and decoding JWTs, must use aMacAlgorithmalgorithm nameissuer- the issuer JWT claim- Throws:
Exception- if something goes wrong
-
setStatelessAuthentication
protected void setStatelessAuthentication(org.springframework.security.config.annotation.web.builders.HttpSecurity http, SecretKey secretKey, String issuer, long expiresIn) throws ExceptionSets up stateless JWT authentication using cookies.- Parameters:
http- the http security fromfilterChain(HttpSecurity)secretKey- the secret key for encoding and decoding JWTs, must use aMacAlgorithmalgorithm nameissuer- the issuer JWT claimexpiresIn- lifetime of the JWT and cookies, in seconds- Throws:
Exception- if something goes wrong
-
applyUrlMapping
protected String applyUrlMapping(String path)
Helper method to prepend configured servlet path to the given path. Path will always be considered as relative to servlet path, even if it starts with a slash character.- Parameters:
path- path to be prefixed with servlet path- Returns:
- the input path prepended by servlet path.
-
getViewAccessChecker
protected ViewAccessChecker getViewAccessChecker()
Vaadin views access checker bean.This getter can be used in implementing class to override logic of
VaadinWebSecurity.setLoginViewmethods and callViewAccessCheckermethods explicitly.Note that this bean is a field-autowired, thus this getter returns
nullwhen called from the constructor of implementing class.- Returns:
ViewAccessCheckerbean used by this VaadinWebSecurity configuration.
-
addLogoutHandlers
protected void addLogoutHandlers(Consumer<org.springframework.security.web.authentication.logout.LogoutHandler> registry)
Sets additionalLogoutHandlers that will participate in logout process.- Parameters:
registry- used to add custom handlers.
-
-