Search code examples
springspring-securityspring-roo

Spring annotation in Controller don't work? None of @Secure and @Async works


I use spring-roo to generate my project. I don't know these 2 things are related or not, but in Controller, none of the annotation @Async or @Secure works. For @Secure: I added <global-method-security secured-annotations="enabled"/> tag to applicationContext-security.xml and modified the pom.xml to meet dependency,

<dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-aspects</artifactId>
   <version>3.0.5.RELEASE</version>
</dependency>

above the method in the controller, I added @Secured("ROLE_ADMIN") but no matter what role everybody can access the method. Anything I missed to configure to make the @Secure Active?

For @Async: In applicationContext.xml, I added

<task:annotation-driven executor="asyncExecutor"/>
<task:executor id="asyncExecutor" pool-size="${executor.poolSize}"/>

in controller.java:

@Async
private void justWait20seconds() {
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

I expect this method will not block the main method, but it didn't. These 2 tag are all in my UserController.java, I don't know whether they are linked. Can anyone help?


Solution

  • It sounds like you don't understand how Spring contexts are organized in a Spring MVC application and are adding the annotation processing instructions to the wrong context. Do some reading on Spring contexts in Spring MVC. You can find another SO answer of mine that branches to other related answers at: Why DispatcherServlet creates another application context?