Spring Security integrates with Spring Boot through a single starter that activates comprehensive protection by default. WhenDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt
Use this file to discover all available pages before exploring further.
spring-boot-starter-security is on the classpath, every endpoint in your application — including Spring Boot’s own /error path and Actuator endpoints — is secured automatically, requiring authentication before any request succeeds.
Default security behavior
The defaultUserDetailsService has a single user. The username is user and the password is randomly generated and printed at WARN level when the application starts:
application.yaml
- A
UserDetailsService(orReactiveUserDetailsServicefor WebFlux) with an in-memory store and a single user - Form-based login or HTTP Basic security depending on the
Acceptheader in the request - A
DefaultAuthenticationEventPublisherfor publishing authentication events
MVC security configuration
To replace the default behavior, add aSecurityFilterChain bean. Adding this bean disables Spring Boot’s default web security configuration while leaving the UserDetailsService configuration in place.
MySecurityConfiguration.java
EndpointRequest— creates aRequestMatcherbased on themanagement.endpoints.web.base-pathpropertyPathRequest— creates aRequestMatcherfor commonly used resource locations
UserDetailsService configuration, add a bean of type UserDetailsService, AuthenticationProvider, or AuthenticationManager.
The auto-configuration of a
UserDetailsService backs off automatically when any of spring-security-oauth2-client, spring-security-oauth2-resource-server, or spring-security-saml2-service-provider is on the classpath.WebFlux security
For WebFlux applications, addspring-boot-starter-security and configure a SecurityWebFilterChain bean:
MyWebFluxSecurityConfiguration.java
Authentication flows
HTTP Basic and form login
HTTP Basic and form login
HTTP Basic and form-based login are enabled by Spring Boot’s default auto-configuration. To customize, define your own
SecurityFilterChain with explicit httpBasic() or formLogin() configuration.Configure a custom UserDetailsService to load users from a database:MyUserDetailsService.java
OAuth2 login
OAuth2 login
Add Add OAuth2 login to your security configuration:
spring-boot-starter-oauth2-client and configure your OAuth2 provider in application.yaml. Spring Boot includes pre-built support for common providers (Google, GitHub, Okta):application.yaml
MyOAuth2SecurityConfiguration.java
OAuth2 resource server (JWT)
OAuth2 resource server (JWT)
Add Configure the security filter chain:Spring Boot auto-configures a
spring-boot-starter-oauth2-resource-server to protect your API with JWT bearer tokens. Configure the issuer URI of your authorization server:application.yaml
MyResourceServerConfiguration.java
JwtDecoder bean using the issuer URI to discover the JWKS endpoint.SAML 2.0
SAML 2.0
SAML v2.0 is a widely adopted framework for exchanging security information between business partners. Add Spring Boot auto-configures a
spring-security-saml2-service-provider and configure your identity provider:application.yaml
RelyingPartyRegistrationRepository and the SAML2 login filter chain when the dependency is present.Method security
Method security
Add method-level security using Secure service methods with standard annotations:
@EnableMethodSecurity on a @Configuration class:MyMethodSecurityConfiguration.java
MyService.java
H2 console in a secured application
The H2 console uses frames and does not implement CSRF protection. In a secured application you must configure Spring Security to allow access:DevProfileSecurityConfiguration.java