Learn. Practice. Feel prepared.
Spring Security Objective Questions and Answers - Set 3
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 3 of the series.
Practice Spring Security MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
31 Which interface allows creating custom access decision logic in Spring Security? High
Correct answer:
AccessDecisionManager
AccessDecisionManager evaluates access decisions using voters to determine if the current user has permission to access a resource.
32 Which annotation is used to define role hierarchy in Spring Security? High
Correct answer:
@RoleHierarchy
@RoleHierarchy is used with RoleHierarchyImpl to define hierarchical relationships between roles, e.g., ADMIN > USER.
33 Which statement about ConcurrentSessionControlAuthenticationStrategy is true? High
Correct answer:
It prevents multiple sessions for the same user
ConcurrentSessionControlAuthenticationStrategy is used to limit concurrent sessions per user to prevent simultaneous logins beyond allowed limits.
34 Which of the following is true for OAuth2 Client in Spring Security? High
Correct answer:
It requests access tokens from Authorization Server
OAuth2 Client interacts with an Authorization Server to obtain access tokens and refresh tokens to access protected resources.
35 Which of the following is a correct way to store JWT in stateless REST APIs? Medium
Correct answer:
HttpOnly Cookie or Authorization Header
JWTs should be stored in HttpOnly cookies or Authorization headers for stateless REST APIs. Local storage is less secure due to XSS risk.
36 Which class in Spring Security is used to represent a user with username, password, and authorities? Medium
Correct answer:
UserDetails
UserDetails represents the user object containing username, password, and granted authorities, and is used by AuthenticationManager during authentication.
37 Which of the following statements about CSRF token repository is correct? High
Correct answer:
It generates and stores CSRF tokens for client requests
CsrfTokenRepository generates CSRF tokens and stores them in HTTP session, cookie, or custom storage for protecting state-changing requests.
38 What is the difference between @PreAuthorize and @Secured? High
Correct answer:
@PreAuthorize can use SpEL expressions, @Secured cannot
@PreAuthorize supports SpEL expressions for flexible access control, while @Secured only checks roles.
39 Which filter in Spring Security handles access-denied exceptions? High
Correct answer:
ExceptionTranslationFilter
ExceptionTranslationFilter intercepts AccessDeniedException and AuthenticationException, sending proper HTTP responses or redirecting to login/error pages.
40 Which component allows dynamic permission checks for URLs at runtime in Spring Security? High
Correct answer:
FilterInvocationSecurityMetadataSource
FilterInvocationSecurityMetadataSource provides URL-to-role mappings and can be customized for dynamic permission evaluation at runtime.
41 Which method in Authentication object returns the granted authorities? Medium
Correct answer:
getAuthorities()
getAuthorities() returns a collection of GrantedAuthority objects assigned to the authenticated user.
42 What is the purpose of DelegatingPasswordEncoder in Spring Security 5? High
Correct answer:
It allows multiple password encoding algorithms and selects by prefix
DelegatingPasswordEncoder allows multiple encoders, using a prefix in the stored password to select the appropriate encoding for verification.
43 Which statement is true about PreAuthenticatedAuthenticationProvider? High
Correct answer:
It authenticates users already authenticated by an external system
PreAuthenticatedAuthenticationProvider is used when authentication is already performed by an external system, e.g., SSO or reverse proxy.
44 Which of the following is true about SecurityFilterChain in Spring Security 5.7+? High
Correct answer:
It replaces WebSecurityConfigurerAdapter
SecurityFilterChain defines the sequence of security filters for request processing and replaces the now-deprecated WebSecurityConfigurerAdapter.
45 Which approach allows combining multiple AuthenticationProviders in Spring Security for a single application? High
Correct answer:
ProviderManager with a list of AuthenticationProviders
ProviderManager allows multiple AuthenticationProviders to be chained; authentication succeeds if any provider authenticates successfully.