Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add @Secured or @PreAuthorized annotations on an entire class

Tags:

It seems logical to secure an entire class of controllers rather than each method. Can I do this:

@Controller @Secured("ROLE_USER") public class accountPages {    //Controllers } 
like image 789
David Parks Avatar asked Oct 31 '10 08:10

David Parks


People also ask

Can I use @PreAuthorize at the class level?

In the above code example, the @PreAuthorize annotation is used at a class level and all methods in the class are affected by it. Only users in role “MANAGER” will be able to access the /managers/status/check web service endpoint.

What's the difference between @secured and @PreAuthorize in Spring Security?

@PreAuthorize is different, it is more powerful than @Secured . The older @Secured annotations did not allow expressions to be used. @Secured("ROLE_ADMIN") annotation is the same as @PreAuthorize ("hasRole('ROLE_ADMIN')") . The @Secured({"ROLE_USER","ROLE_ADMIN") is considered as ROLE_USER OR ROLE_ADMIN.

What is @secured annotation?

The Secured annotation is used to define a list of security configuration attributes for business methods. This annotation can be used as a Java 5 alternative to XML configuration.

What is the use of @PreAuthorize annotation?

The @PreAuthorize annotation checks the given expression before entering the method, whereas the @PostAuthorize annotation verifies it after the execution of the method and could alter the result.


1 Answers

from Spring Security 3 - PACKT Publishing

Be aware that the method-level security annotations can also be applied at the class level as well! Method-level annotations, if supplied, will always override annotations specified at the class level

like image 196
Aaron Saunders Avatar answered Nov 23 '22 07:11

Aaron Saunders