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, Spring Boot auto-configures form-based login and HTTP Basic authentication for all endpoints. The auto-configuration is designed to back off completely when you define your own security beans, giving you full control without fighting the defaults. This guide covers the most common security customizations step by step.
Understand the default security configuration
Out of the box, Spring Boot provides:- A single in-memory user with username
userand a randomly generated password printed atWARNlevel on startup. - Form-based login or HTTP Basic auth for all requests (selected by content negotiation on the
Acceptheader). - A
DefaultAuthenticationEventPublisherfor publishing authentication events.
Switch to a custom UserDetailsService
To replace the auto-configured in-memoryUserDetailsService with your own, define a bean of type UserDetailsService:
UserDetailsService, AuthenticationProvider, or AuthenticationManager bean, it backs off the auto-configured InMemoryUserDetailsManager.
Use in-memory users for development
For local development and testing, define anInMemoryUserDetailsManager bean explicitly:
Override Spring Security auto-configuration
To completely replace the default web security settings, define aSecurityFilterChain bean:
SpringBootWebSecurityConfiguration while leaving UserDetailsServiceAutoConfiguration in place (unless you also define your own UserDetailsService).
Secure specific URL patterns
To apply different rules to different URL paths — for example, permit/public/** without authentication and require authentication for /api/**:
requestMatchers with Ant patterns to match URL paths. Rules are evaluated in the order they are declared — place more specific patterns before broader ones.
Add HTTP Basic auth alongside form login
To accept both browser-based form logins and programmatic HTTP Basic authentication:Accept header: browsers receive the login form redirect, and API clients receive a 401 with a WWW-Authenticate: Basic header.
Disable CSRF protection for REST APIs
CSRF protection is enabled by default and is important for browser-based applications. For stateless REST APIs that authenticate via tokens rather than cookies, disable it:Configure CORS
To define cross-origin resource sharing rules, add aCorsConfigurationSource bean and enable CORS in the security filter chain:
CORS configuration in the
SecurityFilterChain takes precedence over @CrossOrigin annotations and MVC-level CORS mappings. Define it here when you need to apply it uniformly across all secured endpoints.Configure OAuth2 login with GitHub or Google
To add social login, follow these steps:Register your application with the provider
Register a new OAuth2 application on your chosen provider’s developer console:
- GitHub: https://github.com/settings/developers
- Google: https://console.cloud.google.com/apis/credentials
http://localhost:8080/login/oauth2/code/{registrationId} (replace localhost:8080 with your actual host and {registrationId} with github or google).Add credentials to application.yaml
github and google — you only need the client ID and secret.Enable HTTPS when behind a proxy
If your application runs behind a reverse proxy that terminates SSL, configure Tomcat to trust the forwarded headers:RemoteIpValve, which causes HttpServletRequest.isSecure() to return true when the x-forwarded-proto: https header is present. This allows Spring Security’s requiresSecure() checks to work correctly downstream of the proxy.