Class ServerHttpSecurity
- java.lang.Object
-
- org.springframework.security.config.web.server.ServerHttpSecurity
-
public class ServerHttpSecurity extends java.lang.ObjectAServerHttpSecurityis similar to Spring Security'sHttpSecuritybut for WebFlux. It allows configuring web based security for specific http requests. By default it will be applied to all requests, but can be restricted usingsecurityMatcher(ServerWebExchangeMatcher)or other similar methods. A minimal configuration can be found below:@EnableWebFluxSecurity public class MyMinimalSecurityConfiguration { @Bean public MapReactiveUserDetailsService userDetailsService() { UserDetails user = User.withDefaultPasswordEncoder() .username("user") .password("password") .roles("USER") .build(); return new MapReactiveUserDetailsService(user); } }Below is the same as our minimal configuration, but explicitly declaring theServerHttpSecurity.@EnableWebFluxSecurity public class MyExplicitSecurityConfiguration { @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http .authorizeExchange() .anyExchange().authenticated() .and() .httpBasic().and() .formLogin(); return http.build(); } @Bean public MapReactiveUserDetailsService userDetailsService() { UserDetails user = User.withDefaultPasswordEncoder() .username("user") .password("password") .roles("USER") .build(); return new MapReactiveUserDetailsService(user); } }- Since:
- 5.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classServerHttpSecurity.AnonymousSpecConfigures anonymous authenticationclassServerHttpSecurity.AuthorizeExchangeSpecConfigures authorizationclassServerHttpSecurity.CorsSpecConfigures CORS support within Spring Security.classServerHttpSecurity.CsrfSpecConfigures CSRF ProtectionclassServerHttpSecurity.ExceptionHandlingSpecConfigures exception handlingclassServerHttpSecurity.FormLoginSpecConfigures Form Based authenticationclassServerHttpSecurity.HeaderSpecConfigures HTTP Response Headers.classServerHttpSecurity.HttpBasicSpecConfigures HTTP Basic AuthenticationclassServerHttpSecurity.HttpsRedirectSpecConfigures HTTPS redirection rulesclassServerHttpSecurity.LogoutSpecConfigures log outclassServerHttpSecurity.OAuth2ClientSpecclassServerHttpSecurity.OAuth2LoginSpecclassServerHttpSecurity.OAuth2ResourceServerSpecConfigures OAuth2 Resource Server SupportclassServerHttpSecurity.PasswordManagementSpecConfigures password management.classServerHttpSecurity.RequestCacheSpecConfigures the request cache which is used when a flow is interrupted (i.e.classServerHttpSecurity.X509SpecConfigures X509 authentication
-
Constructor Summary
Constructors Modifier Constructor Description protectedServerHttpSecurity()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ServerHttpSecurityaddFilterAfter(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order)Adds aWebFilterafter specific position.ServerHttpSecurityaddFilterAt(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order)Adds aWebFilterat a specific position.ServerHttpSecurityaddFilterBefore(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order)Adds aWebFilterbefore specific position.ServerHttpSecurity.AnonymousSpecanonymous()Enables and Configures anonymous authentication.ServerHttpSecurityanonymous(Customizer<ServerHttpSecurity.AnonymousSpec> anonymousCustomizer)Enables and Configures anonymous authentication.ServerHttpSecurityauthenticationManager(ReactiveAuthenticationManager manager)Configure the default authentication manager.ServerHttpSecurity.AuthorizeExchangeSpecauthorizeExchange()Configures authorization.ServerHttpSecurityauthorizeExchange(Customizer<ServerHttpSecurity.AuthorizeExchangeSpec> authorizeExchangeCustomizer)Configures authorization.SecurityWebFilterChainbuild()Builds theSecurityWebFilterChainServerHttpSecurity.CorsSpeccors()Configures CORS headers.ServerHttpSecuritycors(Customizer<ServerHttpSecurity.CorsSpec> corsCustomizer)Configures CORS headers.ServerHttpSecurity.CsrfSpeccsrf()Configures CSRF Protection which is enabled by default.ServerHttpSecuritycsrf(Customizer<ServerHttpSecurity.CsrfSpec> csrfCustomizer)Configures CSRF Protection which is enabled by default.ServerHttpSecurity.ExceptionHandlingSpecexceptionHandling()Configures exception handling (i.e.ServerHttpSecurityexceptionHandling(Customizer<ServerHttpSecurity.ExceptionHandlingSpec> exceptionHandlingCustomizer)Configures exception handling (i.e.ServerHttpSecurity.FormLoginSpecformLogin()Configures form based authentication.ServerHttpSecurityformLogin(Customizer<ServerHttpSecurity.FormLoginSpec> formLoginCustomizer)Configures form based authentication.ServerHttpSecurity.HeaderSpecheaders()Configures HTTP Response Headers.ServerHttpSecurityheaders(Customizer<ServerHttpSecurity.HeaderSpec> headerCustomizer)Configures HTTP Response Headers.static ServerHttpSecurityhttp()Creates a new instance.ServerHttpSecurity.HttpBasicSpechttpBasic()Configures HTTP Basic authentication.ServerHttpSecurityhttpBasic(Customizer<ServerHttpSecurity.HttpBasicSpec> httpBasicCustomizer)Configures HTTP Basic authentication.ServerHttpSecurity.LogoutSpeclogout()Configures log out.ServerHttpSecuritylogout(Customizer<ServerHttpSecurity.LogoutSpec> logoutCustomizer)Configures log out.ServerHttpSecurity.OAuth2ClientSpecoauth2Client()Configures the OAuth2 client.ServerHttpSecurityoauth2Client(Customizer<ServerHttpSecurity.OAuth2ClientSpec> oauth2ClientCustomizer)Configures the OAuth2 client.ServerHttpSecurity.OAuth2LoginSpecoauth2Login()Configures authentication support using an OAuth 2.0 and/or OpenID Connect 1.0 Provider.ServerHttpSecurityoauth2Login(Customizer<ServerHttpSecurity.OAuth2LoginSpec> oauth2LoginCustomizer)Configures authentication support using an OAuth 2.0 and/or OpenID Connect 1.0 Provider.ServerHttpSecurity.OAuth2ResourceServerSpecoauth2ResourceServer()Configures OAuth 2.0 Resource Server support.ServerHttpSecurityoauth2ResourceServer(Customizer<ServerHttpSecurity.OAuth2ResourceServerSpec> oauth2ResourceServerCustomizer)Configures OAuth 2.0 Resource Server support.ServerHttpSecurity.PasswordManagementSpecpasswordManagement()Configures password management.ServerHttpSecuritypasswordManagement(Customizer<ServerHttpSecurity.PasswordManagementSpec> passwordManagementCustomizer)Configures password management.ServerHttpSecurity.HttpsRedirectSpecredirectToHttps()Configures HTTPS redirection rules.ServerHttpSecurityredirectToHttps(Customizer<ServerHttpSecurity.HttpsRedirectSpec> httpsRedirectCustomizer)Configures HTTPS redirection rules.ServerHttpSecurity.RequestCacheSpecrequestCache()Configures the request cache which is used when a flow is interrupted (i.e.ServerHttpSecurityrequestCache(Customizer<ServerHttpSecurity.RequestCacheSpec> requestCacheCustomizer)Configures the request cache which is used when a flow is interrupted (i.e.ServerHttpSecuritysecurityContextRepository(ServerSecurityContextRepository securityContextRepository)The strategy used withReactorContextWebFilter.ServerHttpSecuritysecurityMatcher(ServerWebExchangeMatcher matcher)The ServerExchangeMatcher that determines which requests apply to this HttpSecurity instance.protected voidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext)ServerHttpSecurity.X509Specx509()Configures x509 authentication using a certificate provided by a client.ServerHttpSecurityx509(Customizer<ServerHttpSecurity.X509Spec> x509Customizer)Configures x509 authentication using a certificate provided by a client.
-
-
-
Method Detail
-
securityMatcher
public ServerHttpSecurity securityMatcher(ServerWebExchangeMatcher matcher)
The ServerExchangeMatcher that determines which requests apply to this HttpSecurity instance.- Parameters:
matcher- the ServerExchangeMatcher that determines which requests apply to this HttpSecurity instance. Default is all requests.- Returns:
- the
ServerHttpSecurityto continue configuring
-
addFilterAt
public ServerHttpSecurity addFilterAt(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order)
Adds aWebFilterat a specific position.- Parameters:
webFilter- theWebFilterto addorder- the place to insert theWebFilter- Returns:
- the
ServerHttpSecurityto continue configuring
-
addFilterBefore
public ServerHttpSecurity addFilterBefore(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order)
Adds aWebFilterbefore specific position.- Parameters:
webFilter- theWebFilterto addorder- the place before which to insert theWebFilter- Returns:
- the
ServerHttpSecurityto continue configuring - Since:
- 5.2.0
-
addFilterAfter
public ServerHttpSecurity addFilterAfter(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order)
Adds aWebFilterafter specific position.- Parameters:
webFilter- theWebFilterto addorder- the place after which to insert theWebFilter- Returns:
- the
ServerHttpSecurityto continue configuring - Since:
- 5.2.0
-
securityContextRepository
public ServerHttpSecurity securityContextRepository(ServerSecurityContextRepository securityContextRepository)
The strategy used withReactorContextWebFilter. It does impact how theSecurityContextis saved which is configured on a perAuthenticationWebFilterbasis.- Parameters:
securityContextRepository- the repository to use- Returns:
- the
ServerHttpSecurityto continue configuring
-
redirectToHttps
public ServerHttpSecurity.HttpsRedirectSpec redirectToHttps()
Configures HTTPS redirection rules. If the default is used:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .redirectToHttps(); return http.build(); }Then all non-HTTPS requests will be redirected to HTTPS. Typically, all requests should be HTTPS; however, the focus for redirection can also be narrowed:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .redirectToHttps() .httpsRedirectWhen((serverWebExchange) -> serverWebExchange.getRequest().getHeaders().containsKey("X-Requires-Https")) return http.build(); }- Returns:
- the
ServerHttpSecurity.HttpsRedirectSpecto customize
-
redirectToHttps
public ServerHttpSecurity redirectToHttps(Customizer<ServerHttpSecurity.HttpsRedirectSpec> httpsRedirectCustomizer)
Configures HTTPS redirection rules. If the default is used:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .redirectToHttps(withDefaults()); return http.build(); }Then all non-HTTPS requests will be redirected to HTTPS. Typically, all requests should be HTTPS; however, the focus for redirection can also be narrowed:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .redirectToHttps((redirectToHttps) -> redirectToHttps .httpsRedirectWhen((serverWebExchange) -> serverWebExchange.getRequest().getHeaders().containsKey("X-Requires-Https")) ); return http.build(); }- Parameters:
httpsRedirectCustomizer- theCustomizerto provide more options for theServerHttpSecurity.HttpsRedirectSpec- Returns:
- the
ServerHttpSecurityto customize
-
csrf
public ServerHttpSecurity.CsrfSpec csrf()
Configures CSRF Protection which is enabled by default. You can disable it using:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .csrf().disabled(); return http.build(); }Additional configuration options can be seen below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .csrf() // Handle CSRF failures .accessDeniedHandler(accessDeniedHandler) // Custom persistence of CSRF Token .csrfTokenRepository(csrfTokenRepository) // custom matching when CSRF protection is enabled .requireCsrfProtectionMatcher(matcher); return http.build(); }- Returns:
- the
ServerHttpSecurity.CsrfSpecto customize
-
csrf
public ServerHttpSecurity csrf(Customizer<ServerHttpSecurity.CsrfSpec> csrfCustomizer)
Configures CSRF Protection which is enabled by default. You can disable it using:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .csrf((csrf) -> csrf.disabled() ); return http.build(); }Additional configuration options can be seen below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .csrf((csrf) -> csrf // Handle CSRF failures .accessDeniedHandler(accessDeniedHandler) // Custom persistence of CSRF Token .csrfTokenRepository(csrfTokenRepository) // custom matching when CSRF protection is enabled .requireCsrfProtectionMatcher(matcher) ); return http.build(); }- Parameters:
csrfCustomizer- theCustomizerto provide more options for theServerHttpSecurity.CsrfSpec- Returns:
- the
ServerHttpSecurityto customize
-
cors
public ServerHttpSecurity.CorsSpec cors()
Configures CORS headers. By default if aCorsConfigurationSourceBean is found, it will be used to create aCorsWebFilter. IfServerHttpSecurity.CorsSpec.configurationSource(CorsConfigurationSource)is invoked it will be used instead. If neither has been configured, the Cors configuration will do nothing.- Returns:
- the
ServerHttpSecurity.CorsSpecto customize
-
cors
public ServerHttpSecurity cors(Customizer<ServerHttpSecurity.CorsSpec> corsCustomizer)
Configures CORS headers. By default if aCorsConfigurationSourceBean is found, it will be used to create aCorsWebFilter. IfServerHttpSecurity.CorsSpec.configurationSource(CorsConfigurationSource)is invoked it will be used instead. If neither has been configured, the Cors configuration will do nothing.- Parameters:
corsCustomizer- theCustomizerto provide more options for theServerHttpSecurity.CorsSpec- Returns:
- the
ServerHttpSecurityto customize
-
anonymous
public ServerHttpSecurity.AnonymousSpec anonymous()
Enables and Configures anonymous authentication. Anonymous Authentication is disabled by default.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .anonymous().key("key") .authorities("ROLE_ANONYMOUS"); return http.build(); }- Returns:
- the
ServerHttpSecurity.AnonymousSpecto customize - Since:
- 5.2.0
-
anonymous
public ServerHttpSecurity anonymous(Customizer<ServerHttpSecurity.AnonymousSpec> anonymousCustomizer)
Enables and Configures anonymous authentication. Anonymous Authentication is disabled by default.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .anonymous((anonymous) -> anonymous .key("key") .authorities("ROLE_ANONYMOUS") ); return http.build(); }- Parameters:
anonymousCustomizer- theCustomizerto provide more options for theServerHttpSecurity.AnonymousSpec- Returns:
- the
ServerHttpSecurityto customize
-
httpBasic
public ServerHttpSecurity.HttpBasicSpec httpBasic()
Configures HTTP Basic authentication. An example configuration is provided below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .httpBasic() // used for authenticating the credentials .authenticationManager(authenticationManager) // Custom persistence of the authentication .securityContextRepository(securityContextRepository); return http.build(); }- Returns:
- the
ServerHttpSecurity.HttpBasicSpecto customize
-
httpBasic
public ServerHttpSecurity httpBasic(Customizer<ServerHttpSecurity.HttpBasicSpec> httpBasicCustomizer)
Configures HTTP Basic authentication. An example configuration is provided below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .httpBasic((httpBasic) -> httpBasic // used for authenticating the credentials .authenticationManager(authenticationManager) // Custom persistence of the authentication .securityContextRepository(securityContextRepository) ); return http.build(); }- Parameters:
httpBasicCustomizer- theCustomizerto provide more options for theServerHttpSecurity.HttpBasicSpec- Returns:
- the
ServerHttpSecurityto customize
-
passwordManagement
public ServerHttpSecurity.PasswordManagementSpec passwordManagement()
Configures password management. An example configuration is provided below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .passwordManagement(); return http.build(); }- Returns:
- the
ServerHttpSecurity.PasswordManagementSpecto customize - Since:
- 5.6
-
passwordManagement
public ServerHttpSecurity passwordManagement(Customizer<ServerHttpSecurity.PasswordManagementSpec> passwordManagementCustomizer)
Configures password management. An example configuration is provided below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .passwordManagement(passwordManagement -> // Custom change password page. passwordManagement.changePasswordPage("/custom-change-password-page") ); return http.build(); }- Parameters:
passwordManagementCustomizer- theCustomizerto provide more options for theServerHttpSecurity.PasswordManagementSpec- Returns:
- the
ServerHttpSecurityto customize - Since:
- 5.6
-
formLogin
public ServerHttpSecurity.FormLoginSpec formLogin()
Configures form based authentication. An example configuration is provided below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .formLogin() // used for authenticating the credentials .authenticationManager(authenticationManager) // Custom persistence of the authentication .securityContextRepository(securityContextRepository) // expect a log in page at "/authenticate" // a POST "/authenticate" is where authentication occurs // error page at "/authenticate?error" .loginPage("/authenticate"); return http.build(); }- Returns:
- the
ServerHttpSecurity.FormLoginSpecto customize
-
formLogin
public ServerHttpSecurity formLogin(Customizer<ServerHttpSecurity.FormLoginSpec> formLoginCustomizer)
Configures form based authentication. An example configuration is provided below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .formLogin((formLogin) -> formLogin // used for authenticating the credentials .authenticationManager(authenticationManager) // Custom persistence of the authentication .securityContextRepository(securityContextRepository) // expect a log in page at "/authenticate" // a POST "/authenticate" is where authentication occurs // error page at "/authenticate?error" .loginPage("/authenticate") ); return http.build(); }- Parameters:
formLoginCustomizer- theCustomizerto provide more options for theServerHttpSecurity.FormLoginSpec- Returns:
- the
ServerHttpSecurityto customize
-
x509
public ServerHttpSecurity.X509Spec x509()
Configures x509 authentication using a certificate provided by a client.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http .x509() .authenticationManager(authenticationManager) .principalExtractor(principalExtractor); return http.build(); }Note that if extractor is not specified,SubjectDnX509PrincipalExtractorwill be used. If authenticationManager is not specified,ReactivePreAuthenticatedAuthenticationManagerwill be used.- Returns:
- the
ServerHttpSecurity.X509Specto customize - Since:
- 5.2
-
x509
public ServerHttpSecurity x509(Customizer<ServerHttpSecurity.X509Spec> x509Customizer)
Configures x509 authentication using a certificate provided by a client.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http .x509((x509) -> x509 .authenticationManager(authenticationManager) .principalExtractor(principalExtractor) ); return http.build(); }Note that if extractor is not specified,SubjectDnX509PrincipalExtractorwill be used. If authenticationManager is not specified,ReactivePreAuthenticatedAuthenticationManagerwill be used.- Parameters:
x509Customizer- theCustomizerto provide more options for theServerHttpSecurity.X509Spec- Returns:
- the
ServerHttpSecurityto customize - Since:
- 5.2
-
oauth2Login
public ServerHttpSecurity.OAuth2LoginSpec oauth2Login()
Configures authentication support using an OAuth 2.0 and/or OpenID Connect 1.0 Provider.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .oauth2Login() .authenticationConverter(authenticationConverter) .authenticationManager(manager); return http.build(); }- Returns:
- the
ServerHttpSecurity.OAuth2LoginSpecto customize
-
oauth2Login
public ServerHttpSecurity oauth2Login(Customizer<ServerHttpSecurity.OAuth2LoginSpec> oauth2LoginCustomizer)
Configures authentication support using an OAuth 2.0 and/or OpenID Connect 1.0 Provider.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .oauth2Login((oauth2Login) -> oauth2Login .authenticationConverter(authenticationConverter) .authenticationManager(manager) ); return http.build(); }- Parameters:
oauth2LoginCustomizer- theCustomizerto provide more options for theServerHttpSecurity.OAuth2LoginSpec- Returns:
- the
ServerHttpSecurityto customize
-
oauth2Client
public ServerHttpSecurity.OAuth2ClientSpec oauth2Client()
Configures the OAuth2 client.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .oauth2Client() .clientRegistrationRepository(clientRegistrationRepository) .authorizedClientRepository(authorizedClientRepository); return http.build(); }- Returns:
- the
ServerHttpSecurity.OAuth2ClientSpecto customize
-
oauth2Client
public ServerHttpSecurity oauth2Client(Customizer<ServerHttpSecurity.OAuth2ClientSpec> oauth2ClientCustomizer)
Configures the OAuth2 client.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .oauth2Client((oauth2Client) -> oauth2Client .clientRegistrationRepository(clientRegistrationRepository) .authorizedClientRepository(authorizedClientRepository) ); return http.build(); }- Parameters:
oauth2ClientCustomizer- theCustomizerto provide more options for theServerHttpSecurity.OAuth2ClientSpec- Returns:
- the
ServerHttpSecurityto customize
-
oauth2ResourceServer
public ServerHttpSecurity.OAuth2ResourceServerSpec oauth2ResourceServer()
Configures OAuth 2.0 Resource Server support.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .oauth2ResourceServer() .jwt() .publicKey(publicKey()); return http.build(); }- Returns:
- the
ServerHttpSecurity.OAuth2ResourceServerSpecto customize
-
oauth2ResourceServer
public ServerHttpSecurity oauth2ResourceServer(Customizer<ServerHttpSecurity.OAuth2ResourceServerSpec> oauth2ResourceServerCustomizer)
Configures OAuth 2.0 Resource Server support.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .oauth2ResourceServer((oauth2ResourceServer) -> oauth2ResourceServer .jwt((jwt) -> jwt .publicKey(publicKey()) ) ); return http.build(); }- Parameters:
oauth2ResourceServerCustomizer- theCustomizerto provide more options for theServerHttpSecurity.OAuth2ResourceServerSpec- Returns:
- the
ServerHttpSecurityto customize
-
headers
public ServerHttpSecurity.HeaderSpec headers()
Configures HTTP Response Headers. The default headers are:Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 X-Content-Type-Options: nosniff Strict-Transport-Security: max-age=31536000 ; includeSubDomains X-Frame-Options: DENY X-XSS-Protection: 1; mode=block
such that "Strict-Transport-Security" is only added on secure requests. An example configuration is provided below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .headers() // customize frame options to be same origin .frameOptions() .mode(XFrameOptionsServerHttpHeadersWriter.Mode.SAMEORIGIN) .and() // disable cache control .cache().disable(); return http.build(); }- Returns:
- the
ServerHttpSecurity.HeaderSpecto customize
-
headers
public ServerHttpSecurity headers(Customizer<ServerHttpSecurity.HeaderSpec> headerCustomizer)
Configures HTTP Response Headers. The default headers are:Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 X-Content-Type-Options: nosniff Strict-Transport-Security: max-age=31536000 ; includeSubDomains X-Frame-Options: DENY X-XSS-Protection: 1; mode=block
such that "Strict-Transport-Security" is only added on secure requests. An example configuration is provided below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .headers((headers) -> headers // customize frame options to be same origin .frameOptions((frameOptions) -> frameOptions .mode(XFrameOptionsServerHttpHeadersWriter.Mode.SAMEORIGIN) ) // disable cache control .cache((cache) -> cache .disable() ) ); return http.build(); }- Parameters:
headerCustomizer- theCustomizerto provide more options for theServerHttpSecurity.HeaderSpec- Returns:
- the
ServerHttpSecurityto customize
-
exceptionHandling
public ServerHttpSecurity.ExceptionHandlingSpec exceptionHandling()
Configures exception handling (i.e. handles when authentication is requested). An example configuration can be found below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .exceptionHandling() // customize how to request for authentication .authenticationEntryPoint(entryPoint); return http.build(); }- Returns:
- the
ServerHttpSecurity.ExceptionHandlingSpecto customize
-
exceptionHandling
public ServerHttpSecurity exceptionHandling(Customizer<ServerHttpSecurity.ExceptionHandlingSpec> exceptionHandlingCustomizer)
Configures exception handling (i.e. handles when authentication is requested). An example configuration can be found below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .exceptionHandling((exceptionHandling) -> exceptionHandling // customize how to request for authentication .authenticationEntryPoint(entryPoint) ); return http.build(); }- Parameters:
exceptionHandlingCustomizer- theCustomizerto provide more options for theServerHttpSecurity.ExceptionHandlingSpec- Returns:
- the
ServerHttpSecurityto customize
-
authorizeExchange
public ServerHttpSecurity.AuthorizeExchangeSpec authorizeExchange()
Configures authorization. An example configuration can be found below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .authorizeExchange() // any URL that starts with /admin/ requires the role "ROLE_ADMIN" .pathMatchers("/admin/**").hasRole("ADMIN") // a POST to /users requires the role "USER_POST" .pathMatchers(HttpMethod.POST, "/users").hasAuthority("USER_POST") // a request to /users/{username} requires the current authentication's username // to be equal to the {username} .pathMatchers("/users/{username}").access((authentication, context) -> authentication .map(Authentication::getName) .map((username) -> username.equals(context.getVariables().get("username"))) .map(AuthorizationDecision::new) ) // allows providing a custom matching strategy that requires the role "ROLE_CUSTOM" .matchers(customMatcher).hasRole("CUSTOM") // any other request requires the user to be authenticated .anyExchange().authenticated(); return http.build(); }- Returns:
- the
ServerHttpSecurity.AuthorizeExchangeSpecto customize
-
authorizeExchange
public ServerHttpSecurity authorizeExchange(Customizer<ServerHttpSecurity.AuthorizeExchangeSpec> authorizeExchangeCustomizer)
Configures authorization. An example configuration can be found below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .authorizeExchange((exchanges) -> exchanges // any URL that starts with /admin/ requires the role "ROLE_ADMIN" .pathMatchers("/admin/**").hasRole("ADMIN") // a POST to /users requires the role "USER_POST" .pathMatchers(HttpMethod.POST, "/users").hasAuthority("USER_POST") // a request to /users/{username} requires the current authentication's username // to be equal to the {username} .pathMatchers("/users/{username}").access((authentication, context) -> authentication .map(Authentication::getName) .map((username) -> username.equals(context.getVariables().get("username"))) .map(AuthorizationDecision::new) ) // allows providing a custom matching strategy that requires the role "ROLE_CUSTOM" .matchers(customMatcher).hasRole("CUSTOM") // any other request requires the user to be authenticated .anyExchange().authenticated() ); return http.build(); }- Parameters:
authorizeExchangeCustomizer- theCustomizerto provide more options for theServerHttpSecurity.AuthorizeExchangeSpec- Returns:
- the
ServerHttpSecurityto customize
-
logout
public ServerHttpSecurity.LogoutSpec logout()
Configures log out. An example configuration can be found below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .logout() // configures how log out is done .logoutHandler(logoutHandler) // log out will be performed on POST /signout .logoutUrl("/signout") // configure what is done on logout success .logoutSuccessHandler(successHandler); return http.build(); }- Returns:
- the
ServerHttpSecurity.LogoutSpecto customize
-
logout
public ServerHttpSecurity logout(Customizer<ServerHttpSecurity.LogoutSpec> logoutCustomizer)
Configures log out. An example configuration can be found below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .logout((logout) -> logout // configures how log out is done .logoutHandler(logoutHandler) // log out will be performed on POST /signout .logoutUrl("/signout") // configure what is done on logout success .logoutSuccessHandler(successHandler) ); return http.build(); }- Parameters:
logoutCustomizer- theCustomizerto provide more options for theServerHttpSecurity.LogoutSpec- Returns:
- the
ServerHttpSecurityto customize
-
requestCache
public ServerHttpSecurity.RequestCacheSpec requestCache()
Configures the request cache which is used when a flow is interrupted (i.e. due to requesting credentials) so that the request can be replayed after authentication. An example configuration can be found below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .requestCache() // configures how the request is cached .requestCache(requestCache); return http.build(); }- Returns:
- the
ServerHttpSecurity.RequestCacheSpecto customize
-
requestCache
public ServerHttpSecurity requestCache(Customizer<ServerHttpSecurity.RequestCacheSpec> requestCacheCustomizer)
Configures the request cache which is used when a flow is interrupted (i.e. due to requesting credentials) so that the request can be replayed after authentication. An example configuration can be found below:@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .requestCache((requestCache) -> requestCache // configures how the request is cached .requestCache(customRequestCache) ); return http.build(); }- Parameters:
requestCacheCustomizer- theCustomizerto provide more options for theServerHttpSecurity.RequestCacheSpec- Returns:
- the
ServerHttpSecurityto customize
-
authenticationManager
public ServerHttpSecurity authenticationManager(ReactiveAuthenticationManager manager)
Configure the default authentication manager.- Parameters:
manager- the authentication manager to use- Returns:
- the
ServerHttpSecurityto customize
-
build
public SecurityWebFilterChain build()
Builds theSecurityWebFilterChain- Returns:
- the
SecurityWebFilterChain
-
http
public static ServerHttpSecurity http()
Creates a new instance.- Returns:
- the new
ServerHttpSecurityinstance
-
setApplicationContext
protected void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException- Throws:
org.springframework.beans.BeansException
-
-