Spring Security is a powerful and highly customizable authentication and access-control framework for Java-based applications. One feature of Spring Security is the ability to secure methods at the method level, also known as method-level security. Method-level security allows developers to specify security constraints on specific methods within a class, rather than applying security constraints to the entire class or application. This allows for more fine-grained control over access to specific parts of the application. To implement method-level security in Spring Security, developers can use the @Secured and @PreAuthorize annotations.
@Secured Annotation
The @Secured annotation is used to specify a list of roles that are allowed to access a particular method.
For example:
Java
@Secured ( "ROLE_ADMIN" )
public void deleteUser( int userId) {
}
|
Only users with the ROLE_ADMIN role will be able to access the deleteUser() method.
@PreAuthorize Annotation
The @PreAuthorize annotation is used to specify more complex security constraints using SpEL (Spring Expression Language).
For example:
Java
@PreAuthorize ( "hasRole('ROLE_ADMIN') or (hasRole('ROLE_USER') and #userId == principal.userId)" )
public void updateUser( int userId) {
}
|
This annotation allows only users with the ROLE_ADMIN role or users with the ROLE_USER role and the same userId as the principal (the currently authenticated user) to access the updateUser() method. By default, method-level security is not enabled in Spring Security. To enable it, developers need to add the <global-method-security> element to the Spring Security configuration file and set the pre-post-annotations attribute to “enabled”.
In summary, method-level security in Spring Security allows developers to apply security constraints to specific methods within a class, rather than applying them to the entire class or application. This feature is implemented using the @Secured and @PreAuthorize annotations and is more fine-grained control over access to specific parts of the application.
Here is an example of how to use Spring Security method-level security in a Spring Boot application:
Add the Spring Security starter to your project’s build file (pom.xml for Maven, build.gradle for Gradle)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Create a Spring Security configuration class that enables method-level security by adding the @EnableGlobalMethodSecurity annotation.
Java
@Configuration
@EnableGlobalMethodSecurity (prePostEnabled = true )
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
|
Annotate the methods that you want to secure with the @PreAuthorize annotation and specify the security constraints using Spring Expression Language (SpEL)
Java
@Service
public class UserService {
@PreAuthorize ( "hasRole('ROLE_ADMIN')" )
public void deleteUser( int userId) {
}
@PreAuthorize ( "hasRole('ROLE_USER') and #userId == principal.userId" )
public void updateUser( int userId) {
}
}
|
Similarly, in this example as well, the deleteUser() method can only be accessed by users with the ‘ROLE_ADMIN’ role, while the updateUser() method can only be accessed by users with the ‘ROLE_USER’ role.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 Feb, 2023
Like Article
Save Article