Spring Security
Spring Security is a framework focused on providing authentication, authorization, and other security features for Java applications.
- Comprehensive support for authentication and authorization.
- Protection against common vulnerabilities (e.g., CSRF, session fixation).
- Integration with OAuth2 for Single Sign-On (SSO).
- Security for REST APIs, including stateless authentication mechanisms like JWT.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Authentication vs. Authorization
Authentication: The process of verifying the identity of a user.
- Example: Logging in with a username and password.
Authorization: Determines whether an authenticated user has permission to access a resource.
-
Example: Granting access to the admin dashboard only to users with the
ADMIN
role.
Core Components
3.1 SecurityContext and SecurityContextHolder
-
SecurityContext
: Holds security information (e.g., authenticated user details). -
SecurityContextHolder
: A static holder for theSecurityContext
. Used to access security details programmatically.
3.2 Authentication Object
- Represents the principal (user) and their credentials.
- Common implementations:
UsernamePasswordAuthenticationToken
,OAuth2Authentication
.
5. Key Concepts
5.1 Filters in Spring Security
The security filter chain intercepts requests and applies security logic.
-
UsernamePasswordAuthenticationFilter
: Handles form-based login. -
OncePerRequestFilter
: Custom filters can extend this class.
5.2 Password Encoding
Always encode passwords before storing them.
Example using BCryptPasswordEncoder
:
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
6. Common Use Cases
6.1 Basic Authentication
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
6.2 JWT Authentication
- Generate a JWT upon successful login.
- Validate the token for each request.
Key classes:
JwtAuthenticationFilter
JwtTokenProvider
7. Annotations in Spring Security
7.1 @PreAuthorize
and @PostAuthorize
Used for method-level security:
@PreAuthorize("hasRole('ADMIN')")
public void adminMethod() {
// Code here
}
7.2 @Secured
An alternative to @PreAuthorize
:
@Secured("ROLE_ADMIN")
public void adminMethod() {
// Code here
}
8. Handling CSRF
CSRF (Cross-Site Request Forgery) protection is enabled by default.
To disable:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
}
9. OAuth2 Integration
Use Spring Security’s OAuth2 support for SSO.
Example configuration:
spring.security.oauth2.client.registration.google.client-id=your-client-id
spring.security.oauth2.client.registration.google.client-secret=your-client-secret
10. Common Interview Questions
- Explain the difference between authentication and authorization.
- How does Spring Security handle CSRF?
- How do you secure a REST API with JWT in Spring Boot?
- What is the role of
SecurityContextHolder
? - Explain the purpose of
UserDetailsService
. - How do you implement role-based access control in Spring Security?
- What are some best practices for securing a Spring Boot application?
11. Best Practices
- Always encode passwords using
PasswordEncoder
. - Use HTTPS to encrypt communication.
- Avoid exposing sensitive endpoints.
- Implement proper exception handling for security events.
- Use a security audit tool to identify vulnerabilities.