Spring Security Interview Questions

 

1. What is Spring Security?

Answer: Spring Security is a framework that provides comprehensive security services for Java applications. It handles authentication (verifying who you are) and authorization (verifying what you are allowed to do) and is often used to secure web applications, REST APIs, and microservices.

2. What is the difference between authentication and authorization?

Answer:

  • Authentication: The process of verifying the identity of a user or system. For example, logging in with a username and password.
  • Authorization: The process of determining whether a user or system has permission to access a resource or perform an action.

3. How does Spring Security handle authentication?

Answer: Spring Security handles authentication by using the AuthenticationManager to authenticate a user based on credentials provided (e.g., username and password). It then creates an Authentication object which is stored in the SecurityContext.

4. What is the role of SecurityContextHolder?

Answer: SecurityContextHolder is a class that holds the SecurityContext associated with the current thread of execution. The SecurityContext contains details of the authenticated user, such as their Authentication object.

5. Explain how Spring Security handles session management.

Answer: Spring Security manages sessions through its SessionManagementConfigurer. It can configure session creation policies, such as stateless sessions (no sessions created) or stateful sessions. It also provides mechanisms to handle session fixation, concurrent session control, and session invalidation.

6. What is the purpose of WebSecurityConfigurerAdapter?

Answer: WebSecurityConfigurerAdapter is a base class used to configure Spring Security. By extending this class, developers can override methods to customize security settings, such as defining authentication mechanisms, specifying URL access rules, and configuring security filters.

7. How do you configure method-level security in Spring Security?

Answer: Method-level security is configured using annotations such as @PreAuthorize, @Secured, or @RolesAllowed on methods in your service classes. You also need to enable method security by adding @EnableGlobalMethodSecurity to a configuration class.

8. What is a UserDetailsService and how is it used?

Answer: UserDetailsService is an interface used to load user-specific data. It provides a method loadUserByUsername(String username) that retrieves user details from a data source. Spring Security uses it to authenticate users and populate the Authentication object.

9. What is the difference between UsernamePasswordAuthenticationFilter and BasicAuthenticationFilter?

Answer:

  • UsernamePasswordAuthenticationFilter: Handles authentication for username and password-based login, usually through form submissions.
  • BasicAuthenticationFilter: Handles HTTP Basic authentication, where credentials are sent in the Authorization header in base64-encoded format.

10. What is OAuth2 and how does Spring Security support it?

Answer: OAuth2 is an authorization framework that allows third-party applications to access user resources without exposing their credentials. Spring Security provides extensive support for OAuth2, including OAuth2 login, token-based authentication, and client credentials flow.

11. Explain the concept of CSRF protection in Spring Security.

Answer: CSRF (Cross-Site Request Forgery) protection prevents unauthorized commands being transmitted from a user that the web application trusts. Spring Security includes CSRF protection by default, adding a CSRF token to forms and verifying it on form submissions.

12. What is a SecurityFilterChain?

Answer: SecurityFilterChain is a component of Spring Security that defines a chain of filters used to process incoming HTTP requests. Each filter in the chain performs a specific security-related task, such as authentication, authorization, or logging.

13. How do you implement custom authentication in Spring Security?

Answer: To implement custom authentication, you can create a custom AuthenticationProvider that contains your authentication logic. Register this custom provider in the AuthenticationManager to handle the authentication process.

14. What are some common security vulnerabilities that Spring Security addresses?

Answer: Spring Security helps protect against common security vulnerabilities such as:

  • Cross-Site Scripting (XSS)
  • Cross-Site Request Forgery (CSRF)
  • Session Fixation
  • Clickjacking
  • Insecure Direct Object References

15. Explain the @PreAuthorize annotation.

Answer: The @PreAuthorize annotation is used to specify authorization rules at the method level. It allows you to use Spring Expression Language (SpEL) to define complex access control expressions based on user roles or other conditions.

Comments

Popular posts from this blog

Transform values with a stream

Collections Framework

Inspect a collection