Learn. Practice. Feel prepared.
Spring Security Objective Questions and Answers - Set 2
This Spring Security quiz contains carefully curated objective questions with correct answers and clear explanations. It is designed for developers, security learners, and interview preparation, covering authentication, authorization, encryption, JWT, OAuth2, and advanced Spring Security concepts. This is part 2 of the series.
Practice Spring Security MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
16 Which of the following filters runs before UsernamePasswordAuthenticationFilter in Spring Security filter chain? High
Correct answer:
SecurityContextPersistenceFilter
SecurityContextPersistenceFilter runs early in the filter chain to load the SecurityContext from the session before authentication occurs.
17 What does the method hasAuthority('ADMIN') check in Spring Security? Medium
Correct answer:
Whether the user has authority ADMIN
hasAuthority('ADMIN') checks if the user has the specified GrantedAuthority. Roles usually have a 'ROLE_' prefix when converted to authorities.
18 Which of the following statements about Remember-Me functionality is correct? Medium
Correct answer:
It uses a persistent token or cookie to remember authenticated users
Remember-Me in Spring Security allows users to remain authenticated across sessions using cookies or persistent tokens.
19 Which authentication manager implementation allows multiple AuthenticationProviders in Spring Security? High
Correct answer:
ProviderManager
ProviderManager manages a chain of AuthenticationProviders, attempting authentication with each until one succeeds or all fail.
20 What is the default session creation policy in Spring Security? Medium
Correct answer:
IfRequired
Spring Security creates a session only if required by default (SessionCreationPolicy.IF_REQUIRED).
21 Which filter validates JWT tokens for REST APIs in Spring Security? High
Correct answer:
OncePerRequestFilter
Custom JWT validation filters often extend OncePerRequestFilter to validate tokens once per request.
22 What is the purpose of SecurityContextHolder in a multi-threaded environment? High
Correct answer:
To store authentication in a thread-safe manner
SecurityContextHolder uses ThreadLocal by default to store the SecurityContext per thread, ensuring thread safety.
23 Which of the following statements about @PostAuthorize is correct? High
Correct answer:
It is evaluated after method execution
@PostAuthorize is used for post-invocation access control. You can access the method’s return value to make authorization decisions.
24 Which encryption algorithm is recommended for storing passwords in Spring Security? Medium
Correct answer:
BCrypt
BCrypt is recommended because it is slow (computationally expensive) and includes a random salt, making password hashes more secure.
25 Which Spring Security component handles logout requests automatically? Medium
Correct answer:
LogoutFilter
LogoutFilter intercepts logout requests and invokes configured LogoutHandlers to clear authentication and invalidate sessions.
26 In a stateless REST API using Spring Security, which SessionCreationPolicy is recommended? High
Correct answer:
STATELESS
For stateless REST APIs, STATELESS ensures that Spring Security does not create or use any HTTP session.
27 Which of the following is true for OAuth2 Authorization Server in Spring Security? High
Correct answer:
It issues access tokens to clients
The Authorization Server issues access tokens (JWT or opaque) to clients after validating user credentials and client details.
28 Which of the following is true about SecurityContextPersistenceFilter? High
Correct answer:
It loads and stores SecurityContext from session
SecurityContextPersistenceFilter ensures the SecurityContext is loaded at the start of request and stored at the end, typically using the HTTP session.
29 Which annotation in Spring Security restricts method access to users with specific roles? Medium
Correct answer:
@Secured
@Secured("ROLE_ADMIN") restricts access to methods only to users with specified roles. It is simpler than @PreAuthorize but less flexible.
30 What is the default behavior of BasicAuthenticationFilter? Medium
Correct answer:
Processes HTTP Basic authentication headers
BasicAuthenticationFilter processes HTTP Basic authentication credentials sent in the Authorization header and authenticates the user.