Component Inspector
Click on any component in the diagram to explore its role, technical breakdown, and code examples.
Simulation Legend
Filter
Filter
TranslationFilter
Spring Security 6: Architecture & Principles
In an era where data breaches cost millions and erode user trust, securing applications isn’t optional — it’s existential. Security isn’t just about tools; it’s about principles. Spring Security isn’t just a framework — it’s a manifestation of these principles.
Defense in Depth
“Don’t rely on a single safeguard.”
- Layered Filters: A chain of security filters (e.g., auth, CSRF).
- Redundant Checks: Method-level security (`@PreAuthorize`) alongside URL rules.
Least Privilege
“Only grant necessary access.”
- RBAC: Restrict endpoints to specific roles.
- Scope-Based: Limit third-party app permissions in OAuth2.
Fail-Secure Defaults
“Deny access unless explicitly permitted.”
- All endpoints require auth unless whitelisted.
- Automatic CSRF protection for stateful flows.
Confidentiality & Integrity
“Protect data from eavesdropping and tampering.”
- HTTPS enforcement.
- Password hashing (e.g., `BCryptPasswordEncoder`).
- JWT signature validation.
Core Components Configuration
1. SecurityFilterChain
Defines the order and behavior of security filters applied to incoming requests. Each filter handles a specific task.
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults())
.addFilterBefore(
new CustomFilter(),
BasicAuthenticationFilter.class
);
return http.build();
}
2. AuthenticationManager & Provider
Coordinates the authentication process by delegating to one or more
AuthenticationProvider instances to validate credentials.
@Component
public class ApiKeyAuthProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication auth) {
String apiKey = (String) auth.getCredentials();
if (isValidApiKey(apiKey)) {
return new ApiKeyAuthenticationToken(
apiKey,
List.of(new SimpleGrantedAuthority("ROLE_USER"))
);
}
throw new BadCredentialsException("Invalid API Key");
}
@Override
public boolean supports(Class> authentication) {
return ApiKeyAuthenticationToken.class.isAssignableFrom(authentication);
}
}
3. UserDetailsService & PasswordEncoder
Loads user data from storage and validates hashes to ensure passwords are never stored in plaintext.
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) {
User user = userRepository.findByEmail(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
return new org.springframework.security.core.userdetails.User(
user.getEmail(),
user.getPassword(),
user.getRoles().stream()
.map(role -> new SimpleGrantedAuthority("ROLE_" + role))
.toList()
);
}
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12); // Strength 12 recommended
}