ClassFormatError when using @Secured and @RequestMapping for the same method

I got a weird error when I try to use @Secured

or @PreAuthorize

in a method @Controller

already annotated with @RequestMapping

. This results in an error when trying to start the application (using spring STS 2.8.1 with vFabric 2.6.1 on win 7 32 bits, both with spring and spring security 3.1):

Caused by: java.lang.ClassFormatError: Duplicate method name&signature in class file com/dnp/web/controllers/HomeController
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2820)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1150)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:417)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1283)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1254)
    ... 43 more

      

And the controller code:

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @PreAuthorize("hasRole('ROLE_USER')")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is "+ locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "home";
    }
}

      

I don’t know why this error occurs and didn’t find anything on the internet. In addition, protected annotations at the service level work seamlessly.

PD: The full application trace is here: http://pastebin.com/raw.php?i=VxdYPDXL and the main security context and servlet sections are: http://pastebin.com/cva5VgkH

+3


source to share


3 answers


It looks like the problem is caused by some strange conflict between Spring AOP and AspectJ compilation in time with the same methods: I use aspect to advise controller method calls and it has been compiled in controllers. So the solution was to just turn off AspectJ compilation and now it works without issue.



0


source


@rabusmar I got this link from SpringForum http://forum.springsource.org/showthread.php?109850-RequestMapping-and-PreAuthorize-not-compatible

It looks like PreAuthorize and query mapping are incompatible. They gave the solution at the end of the page. Just try it.



Also, a ClassFormatError is thrown when a class already processed by one annotation is processed again by another annotation, in your case the HomeController may have already been processed by PreAuthorize and again using RequestMapping or vice versa, and this is the reason you get a ClassFormatError.

You can try to find a solution in this spring forum link I pointed out above.

0


source


I came across a similar ClassFormatError in some groovy code. But the error message does not indicate which method name & signature was duplicated.

I found the error by running the following command:

javap -private -c io.cloudsoft.mapr.m3.MasterNodeImpl | grep -A 1 '^$' | sort | uniq -c | less

      

It finds all the method signatures here (always preceded by a blank line in my javap output) and then counts the unique occurrences so I can see which method signature is duplicated. I hope others do a great job with this.

(For those wondering, my problem was groovy's weird behavior @InheritConstructors who somehow added a no-arg constructor twice).

0


source







All Articles