This document has been created to answer the question of what is the best way to understand Spring Security and customise configuration on demand.
So we will start with the default configuration first and move to the customisation.
1. Default Spring Security Setup (Basic)
Description
A minimal setup relying on Spring Boot’s auto-configuration. Ideal for quick prototypes or standard username/password-based logins.
Key Characteristics
Uses
DaoAuthenticationProvider
automaticallyRelies on
AuthenticationConfiguration
— Spring Boot’s default internal configuration class — to create theAuthenticationManager
This class becomes the first point of customisation if you want to override the default authentication behaviour
Minimal manual setup required
Example
@Configuration
public class ApplicationConfiguration {
@Bean
public UserDetailsService userDetailsService(UserRepository userRepository) {
return username -> userRepository.findByEmail(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationProvider authenticationProvider(UserDetailsService userDetailsService) {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}
}
🛠️ 2. Advanced Security Setup (Custom)
Description
A fully customizable setup allowing for token-based authentication, path-specific security chains, and integration of filters like JWT validation.
Key Characteristics
Manually builds
AuthenticationManager
fromHttpSecurity
Integrates custom
AuthenticationProvider
Adds
JwtAuthFilter
before standard filtersUses
SecurityFilterChain
withsecurityMatcher()
The
HttpSecurity
object used to buildAuthenticationManager
is the same instance being configured in theSecurityFilterChain
Example: AdventureTube Auth Service
@Configuration
@EnableWebSecurity
@AllArgsConstructor
public class AuthServiceConfig {
private final CustomUserDetailService customUserDetailService;
private final JwtAuthFilter jwtAuthFilter;
@Bean
@Order(1)
public SecurityFilterChain apiFilterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.securityMatcher("/auth/**")
.authorizeHttpRequests(auth -> auth
.requestMatchers(SecurityConstants.OPEN_ENDPOINTS).permitAll()
.anyRequest().hasRole("ADMIN")
)
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
@Bean
public AuthenticationManager customAuthenticationManager(HttpSecurity http) throws Exception {
// This http instance is the same as used in SecurityFilterChain
AuthenticationManagerBuilder builder = http.getSharedObject(AuthenticationManagerBuilder.class);
builder.authenticationProvider(customAuthenticationProvider());
return builder.build();
}
@Bean
public CustomAuthenticationProvider customAuthenticationProvider() {
return new CustomAuthenticationProvider(customUserDetailService, passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Summary Comparison
Feature | Default Setup | Custom Setup |
---|---|---|
AuthenticationManager | Auto-wired via AuthenticationConfiguration (default class) | Built manually using HttpSecurity |
Provider Type | DaoAuthenticationProvider | CustomAuthenticationProvider |
Flexibility | Low | High |
Filter Control | Minimal | Full control (e.g., JWT, token validation) |
Path-Specific Config | No | Yes (securityMatcher("/auth/**") ) |
Reuses HttpSecurity | Not applicable | Yes (same HttpSecurity used for filter chain and manager) |
Recommended For | Simple login flows | Token-based, microservice, advanced setups |
This transition from basic to advanced Spring Security configuration empowers developers to scale from rapid prototyping to secure, production-ready systems with fine-grained control.