Is it possible to enter code in spring using AOP annotations?

Is it possible to do something like the following:

public void doStuff(@RequirePrivilege("foo") User user) {
    // ...
}

code>

and does it work efficiently as if it were next?

public void doStuff(User user) {
    if(!user.hasPrivilege("foo"))
        throw new UserHasInsufficientPrivileges(); // this is a RuntimeException
    // ...
}

code>

I know that Spring has various kinds of AOP support, but the best I could find was AOP code that was annotated so that it would run before or after a specific method. I want to do the reverse and annotate the code that needs to be changed.

Ultimately, I could just do the above check inside the method, but the way of annotating makes the additional documentation, which makes it obvious that the user needs a certain privilege without having to synchronize the documentation with the code.

+3
source share
2

AspectJ , . , , .

Spring AspectJ, , , , , , , . , , , .

AspectJ In Action (http://www.manning.com/laddad2/), : :

* *(@RequestParam
(@Sensitive *))

*Any method with one parameter marked with the @RequestParam annotations and the parameter’s type is marked with the @Sensitive annotation.*

void create(@RequestParam
MedicalRecord mr), assuming
MedicalRecord carries the
@Sensitive annotation.
+1

, " " Spring AOP, Spring Security . AspectJ.

+1

Source: https://habr.com/ru/post/1710816/


All Articles