Class VaadinWebSecurity
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 SecurityFilterChain and
WebSecurityCustomizer.
To use this, create your own web security class by extending this class and
annotate it with @EnableWebSecurity and
@Configuration.
For example:
@EnableWebSecurity
@Configuration
public class MyWebSecurity extends VaadinWebSecurity {
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddLogoutHandlers(Consumer<org.springframework.security.web.authentication.logout.LogoutHandler> registry) Sets additionalLogoutHandlers that will participate in logout process.org.springframework.security.web.util.matcher.RequestMatcher[]antMatchers(String... patterns) Deprecated, for removal: This API element is subject to removal in a future version.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) protected booleanGets if navigation access control should be enabled.org.springframework.security.web.SecurityFilterChainfilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http) Registers defaultSecurityFilterChainbean.Gets the default authentication-context bean.static org.springframework.security.web.util.matcher.RequestMatcherMatcher 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.RequestMatcherMatcher 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 NavigationAccessControlVaadin navigation access control bean.protected ViewAccessCheckerDeprecated, for removal: This API element is subject to removal in a future version.ViewAccessChecker is not used anymore by VaadinWebSecurity, and has been replaced byNavigationAccessControl.protected org.springframework.security.web.authentication.logout.LogoutSuccessHandleroidcLogoutSuccessHandler(String postLogoutRedirectUri) Gets aOidcClientInitiatedLogoutSuccessHandlerinstance that redirects to the given URL after logout.org.springframework.security.web.util.matcher.RequestMatcher[]pathMatchers(String... patterns) Utility to createRequestMatchers from path patterns.org.springframework.security.web.util.matcher.RequestMatcher[]routeMatchers(String... patterns) Deprecated, for removal: This API element is subject to removal in a future version.AntPathRequestMatcher is deprecated and will be removed, useroutePathMatchers(String...)instead.org.springframework.security.web.util.matcher.RequestMatcher[]routePathMatchers(String... patterns) Utility to createRequestMatchers for a Vaadin routes, using ant patterns and HTTP get method.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 voidsetOAuth2LoginPage(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String oauth2LoginPage, String postLogoutRedirectUri) Sets up the login page URI of the OAuth2 provider and the post logout URI 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.WebSecurityCustomizerRegisters defaultWebSecurityCustomizerbean.
-
Constructor Details
-
VaadinWebSecurity
public VaadinWebSecurity()
-
-
Method Details
-
filterChain
@Bean(name="VaadinSecurityFilterChainBean") public org.springframework.security.web.SecurityFilterChain filterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http) throws Exception Registers 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
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 Exception Applies 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
-
antMatchers
@Deprecated(since="24.8", forRemoval=true) public org.springframework.security.web.util.matcher.RequestMatcher[] antMatchers(String... patterns) Deprecated, for removal: This API element is subject to removal in a future version.AntPathRequestMatcher is deprecated and will be removed, usepathMatchers(String...)instead.Utility to createRequestMatchers from ant patterns.- Parameters:
patterns- ant patterns- Returns:
- an array or
RequestMatcherinstances for the given patterns.
-
routeMatchers
@Deprecated(since="24.8", forRemoval=true) public org.springframework.security.web.util.matcher.RequestMatcher[] routeMatchers(String... patterns) Deprecated, for removal: This API element is subject to removal in a future version.AntPathRequestMatcher is deprecated and will be removed, useroutePathMatchers(String...)instead.Utility to createRequestMatchers for a Vaadin routes, using ant patterns and HTTP get method.- Parameters:
patterns- ant patterns- Returns:
- an array or
RequestMatcherinstances for the given patterns.
-
pathMatchers
public org.springframework.security.web.util.matcher.RequestMatcher[] pathMatchers(String... patterns) Utility to createRequestMatchers from path patterns.- Parameters:
patterns- path patterns, as described inPathPatternjavadoc.- Returns:
- an array or
RequestMatcherinstances for the given patterns. - See Also:
-
PathPatternRequestMatcher.matcher(HttpServletRequest)PathPattern
-
routePathMatchers
public org.springframework.security.web.util.matcher.RequestMatcher[] routePathMatchers(String... patterns) Utility to createRequestMatchers for a Vaadin routes, using ant patterns and HTTP get method.- Parameters:
patterns- path patterns, as described inPathPatternjavadoc.- Returns:
- an array or
RequestMatcherinstances for the given patterns. - See Also:
-
PathPatternRequestMatcher.matcher(HttpServletRequest)PathPattern
-
setLoginView
protected void setLoginView(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String hillaLoginViewPath) throws Exception Sets 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 Exception Sets 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 Exception Sets 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 Exception Sets 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 Exception Sets up the login page URI of the OAuth2 provider on the specified HttpSecurity instance.
This method also configures a logout success handler that redirects to the application base URL after logout.- 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
-
setOAuth2LoginPage
protected void setOAuth2LoginPage(org.springframework.security.config.annotation.web.builders.HttpSecurity http, String oauth2LoginPage, String postLogoutRedirectUri) throws Exception Sets up the login page URI of the OAuth2 provider and the post logout URI on the specified HttpSecurity instance.
The post logout redirect uri can be relative or absolute URI or a template. The supported uri template variables are: {baseScheme}, {baseHost}, {basePort} and {basePath}.
NOTE: "{baseUrl}" is also supported, which is the same as "{baseScheme}://{baseHost}{basePort}{basePath}" handler. setPostLogoutRedirectUri("{baseUrl}");- 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.postLogoutRedirectUri- the post logout redirect uri. Can be a template.- Throws:
Exception- Re-throws the possible exceptions while activating OAuth2LoginConfigurer
-
oidcLogoutSuccessHandler
protected org.springframework.security.web.authentication.logout.LogoutSuccessHandler oidcLogoutSuccessHandler(String postLogoutRedirectUri) Gets aOidcClientInitiatedLogoutSuccessHandlerinstance that redirects to the given URL after logout.
If aClientRegistrationRepositorybean is not registered in the application context, the method returns null.- Parameters:
postLogoutRedirectUri- the post logout redirect uri- Returns:
- a
OidcClientInitiatedLogoutSuccessHandler, or null if aClientRegistrationRepositorybean is not registered in the application context.
-
setStatelessAuthentication
protected void setStatelessAuthentication(org.springframework.security.config.annotation.web.builders.HttpSecurity http, SecretKey secretKey, String issuer) throws Exception Sets 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 Exception Sets 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
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
Deprecated, for removal: This API element is subject to removal in a future version.ViewAccessChecker is not used anymore by VaadinWebSecurity, and has been replaced byNavigationAccessControl. Calling this method will get a stub implementation that delegates to theNavigationAccessControlinstance.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.
-
pathMatchers(String...)instead.