0

I got into a really strange problem. I have classes

public class A extends ...{

}

public class B extends A{

    @Override
    @PreAuthorize(...)
    public String doMagic(){
        String v = super.doMagic();
        doSomeOtherThings();
        ...
        return v;
    }
}

there were multiple classes extending A, so I refactored it into:

public class A extends ...{

    @Override
    public String doMagic(){
        String v = super.doMagic();
        doSomeOtherThings();
        ...
        return v;
    } 
}

public class B extends A{

    @Override
    @PreAuthorize(...)
    public String doMagic(){
        return super.doMagic();
    }
}

but now the interesting thing happened...

I have tests verifying access rights (based on PreAuthorize), which executes properly in IntelliJ, but fails when executed with maven. It looks like method from B is ignored and/or the annotation is not being processed. In the first version tests passes in both cases.

Any ideas what might have happened? This seems that the problem might be related to cglib..

and it works again in such case:

public class A extends ...{

    protecte String doMagic2(){
        String v = super.doMagic();
        doSomeOtherThings();
        ...
        return v;
    } 
}

public class B extends A{

    @Override
    @PreAuthorize(...)
    public String doMagic(){
        return doMagic2();
    }
}
zibi
  • 3,183
  • 3
  • 27
  • 47

1 Answers1

3

With the information you provide, this is what most likely happens: In order to create a proxy, cglib is creating a subclass at run time that overrides all methods that should be intercepted. This concept does however not allow for the interception of calls that explicitly hit a super class because the run time implementation of the proxy method is bypassed. I elaborated this here: Why does cglib not proxy super invocations?

Also, when a method is overridden by cglib, the annotations of the overridden method are not longer present. I assume that you are at some point proxing the @PreAuthorize-annotated method such that the annotation is not longer visible after applying the run time sub class that cglib creates for you.

If the results differ for Maven and IntelliJ, you have most likely an error in your setup since none of them should make up code.

Community
  • 1
  • 1
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192