lundi 8 octobre 2018

Spring Security @Secured block reflection

I'm facing a new problem today in my Spring Boot 1.5.16 project.

I'm creating my own annotations and I want to use reflection to retrieve objects (Beans in current context) with my annotations.

Everything is ok when I don't have security on my beans, but as soon as I add @Secured or @PreAuthorize on my bean with my annotation, I can't retrieve it anymore.

Here are some pieces of code :

My annotation :

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface MyNewAnnotation {
    String param();
}

My Object using the annotation :

@MyNewAnnotation(param="Value")
public class MyObject {
  ...

    @Secured("ROLE_XXX")
    public String handleSomething(SomeAction action) {
        ...
    }
}

Code used elsewhere in another bean to retrieve the information about the annotation :

//Here context is the ApplicationContext tha I get with @Autowired
Collection<Object> containers = context.getBeansWithAnnotation(MyNewAnnotation.class).values();

for (Object container : containers) {
        CommandHandler annotation = container.getClass().getAnnotation(MyNewAnnotation.class);
        System.out.println(container.getClass().getName());
        System.out.println(annotation);
    }

Container is my object, but annotation is null ! But if I remove the @Secured annotation, I get an object.

Another thing : if I try to loop via reflection to get all MyObject methods, I don't retrieve handleSomething.

I think i must be doing something in a wrong way, maybe it is possible to get all the informations I need differently ?

Julien





Aucun commentaire:

Enregistrer un commentaire