Which annotation is used to secure a method in Spring Security?

Which annotation is used to secure a method in Spring Security?

A) @Secured
B) @Authenticated
C) @Protected
D) @Restricted

Answer:

A) @Secured

Explanation:

The @Secured annotation in Spring Security is used to secure individual methods by specifying which roles are allowed to invoke them. This annotation can be applied to methods in your service or controller classes, ensuring that only users with the appropriate roles can access those methods.

For example:


@Secured("ROLE_ADMIN")
public void adminMethod() {
    // Method accessible only by users with the ADMIN role
}

In this example, the adminMethod() can only be accessed by users with the ROLE_ADMIN authority. Spring Security will check the user’s roles before allowing the method to execute, providing fine-grained control over method-level security.

Reference links:

https://www.javaguides.net/p/spring-security-tutorial.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top