Spring Framework FAQ: Top Questions
31. What is Spring Security and how does it work?
Spring Security is a powerful authentication and authorization framework for securing Java applications. It handles web security, method-level security, and integrates with OAuth2, LDAP, and more.
📘 Key Features:
- Authentication and authorization.
- CSRF, CORS, session management, and password encoding.
- Extensible and customizable architecture.
📥 Basic Configuration Example:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
🏆 Expected Output:
All endpoints require authentication, and users are redirected to a login form.
🛠️ Use Cases:
- Secure REST APIs and web apps.
- Implement role-based access control.