Method Level Security#
Method Level Security In Spring Security#
- In the section Configure Authorities, we applied authorization rules on the API paths/URLs using spring security configuration. However, the
method levelsecurity allows to apply the authorization rules at any layer of an application like in service layer or repository layer etc.Method levelsecurity can be enabled using the annotation@EnableGlobalMethodSecurityon the configuration class. Method levelsecurity will also helps authorization rules even in the non-web applications where we will not have any endpoints.-
Method levelsecurity provides the below approaches to apply the authorization rules and execution your business logic.- Invocation authorization`: Validate of someone can invoke a method or not based on their roles/authorities.
Filtering authorization: Validates what a method can receive through its parameters and what the invoker can receive back from the post business logic execution.
-
Spring security will use the aspects from the
AOPmodule and have the interceptors in between the method invocation to apply the authorization rules configured. Method levelsecurity offers below 3 differences styles for configuring the authorization rules on top of the methods.- The
prePostEnabledproperty enables Spring Security@PreAuthorize&@PostAuthorizeannotations. - The
securedEnabledproperty enables@Securedannotation. - The
jsr250Enabledproperty enables@RoleAllowedannotation.
- The
| MethodSecurityConfig.java | |
|---|---|
1 2 3 4 5 6 7 | |
@Securedand@RoleAllowedare less powerful compared to@PreAuthorizeand@PostAuthorize