HttpSecurity, WebSecurity and AuthenticationManagerBuilder
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Securing web applications is a fundamental aspect of software development, particularly when dealing with sensitive data. Spring Security is one of the most robust frameworks for implementing authentication, authorization, and other security features in Java applications. Within Spring Security, HttpSecurity, WebSecurity, and AuthenticationManagerBuilder are key classes/interfaces that provide developers with tools for defining security configurations. This article explores these components in detail, providing technical explanations and examples.
HttpSecurity
HttpSecurity is a crucial part of configuring web-based security for specific HTTP requests. It allows developers to define which endpoints are secured and who can access them, leveraging a fluent API to set up configurations.
Core Features
- URL-Based Authorization: Determines access based on URL patterns.
- Form Login: Configurations related to login forms, managing login URL, success, and failure handlers.
- Logout Options: Configuring logout behavior.
- CSRF Protection: Cross-site request forgery protection.
- Session Management: Manages session creation, concurrency, and rules.
Example
In this example, any request to /admin/** requires the user to have an ADMIN role, while access to / and /home is open to all users.
WebSecurity
While HttpSecurity focuses on securing specific HTTP requests, WebSecurity provides a mechanism to configure global settings affecting all requests. It is generally used to set paths that should be completely ignored by Spring Security, bypassing its filters.
Core Features
- Security Filter Chain: Custom configuration of how requests are handled by Spring Security filters.
- Ignoring Requests: Define paths that should be excluded from security constraints.
Example
In this example, static resources like CSS, JavaScript, and images are excluded from security filtering, allowing public access.
AuthenticationManagerBuilder
AuthenticationManagerBuilder is used for in-memory, JDBC, or LDAP-based authentication. It configures global authentication details for the application.
Core Features
- In-Memory Authentication: Simplest way of managing static list of users, often used for testing purposes.
- JDBC Authentication: Retrieves user details from a database.
- LDAP Authentication: Integrates with external LDAP servers for user data.
Example
In this example, two users are defined in-memory: a regular user and an administrator, each with their respective roles.
Comparison Table
| Component | Purpose | Key Features | Common Use Cases |
| HttpSecurity | Secure specific HTTP requests | URL-based authorization, form login, CSRF protection, session management | Configuring page-specific access and authentication |
| WebSecurity | Global configurations for all requests | Security filter chain, ignoring requests | Excluding static resources from security filtering |
| AuthenticationManagerBuilder | Set up authentication globally | In-memory, JDBC, LDAP authentication | Defining global authentication strategies |
Summary
Understanding the roles of HttpSecurity, WebSecurity, and AuthenticationManagerBuilder is crucial for effectively using Spring Security. Each component plays a specific role in defining the security landscape of an application. By setting up appropriate configurations for each, developers can ensure robust security and smooth authentication and authorization processes across their applications.
Related reading
- Identical messages committed during a network partition
- ImageMagick security policy 'PDF' blocking conversion
- ImagePullBackOff unauthorized authentication required
- ImagePullSecrets GCR
- HttpServletRequest to complete URL
- httptrace endpoint of Spring Boot Actuator doesn't exist anymore with Spring Boot 2.2.0
- Implement exclusive access to an EFS/NFS directory
- Implementing Licencing mechanism for a Software

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.