Learn. Practice. Feel prepared.
Spring Security Objective Questions and Answers
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.
Practice Spring Security MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
1 Which of the following is true about the default password storage in Spring Security 5? Medium
Correct answer:
Passwords are hashed using BCrypt
Spring Security 5 uses BCryptPasswordEncoder by default for password hashing, providing a secure one-way hash that includes a random salt.
2 What is the default HTTP method for CSRF protection in Spring Security? Easy
Correct answer:
POST
CSRF protection in Spring Security is primarily enabled for state-changing methods such as POST, PUT, DELETE, and PATCH. GET requests are not protected since they are considered safe.
3 Which annotation enables method-level security in Spring Security? Medium
Correct answer:
@EnableMethodSecurity
@EnableMethodSecurity enables method-level security annotations like @PreAuthorize and @PostAuthorize in Spring Security.
4 In Spring Security, what does the 'ROLE_' prefix signify? Medium
Correct answer:
It is optional and used to distinguish roles from other authorities
Spring Security uses the 'ROLE_' prefix by convention to differentiate roles from other authorities when using expressions like hasRole('ADMIN').
5 Which of the following is NOT a standard authentication mechanism supported by Spring Security? Medium
Correct answer:
Digest Auth
Digest Authentication is supported but rarely used; standard modern approaches include Form Login, Basic Auth, and Token-based (JWT) authentication.
6 What is the primary purpose of the AuthenticationManager in Spring Security? Medium
Correct answer:
To authenticate user credentials
AuthenticationManager is responsible for validating user credentials and returning an authenticated Authentication object if successful.
7 Which interface in Spring Security provides access to the current user’s authentication information? Medium
Correct answer:
SecurityContextHolder
SecurityContextHolder holds the SecurityContext, which contains the Authentication object representing the currently authenticated user.
8 Which of the following statements about JWT in Spring Security is correct? Medium
Correct answer:
JWTs are self-contained tokens containing user claims
JWT (JSON Web Token) is self-contained and carries user claims. It is signed to ensure integrity and optionally encrypted, but not stored on the server.
9 What does the @PreAuthorize annotation allow you to do? Medium
Correct answer:
Authorize access to methods based on SpEL expressions
@PreAuthorize allows method-level access control using SpEL expressions like @PreAuthorize("hasRole('ADMIN')").
10 In Spring Security, what is the default behavior when a user is unauthenticated and tries to access a protected resource? Medium
Correct answer:
Redirects to login page
By default, Spring Security redirects unauthenticated users to the login page when they attempt to access a protected resource.
11 Which PasswordEncoder implementation should be used for maximum compatibility with older Spring Security versions? Medium
Correct answer:
NoOpPasswordEncoder
NoOpPasswordEncoder stores passwords in plain text and ensures backward compatibility with older password storage, but it is insecure for production use.
12 Which Spring Security filter handles authentication of login credentials? Medium
Correct answer:
UsernamePasswordAuthenticationFilter
UsernamePasswordAuthenticationFilter processes login requests and delegates authentication to the AuthenticationManager.
13 How can you disable CSRF protection for a specific endpoint in Spring Security? Medium
Correct answer:
By configuring HttpSecurity.csrf().ignoringAntMatchers("/endpoint")
Spring Security allows selective disabling of CSRF using ignoringAntMatchers() in the HttpSecurity configuration.
14 Which method in UserDetailsService loads a user by username? Medium
Correct answer:
loadUserByUsername()
loadUserByUsername(String username) is the only method in UserDetailsService to fetch user details for authentication.
15 Which of the following is true about OAuth2 Resource Server in Spring Security? High
Correct answer:
It validates incoming JWT tokens
A Resource Server validates JWT tokens sent by clients and ensures the user is authorized to access protected resources.