Class ServerHttpSecurity
java.lang.Object
org.springframework.security.config.web.server.ServerHttpSecurity
A 
ServerHttpSecurity is similar to Spring Security's HttpSecurity but
 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 using
 securityMatcher(ServerWebExchangeMatcher) or other similar methods.
 A minimal configuration can be found below:
 
 @Configuration
 @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 the
 ServerHttpSecurity.
 
 @Configuration
 @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 SummaryNested ClassesModifier and TypeClassDescriptionfinal classConfigures anonymous authenticationclassConfigures authorizationfinal classConfigures CORS support within Spring Security.final classConfigures CSRF Protectionfinal classConfigures exception handlingfinal classConfigures Form Based authenticationfinal classConfigures HTTP Response Headers.final classConfigures HTTP Basic AuthenticationclassConfigures HTTPS redirection rulesfinal classConfigures log outfinal classfinal classclassConfigures OAuth2 Resource Server Supportfinal classConfigures OIDC 1.0 Logout supportfinal classConfigures One-Time Token Login Supportfinal classConfigures password management.final classConfigures the request cache which is used when a flow is interrupted (i.e.classConfigures how sessions are managed.final classConfigures X509 authentication
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionaddFilterAfter(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order) Adds aWebFilterafter specific position.addFilterAt(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order) Adds aWebFilterat a specific position.addFilterBefore(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order) Adds aWebFilterbefore specific position.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.anonymous(Customizer<ServerHttpSecurity.AnonymousSpec> anonymousCustomizer) Enables and Configures anonymous authentication.Configure the default authentication manager.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.authorizeExchange(Customizer<ServerHttpSecurity.AuthorizeExchangeSpec> authorizeExchangeCustomizer) Configures authorization.build()Builds theSecurityWebFilterChaincors()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.cors(Customizer<ServerHttpSecurity.CorsSpec> corsCustomizer) Configures CORS headers.csrf()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.csrf(Customizer<ServerHttpSecurity.CsrfSpec> csrfCustomizer) Configures CSRF Protection which is enabled by default.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.exceptionHandling(Customizer<ServerHttpSecurity.ExceptionHandlingSpec> exceptionHandlingCustomizer) Configures exception handling (i.e.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.formLogin(Customizer<ServerHttpSecurity.FormLoginSpec> formLoginCustomizer) Configures form based authentication.headers()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.headers(Customizer<ServerHttpSecurity.HeaderSpec> headerCustomizer) Configures HTTP Response Headers.static ServerHttpSecurityhttp()Creates a new instance.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.httpBasic(Customizer<ServerHttpSecurity.HttpBasicSpec> httpBasicCustomizer) Configures HTTP Basic authentication.logout()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.logout(Customizer<ServerHttpSecurity.LogoutSpec> logoutCustomizer) Configures log out.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.oauth2Client(Customizer<ServerHttpSecurity.OAuth2ClientSpec> oauth2ClientCustomizer) Configures the OAuth2 client.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.oauth2Login(Customizer<ServerHttpSecurity.OAuth2LoginSpec> oauth2LoginCustomizer) Configures authentication support using an OAuth 2.0 and/or OpenID Connect 1.0 Provider.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.oauth2ResourceServer(Customizer<ServerHttpSecurity.OAuth2ResourceServerSpec> oauth2ResourceServerCustomizer) Configures OAuth 2.0 Resource Server support.oidcLogout(Customizer<ServerHttpSecurity.OidcLogoutSpec> oidcLogoutCustomizer) Configures OIDC Connect 1.0 Logout support.oneTimeTokenLogin(Customizer<ServerHttpSecurity.OneTimeTokenLoginSpec> oneTimeTokenLoginCustomizer) Configures One-Time Token Login Support.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.passwordManagement(Customizer<ServerHttpSecurity.PasswordManagementSpec> passwordManagementCustomizer) Configures password management.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.redirectToHttps(Customizer<ServerHttpSecurity.HttpsRedirectSpec> httpsRedirectCustomizer) Configures HTTPS redirection rules.Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.requestCache(Customizer<ServerHttpSecurity.RequestCacheSpec> requestCacheCustomizer) Configures the request cache which is used when a flow is interrupted (i.e.securityContextRepository(ServerSecurityContextRepository securityContextRepository) The strategy used withReactorContextWebFilter.securityMatcher(ServerWebExchangeMatcher matcher) The ServerExchangeMatcher that determines which requests apply to this HttpSecurity instance.Configures Session Management.protected voidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext) x509()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0.x509(Customizer<ServerHttpSecurity.X509Spec> x509Customizer) Configures x509 authentication using a certificate provided by a client.
- 
Constructor Details- 
ServerHttpSecurityprotected ServerHttpSecurity()
 
- 
- 
Method Details- 
securityMatcherThe 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
 
- 
addFilterAtpublic ServerHttpSecurity addFilterAt(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order) Adds aWebFilterat a specific position.- Parameters:
- webFilter- the- WebFilterto add
- order- the place to insert the- WebFilter
- Returns:
- the ServerHttpSecurityto continue configuring
 
- 
addFilterBeforepublic ServerHttpSecurity addFilterBefore(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order) Adds aWebFilterbefore specific position.- Parameters:
- webFilter- the- WebFilterto add
- order- the place before which to insert the- WebFilter
- Returns:
- the ServerHttpSecurityto continue configuring
- Since:
- 5.2.0
 
- 
addFilterAfterpublic ServerHttpSecurity addFilterAfter(org.springframework.web.server.WebFilter webFilter, SecurityWebFiltersOrder order) Adds aWebFilterafter specific position.- Parameters:
- webFilter- the- WebFilterto add
- order- the place after which to insert the- WebFilter
- Returns:
- the ServerHttpSecurityto continue configuring
- Since:
- 5.2.0
 
- 
securityContextRepositorypublic 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@Deprecated(since="6.1", forRemoval=true) public ServerHttpSecurity.HttpsRedirectSpec redirectToHttps()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. UseredirectToHttps(Customizer)orredirectToHttps(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
redirectToHttpspublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.HttpsRedirectSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
csrfDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Usecsrf(Customizer)orcsrf(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
csrfConfigures 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- the- Customizerto provide more options for the- ServerHttpSecurity.CsrfSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
corsDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Usecors(Customizer)orcors(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
corsConfigures 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- the- Customizerto provide more options for the- ServerHttpSecurity.CorsSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
anonymousDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Useanonymous(Customizer)oranonymous(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
anonymouspublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.AnonymousSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
httpBasicDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. UsehttpBasic(Customizer)orhttpBasic(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
httpBasicpublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.HttpBasicSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
sessionManagementpublic ServerHttpSecurity sessionManagement(Customizer<ServerHttpSecurity.SessionManagementSpec> customizer) Configures Session Management. An example configuration is provided below:@Bean SecurityWebFilterChain filterChain(ServerHttpSecurity http, ReactiveSessionRegistry sessionRegistry) { http // ... .sessionManagement((sessionManagement) -> sessionManagement .concurrentSessions((concurrentSessions) -> concurrentSessions .maxSessions(1) .maxSessionsPreventsLogin(true) .sessionRegistry(sessionRegistry) ) ); return http.build(); }- Parameters:
- customizer- the- Customizerto provide more options for the- ServerHttpSecurity.SessionManagementSpec
- Returns:
- the ServerHttpSecurityto continue configuring
- Since:
- 6.3
 
- 
passwordManagement@Deprecated(since="6.1", forRemoval=true) public ServerHttpSecurity.PasswordManagementSpec passwordManagement()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. UsepasswordManagement(Customizer)orpasswordManagement(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
passwordManagementpublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.PasswordManagementSpec
- Returns:
- the ServerHttpSecurityto customize
- Since:
- 5.6
 
- 
formLoginDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. UseformLogin(Customizer)orformLogin(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
formLoginpublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.FormLoginSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
x509Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Usex509(Customizer)orx509(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
x509Configures 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- the- Customizerto provide more options for the- ServerHttpSecurity.X509Spec
- Returns:
- the ServerHttpSecurityto customize
- Since:
- 5.2
 
- 
oauth2LoginDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Useoauth2Login(Customizer)oroauth2Login(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
oauth2Loginpublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.OAuth2LoginSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
oauth2ClientDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Useoauth2Client(Customizer)oroauth2Client(Customizer.withDefaults())to stick with defaults. See the documentation for more details.Configures the OAuth2 client.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .oauth2Client() .clientRegistrationRepository(clientRegistrationRepository) .authorizedClientRepository(authorizedClientRepository); return http.build(); }- Returns:
- the ServerHttpSecurity.OAuth2ClientSpecto customize
 
- 
oauth2Clientpublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.OAuth2ClientSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
oauth2ResourceServer@Deprecated(since="6.1", forRemoval=true) public ServerHttpSecurity.OAuth2ResourceServerSpec oauth2ResourceServer()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Useoauth2ResourceServer(Customizer)insteadConfigures 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
 
- 
oauth2ResourceServerpublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.OAuth2ResourceServerSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
oidcLogoutpublic ServerHttpSecurity oidcLogout(Customizer<ServerHttpSecurity.OidcLogoutSpec> oidcLogoutCustomizer) Configures OIDC Connect 1.0 Logout support.@Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http // ... .oidcLogout((logout) -> logout .backChannel(Customizer.withDefaults()) ); return http.build(); }- Parameters:
- oidcLogoutCustomizer- the- Customizerto provide more options for the- ServerHttpSecurity.OidcLogoutSpec
- Returns:
- the ServerHttpSecurityto customize
- Since:
- 6.2
 
- 
headersDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Useheaders(Customizer)orheaders(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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: 0 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
 
- 
headersConfigures 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: 0 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- the- Customizerto provide more options for the- ServerHttpSecurity.HeaderSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
exceptionHandling@Deprecated(since="6.1", forRemoval=true) public ServerHttpSecurity.ExceptionHandlingSpec exceptionHandling()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. UseexceptionHandling(Customizer)orexceptionHandling(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
exceptionHandlingpublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.ExceptionHandlingSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
authorizeExchange@Deprecated(since="6.1", forRemoval=true) public ServerHttpSecurity.AuthorizeExchangeSpec authorizeExchange()Deprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. UseauthorizeExchange(Customizer)orauthorizeExchange(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
authorizeExchangepublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.AuthorizeExchangeSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
logoutDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. Uselogout(Customizer)orlogout(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
logoutConfigures 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- the- Customizerto provide more options for the- ServerHttpSecurity.LogoutSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
requestCacheDeprecated, for removal: This API element is subject to removal in a future version.For removal in 7.0. UserequestCache(Customizer)orrequestCache(Customizer.withDefaults())to stick with defaults. See the documentation for more details.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
 
- 
requestCachepublic 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- the- Customizerto provide more options for the- ServerHttpSecurity.RequestCacheSpec
- Returns:
- the ServerHttpSecurityto customize
 
- 
authenticationManagerConfigure the default authentication manager.- Parameters:
- manager- the authentication manager to use
- Returns:
- the ServerHttpSecurityto customize
 
- 
oneTimeTokenLoginpublic ServerHttpSecurity oneTimeTokenLogin(Customizer<ServerHttpSecurity.OneTimeTokenLoginSpec> oneTimeTokenLoginCustomizer) Configures One-Time Token Login Support.Example Configuration@Configuration @EnableWebFluxSecurity public class SecurityConfig { @Bean public SecurityWebFilterChain securityFilterChain(ServerHttpSecurity http) throws Exception { http // ... .oneTimeTokenLogin(Customizer.withDefaults()); return http.build(); } @Bean public ServerOneTimeTokenGenerationSuccessHandler oneTimeTokenGenerationSuccessHandler() { return new MyMagicLinkServerOneTimeTokenGenerationSuccessHandler(); } }- Parameters:
- oneTimeTokenLoginCustomizer- the- Customizerto provide more options for the- ServerHttpSecurity.OneTimeTokenLoginSpec
- Returns:
- the ServerHttpSecurityfor further customizations
 
- 
buildBuilds theSecurityWebFilterChain- Returns:
- the SecurityWebFilterChain
 
- 
httpCreates a new instance.- Returns:
- the new ServerHttpSecurityinstance
 
- 
setApplicationContextprotected void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException - Throws:
- org.springframework.beans.BeansException
 
 
-