user3408657
user3408657

Reputation: 189

Spring AOP not working

I am trying to create some annotations which check permissions from the securitycontext before accessing some protected resources. I wrote a sample code very similar to what I would want to implement, however when I invoke SomethingProtected(), it seems like the @Before part of the aspect never actually gets triggered. Any help would be appreciated.

I have:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NeedsPermissions {
    boolean read() default true;
}

and

@Aspect
public class NeedsPermissionsAspect {
    @Before("execution(* *.*(..)) && @annotation(NeedsPermissions)")
    public void CheckPermissions(JoinPoint pjp, NeedsPermissions needsPermissions) throws Throwable {
        System.out.println("aspect");
        if (needsPermissions.read() == true) {
            SecurityContext securityContext = SecurityContext.getSecurityContext();
            MyUser user = securityContext.getUser();
            if (!user.read){
                throw new Exception("Not Allowed");
            }
        }
    }
}

and

@Configuration
@EnableAspectJAutoProxy
public class NeedsPermissionsConfig {
}

and

public class ProtectedResource {

    @NeedsPermissions
    public void SomethingProtected(){
        System.out.println("Something protected");
    }
}

and

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <aop:aspectj-autoproxy/>



    <!-- Aspect -->
    <bean id="needsPermissionsAspect" class="NeedsPermissionsAspect">
        <!-- configure properties of aspect here as normal -->
    </bean>

</beans>

Upvotes: 1

Views: 1817

Answers (2)

Laura Liparulo
Laura Liparulo

Reputation: 2897

In my case I can tell you that with Spring 3.X the annotation in the configuration is not necessary. It's enough to have the dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

This way you can use the AspectJ annotations:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

You need to annotate the class with both @Aspect and @Component.+

Also make sure that your Pointcut is aiming at the right position in the package, otherwise you won't see anything in the logs, as the pointcut won't find the methods. For example, it the method is found under "com.demo.messaging.MyClass.myMethod" you need something like

@Pointcut("execution(* com.demo.messaging.*.*(..))")

Upvotes: 0

Tiago Medici
Tiago Medici

Reputation: 2194

try to change you annotation with this

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AppConfig {

}

Upvotes: 3

Related Questions