Spring Certified Professional 2023 · Free Practice Question Easy
Question 21
To enable annotation-based security, we need to add the _______ annotation on any @Configuration class.
-
A
@SpringSecurity -
B
@EnableMethodSecurity -
C
@EnableApplicationSecurity -
D
@SecuredApplication
Reveal correct answer
Correct answer: B
Explanation
@EnableMethodSecurity
To enable annotation-based security, we need to add the @EnableMethodSecurity annotation on any @Configuration class. This is how our configuration class will look like:
- package com.javadevjournal.config;
- import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity ;
- /**
- * EnableMethodSecurity to allow method level Spring security annotation for our application
- */
- @EnableMethodSecurity (
- securedEnabled = true,
- jsr250Enabled = true,
- prePostEnabled = true
- )
- public class MethodSecurityConfig {
- //default configuration class
- }
Let’s look at a few important parameters of the @EnableMethodSecurity annotation
securedEnabled– Determine if the@Securityannotation should be enabled.jsr250Enabled– Allow us to use JSR250-based annotation (e.g.@RoleAllowed).prePostEnabled– Enable Spring’s pre/post annotations.
https://www.javadevjournal.com/spring-security/spring-method-security/
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
