Spring Boot's security auto-configuration provides opinionated defaults for securing web applications, supporting both Servlet and Reactive stacks. It integrates deeply with Spring Security to provide authentication, authorization, OAuth2 support (Client, Resource Server, and Authorization Server), and SAML2 integration.
When spring-security-web is on the classpath, Spring Boot applies default security to all endpoints, including Actuator and error pages.
The auto-configuration behavior differs slightly between stacks:
ServletWebSecurityAutoConfiguration module/spring-boot-security/src/main/java/org/springframework/boot/security/autoconfigure/web/servlet/ServletWebSecurityAutoConfiguration.java26-30 It configures the default SecurityFilterChain when no user-defined filter chain is present.ReactiveWebSecurityAutoConfiguration. It sets up the default SecurityWebFilterChain for non-blocking applications.By default, Spring Boot configures an in-memory UserDetailsService (or ReactiveUserDetailsService) with a single user named user and a generated password. This behavior is triggered by UserDetailsServiceAutoConfiguration. Authentication can be customized via properties:
spring.security.user.namespring.security.user.passwordSpring Boot provides specialized matchers to ease the configuration of common paths:
RequestMatcher based on the Actuator management.endpoints.web.base-path. It is implemented for both Servlet module/spring-boot-security/src/main/java/org/springframework/boot/security/autoconfigure/actuate/web/servlet/EndpointRequest.java1-20 and Reactive module/spring-boot-security/src/main/java/org/springframework/boot/security/autoconfigure/actuate/web/reactive/EndpointRequest.java1-20 stacks./css/**, /js/**, /favicon.ico) module/spring-boot-security/src/main/java/org/springframework/boot/security/autoconfigure/web/servlet/ServletWebSecurityAutoConfiguration.java26-27Spring Boot provides extensive auto-configuration for modern identity protocols. When these modules are configured, the default UserDetailsService typically backs off to allow the external identity provider to manage users.
Supports both JWT and Opaque Token validation. The configuration is driven by OAuth2ResourceServerProperties module/spring-boot-security-oauth2-resource-server/src/main/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/OAuth2ResourceServerProperties.java43-44
Auto-configures a JwtDecoder (Servlet) or ReactiveJwtDecoder (Reactive) based on properties like jwk-set-uri, issuer-uri, or a local public-key-location module/spring-boot-security-oauth2-resource-server/src/main/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/OAuth2ResourceServerProperties.java58-80
JwkSetUriCondition checks for the presence of the spring.security.oauth2.resourceserver.jwt.jwk-set-uri property module/spring-boot-security-oauth2-resource-server/src/main/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/JwkSetUriCondition.java32-43JwkSetUriJwtDecoderBuilderCustomizer or SpringReactiveOpaqueTokenIntrospectorBuilderCustomizer to refine the decoder or introspector construction module/spring-boot-security-oauth2-resource-server/src/main/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/reactive/ReactiveOpaqueTokenIntrospectionClientConfiguration.java44-58Configures a ReactiveOpaqueTokenIntrospector (or Servlet equivalent) using the provided introspection-uri, client-id, and client-secret module/spring-boot-security-oauth2-resource-server/src/main/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/reactive/ReactiveOpaqueTokenIntrospectionClientConfiguration.java41-60
Managed by OAuth2AuthorizationServerAutoConfiguration. It sets up the necessary endpoints for OIDC and OAuth2 authorization, such as /token, /authorize, and /.well-known/openid-configuration module/spring-boot-security-oauth2-authorization-server/src/main/java/org/springframework/boot/security/oauth2/server/authorization/autoconfigure/servlet/OAuth2AuthorizationServerAutoConfiguration.java1-30
authorizationServerSecurityFilterChain bean to handle OAuth2 protocol endpoints module/spring-boot-security-oauth2-authorization-server/src/test/java/org/springframework/boot/security/oauth2/server/authorization/autoconfigure/servlet/OAuth2AuthorizationServerWebSecurityConfigurationTests.java80-95InMemoryRegisteredClientRepository based on properties under spring.security.oauth2.authorizationserver.client.* module/spring-boot-security-oauth2-authorization-server/src/test/java/org/springframework/boot/security/oauth2/server/authorization/autoconfigure/servlet/OAuth2AuthorizationServerWebSecurityConfigurationTests.java72-83Configures a RelyingPartyRegistrationRepository using Saml2RelyingPartyRegistrationConfiguration module/spring-boot-security-saml2/src/main/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyRegistrationConfiguration.java68-78
RelyingPartyRegistrations.collectionFromMetadataLocation module/spring-boot-security-saml2/src/main/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyRegistrationConfiguration.java122-134Saml2X509Credential module/spring-boot-security-saml2/src/main/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyRegistrationConfiguration.java91-107The following diagram illustrates how Spring Boot maps configuration properties and classpath presence to Spring Security bean definitions.
| Property/Class | Logic Entity | Code Implementation |
|---|---|---|
spring.security.oauth2.resourceserver.jwt.* | JwtDecoder | JwtDecoderConfiguration |
spring.security.oauth2.authorizationserver.client.* | RegisteredClientRepository | OAuth2AuthorizationServerAutoConfiguration |
WebClient.class | Reactive Support | ReactiveOpaqueTokenIntrospectionClientConfiguration |
Security Auto-Configuration Architecture
Sources: module/spring-boot-security/src/main/java/org/springframework/boot/security/autoconfigure/web/servlet/ServletWebSecurityAutoConfiguration.java26-30 module/spring-boot-security-oauth2-resource-server/src/main/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/OAuth2ResourceServerProperties.java43-44 module/spring-boot-security-saml2/src/main/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyRegistrationConfiguration.java68-71
Spring Boot provides the spring-boot-starter-security-test (and stack-specific variants like spring-boot-starter-security-saml2-test) to facilitate testing secured applications smoke-test/spring-boot-smoke-test-saml2-service-provider/build.gradle27-28
SecurityMockMvcAutoConfiguration automatically configures MockMvc with Spring Security's SecurityContext support module/spring-boot-security-test/src/main/java/org/springframework/boot/security/test/autoconfigure/webmvc/SecurityMockMvcAutoConfiguration.java47-49
SecurityMockMvcRequestPostProcessors.testSecurityContext() to ensure requests run with the user in the TestSecurityContextHolder module/spring-boot-security-test/src/main/java/org/springframework/boot/security/test/autoconfigure/webmvc/SecurityMockMvcAutoConfiguration.java77-92HtmlUnitDriver is present, it configures a MockMvcHtmlUnitDriverCustomizer that sets a DelegatingSecurityContextExecutor module/spring-boot-security-test/src/main/java/org/springframework/boot/security/test/autoconfigure/webmvc/SecurityMockMvcAutoConfiguration.java61-69Code Entity Association: Testing
| Feature | Auto-Configuration Class | Properties Prefix |
|---|---|---|
| Default Servlet Security | ServletWebSecurityAutoConfiguration | spring.security.* |
| OAuth2 Resource Server | OAuth2ResourceServerAutoConfiguration | spring.security.oauth2.resourceserver.* |
| OAuth2 Auth Server | OAuth2AuthorizationServerAutoConfiguration | spring.security.oauth2.authorizationserver.* |
| SAML2 Relying Party | Saml2RelyingPartyAutoConfiguration | spring.security.saml2.relyingparty.* |
| MockMvc Security Test | SecurityMockMvcAutoConfiguration | N/A |
Sources: module/spring-boot-security-oauth2-resource-server/src/main/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/OAuth2ResourceServerProperties.java43-44 module/spring-boot-security-saml2/src/main/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyRegistrationConfiguration.java68-71 module/spring-boot-security-test/src/main/java/org/springframework/boot/security/test/autoconfigure/webmvc/SecurityMockMvcAutoConfiguration.java47-49
Refresh this wiki