0

I was trying to figure out a way to get list of all preauthorize annotations from a spring container

Assume I have something like below.

    @PreAuthorize("hasPermission(null, 'opetussuunnitelma', 'LUONTI1')")
@PreAuthorize("hasPermission(null, 'opetussuunnitelma', 'LUONTI2')")
@PreAuthorize("hasPermission(null, 'opetussuunnitelma', 'LUONTI3')")

> I need to get that string(hasPermission(null, 'opetussuunnitelma',
> 'LUONTI2')) or at least List<Map> (x=null,y='opetussuunnitelma',
> z='LUONTI1')

Is there any way I could accomplish this, as I have n number of such annotations and I need to parse all those annotation strings and do something with that.

Aadam
  • 1,521
  • 9
  • 30
  • 60
  • "Parse and do something" with the annotation values is not very descriptive. What do you want to do with the parsed strings? When do you want to do that? Would it be okay to intercept each annotated method before it is being executed? Then use Spring AOP or AspectJ. Or do you need all the annotations right after application start-up? If so, for what reason (I cannot think of one)? Describe the problem, not the solution you have in mind. Your question suffers from the [XY problem](https://meta.stackexchange.com/a/66378/309898). – kriegaex Oct 08 '19 at 01:57

1 Answers1

1

In order to get all your app's classes loaded in JVM, you can try the Reflection library, get the loaded beans from Spring application context or manually add them to a static list. That said, there is no definite and proper way of achieving this.

To get the annotations, use Java reflection.

More info: https://www.geeksforgeeks.org/method-class-getannotation-method-in-java/

public void handleAnnotations(Class c) {
    try { 
        Method[] methods = c.getMethods(); 

        for (Method method : methods) { 
            PreAuthorize[] annotations = c.getAnnotationsByType(PreAuthorize.class);
            // handle annotations
        } 

    } 
    catch (Exception e) { 
        // handle exception
    } 
}
Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67
  • Ok, I did that, I was able to get string value of annotation. any how, this cannot be executed in runtime, I think I need to get list of all classnames, then extract method names and use above code to get annotation string, but there would be no source on server I have to give the path of class files inside a war? – Aadam Oct 07 '19 at 10:26
  • You can execute this at runtime. Also no source is needed, just bytecode. – Horatiu Jeflea Oct 07 '19 at 10:36
  • 1
    You just copied and pasted code from an external web site, not even adjusting it to the author's question. There is no `getCustomAnnotation` method anywhere in his code. Neither is there any reference to `@PreAuthorize` in your answer. I wonder why he accepted it. You also failed to answer the question how he should find all the annotations on all methods in his project. – kriegaex Oct 08 '19 at 02:02
  • @HoratiuJeflea, I do not understand your comment. Can you rephrase or explain, please? – kriegaex Oct 08 '19 at 05:13
  • @Aadam the “bytecode only” comment is available for fetching annotations only, not for getting all class names – Horatiu Jeflea Oct 08 '19 at 05:32
  • @kriegaex I accepted cause that could work out, even though I have to code it in the way I can full fill the requirement, it worked like a hint, anything better than that,, please ans the question, will sure accept a better answer if provided, thank you guys. :) – Aadam Oct 09 '19 at 15:20
  • edited the answer meanwhile :) I misread a part of the question at first – Horatiu Jeflea Oct 09 '19 at 15:21
  • @Aadam, before I can suggest another approach you first would have to answer my questions above. – kriegaex Oct 10 '19 at 06:07
  • @kriegaec if you understand my question please answer for that question. Else let it be above solution also suffice. If you have better answer for my actual question you are free to answer and I will surely accept it. Thank you – Aadam Oct 10 '19 at 06:12