Spring Security Architecture Last Updated : 12 Sep, 2025 Comments Improve Suggest changes 5 Likes Like Report Spring Security is a framework that provides authentication, authorization and protection against common attacks in Java applications. It is the de facto standard for securing Spring-based applications, offering flexible integration with modern security mechanisms such as JWT, OAuth2, LDAP and database-backed authentication.Spring-SecurityCore Components of Spring Security Architecture1. Security Filter ChainActs as the entry point for all incoming requests.Every incoming request passes through a chain of filters (e.g., UsernamePasswordAuthenticationFilter, BasicAuthenticationFilter).Filters handle authentication, authorization, CSRF protection and session management.Ensures modular and customizable security logic.Example: Java @Configuration public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .csrf(csrf -> csrf.disable()) // Disable CSRF for APIs .authorizeHttpRequests(auth -> auth .requestMatchers("/public/**").permitAll() .anyRequest().authenticated() ) .httpBasic(); // Basic Authentication return http.build(); } } 2. Authentication ManagerThe core component responsible for user authentication.Delegates authentication requests to one or more Authentication Providers.Implements the Strategy pattern, meaning multiple authentication mechanisms (DB, LDAP, JWT, OAuth2) can work together.Example: Java @Configuration public class AuthManagerConfig { @Bean public AuthenticationManager authenticationManager( AuthenticationConfiguration configuration) throws Exception { return configuration.getAuthenticationManager(); } } 3. Authentication ProvidersAuthentication Providers are the actual components that validate user credentials.Examples:DaoAuthenticationProvider: Uses database via UserDetailsService + PasswordEncoder.JwtAuthenticationProvider: Validates JWT tokens.Example: Java @Configuration public class ProviderConfig { @Bean public DaoAuthenticationProvider authenticationProvider( UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) { DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); provider.setUserDetailsService(userDetailsService); provider.setPasswordEncoder(passwordEncoder); return provider; } } 4. UserDetailsServiceLoads user-specific data (username, password, roles) from a data source like a database.Returns a UserDetails object.Used primarily by providers like DaoAuthenticationProvider.Example: Java @Configuration public class UserConfig { @Bean public UserDetailsService userDetailsService(PasswordEncoder encoder) { return new InMemoryUserDetailsManager( User.withUsername("john") .password(encoder.encode("password")) .roles("USER") .build(), User.withUsername("admin") .password(encoder.encode("admin123")) .roles("ADMIN") .build() ); } } 5. Password EncoderEnsures secure password storage and validation.Encodes raw passwords into secure hashes before saving/validation.Example: Java @Configuration public class PasswordConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); // Strong hashing } } 6. SecurityContextHolderStores the SecurityContext for the current request/thread. And holds the Authentication object, which contains:Principal: Represents the logged-in user (username or user object).Authorities: Roles/permissions granted to the user.Example: Java @RestController public class UserController { @GetMapping("/me") public String getCurrentUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); return "Logged in as: " + authentication.getName() + " | Roles: " + authentication.getAuthorities(); } } How It Works InternallyA client sends an HTTP Request.The request passes through the Security Filter Chain.The Authentication Manager delegates authentication to an appropriate Authentication Provider.The provider may use UserDetailsService and PasswordEncoder to validate credentials.Upon success, user details are stored in SecurityContextHolder.Authorization checks use the stored principal and authorities to allow or deny access.The processed request finally reaches the application’s controller and returns an HTTP Response. Create Quiz Comment A abhijitrathoreabsn516 Follow 5 Improve A abhijitrathoreabsn516 Follow 5 Improve Article Tags : Springboot Technical Scripter 2022 Java-Spring-Security Explore Spring Boot Basics and PrerequisitesIntroduction to Spring Boot4 min readDifference between Spring and Spring Boot4 min readSpring - Understanding Inversion of Control with Example6 min readSpring - IoC Container2 min readBeanFactory vs ApplicationContext in Spring6 min readSpring Boot CoreSpring Boot - Architecture2 min readSpring Boot - Annotations5 min readSpring Boot Actuator5 min readHow to create a basic application in Java Spring Boot3 min readSpring Boot - Code Structure3 min readSpring Boot - Scheduling4 min readSpring Boot - Logging8 min readException Handling in Spring Boot8 min readSpring Boot with REST APISpring Boot - Introduction to RESTful Web Services3 min readSpring Boot - REST Example4 min readHow to Create a REST API using Java Spring Boot?4 min readHow to Make a Simple RestController in Spring Boot?2 min readJSON using Jackson in REST API Implementation with Spring Boot3 min readSpring Boot with Database and Data JPA Spring Boot with H2 Database6 min readSpring Boot - JDBC8 min readAdvantages of Spring Boot JDBC3 min readSpring Boot - CRUD Operations7 min readSpring Boot - MongoRepository with Example5 min readSpring Boot JpaRepository with Example5 min readSpring Boot - CrudRepository with Example5 min readSpring Boot with KafkaSpring Boot Kafka Producer Example3 min readSpring Boot Kafka Consumer Example3 min readSpring Boot | How to consume JSON messages using Apache Kafka3 min readSpring Boot | How to consume string messages using Apache Kafka3 min readSpring Boot | How to publish String messages on Apache Kafka2 min readSpring Boot | How to publish JSON messages on Apache Kafka4 min readSpring Boot with AOPSpring Boot - AOP(Aspect Oriented Programming)4 min readHow to Implement AOP in Spring Boot Application4 min readSpring Boot - Difference Between AOP and OOP3 min readSpring Boot - Difference Between AOP and AspectJ3 min readSpring Boot - Cache Provider6 min read Like