Let’s compares the default credential authentication process used by Spring Security and the custom credential authentication logic defined in the CustomAuthenticationProvider
of the auth-service
module.
βοΈ Default Credential Flow (Current Behavior)
By default, AdventureTube’s auth-service
uses Spring Securityβs built-in DaoAuthenticationProvider
through this configuration:
authenticationManagerBuilder
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
π Flow
Spring injects
DaoAuthenticationProvider
It uses your
CustomUserDetailService
to load user by emailIt uses
BCryptPasswordEncoder
to compare passwordsIf valid, it returns an authenticated
UsernamePasswordAuthenticationToken
β Pros
Simple and standard
No need to write extra logic
Well-supported by Spring ecosystem
β Limitations
No hook for logging, auditing, or pre/post validation
Limited flexibility to handle complex credential logic or multi-source auth
π οΈ Custom Credential Flow (CustomAuthenticationProvider)
You have defined a custom provider:
public class CustomAuthenticationProvider extends DaoAuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// Manually load user, validate password
return new UsernamePasswordAuthenticationToken(...);
}
}
π§© How It Works
Fully overrides
authenticate()
Manually loads user and verifies password
Can throw custom exceptions (e.g. logging failed attempts)
Can be extended to support external token-based or multi-factor logic
π§ͺ How to Activate
To enable it, modify your config:
@Bean
public AuthenticationManager customAuthenticationManager(HttpSecurity httpSecurity) throws Exception {
AuthenticationManagerBuilder builder = httpSecurity.getSharedObject(AuthenticationManagerBuilder.class);
builder.authenticationProvider(customAuthenticationProvider());
return builder.build();
}
@Bean
public CustomAuthenticationProvider customAuthenticationProvider() {
CustomAuthenticationProvider provider = new CustomAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(passwordEncoder());
return provider;
}
β Pros
Full control over auth process
Easy to extend for non-standard login types
Better for audit trails, dynamic credential checks
β Considerations
Slightly more boilerplate
Must manually handle exceptions correctly
β Summary Table
Feature | Default (DaoAuthenticationProvider ) | Custom (CustomAuthenticationProvider ) |
---|---|---|
Built-in integration | β Yes | β No (manual setup required) |
Uses UserDetailsService | β Yes | β Yes |
Uses PasswordEncoder | β Yes | β Yes |
Custom pre-checks/logging | β No | β Yes |
JWT/Token logic extensible | β Limited | β Full control |
Use case complexity | β Simple login | β Complex login, auditing, multi-auth |
You can safely use the default flow for typical use cases, but the custom provider gives you full flexibility when needed.