WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Advanced · question 45 of 100

How can you achieve method-level security using Spring Security?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

Spring Security provides different ways to achieve method-level security in our applications. Method-level security controls the access level of a specific method execution in a class. It is useful when we want to secure individual methods based on different roles and permissions.

There are two ways in which we can achieve method-level security using Spring Security:

1. Using @Secured annotation: In this approach, we need to annotate the method with the @Secured annotation and specify the role or permission required to access that method. For example, let’s say we have a UserService class that has a method saveUser() that should be only accessible to users with the "ADMIN" role. We can achieve this by adding the @Secured("ROLE_ADMIN") annotation before the method declaration:

    @Service
    public class UserService {
        @Secured("ROLE_ADMIN")
        public void saveUser(User user) {
            //Some business logic here
        }
    }

2. Using AOP: In this approach, we define an aspect that intercepts the method execution and applies the security rules. We need to configure the aspect and define the security rules in the application context XML file. For example, let’s say we have a ProductService class that has a method updateProduct() that should be only accessible to users with the "PRODUCT_MANAGER" role. We can achieve this by configuring an aspect using AOP:

    <bean id="mySecurityAspect" class="org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect">
        <property name="securityInterceptor" ref="methodSecurityInterceptor"/>
    </bean>

    <bean id="methodSecurityInterceptor" class="org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor">
        <property name="authenticationManager" ref="authenticationManager"/>
        <property name="accessDecisionManager" ref="accessDecisionManager"/>
    </bean>

    <bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased">
        <constructor-arg>
            <list>
                <bean class="org.springframework.security.access.vote.RoleVoter"/>
                <bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
            </list>
        </constructor-arg>
    </bean>

    <bean id="productService" class="com.example.service.ProductService"/>
    
    <aop:config>
        <aop:aspect id="productServiceSecurity" ref="mySecurityAspect">
            <aop:pointcut expression="execution(* com.example.service.ProductService.updateProduct(..))"/>
            <aop:around method="org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect.aspectOf()"/>
        </aop:aspect>
    </aop:config>

In the above example, we defined an aspect that applies to the updateProduct() method of ProductService class. We also configured a method security interceptor to manage the access control to this method. Finally, we defined an access decision manager that checks whether the user has the required permission to access the method.

Overall, both approaches can be used to achieve method-level security with Spring Security. Using the @Secured annotation is ideal for simple and straightforward methods whereas AOP is better suited for larger and more complex applications.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics