让GAE Shiro和Resteasy一起工作

时间:2013-04-12 12:44:58

标签: java google-app-engine jax-rs resteasy shiro

我想把GAE-Shiro和Resteasy放在一起。所以我尝试做一个快速端口。但是我收到了这个错误:

[ERROR] java.lang.RuntimeException: java.lang.InstantiationException: com.cilogi.shiro.guice.ServeModule
[ERROR]     at org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener.getModules(GuiceResteasyBootstrapServletContextListener.java:83)
[ERROR]     at org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener.contextInitialized(GuiceResteasyBootstrapServletContextListener.java:27)
[ERROR]     at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548)
[ERROR]     at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
[ERROR]     at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
[ERROR]     at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
[ERROR]     at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
[ERROR]     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
[ERROR]     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
[ERROR]     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
[ERROR]     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
[ERROR]     at org.mortbay.jetty.Server.doStart(Server.java:224)
[ERROR]     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
[ERROR]     at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:228)
[ERROR]     at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:255)
[ERROR]     at com.google.appengine.tools.development.AbstractServer.startup(AbstractServer.java:79)
[ERROR]     at com.google.appengine.tools.development.DevAppServerImpl$Servers.startup(DevAppServerImpl.java:451)
[ERROR]     at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:198)
[ERROR]     at com.google.appengine.tools.development.gwt.AppEngineLauncher.start(AppEngineLauncher.java:97)
[ERROR]     at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)
[ERROR]     at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)
[ERROR]     at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)
[ERROR]     at com.google.gwt.dev.DevMode.main(DevMode.java:311)
[ERROR] Caused by: java.lang.InstantiationException: com.cilogi.shiro.guice.ServeModule
[ERROR]     at java.lang.Class.newInstance0(Class.java:359)
[ERROR]     at java.lang.Class.newInstance(Class.java:327)
[ERROR]     at org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener.getModules(GuiceResteasyBootstrapServletContextListener.java:70)
[ERROR]     ... 22 more

这是我当前的web.xml配置

    <context-param>
        <param-name>resteasy.guice.modules</param-name>
<!--         <param-value>org.jboss.errai.ui.demo.server.MyServletModule</param-value> -->
        <param-value>com.cilogi.shiro.guice.ServeModule</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
        </listener-class>
    </listener>

    <context-param>  
        <param-name>resteasy.servlet.mapping.prefix</param-name>  
        <param-value>/</param-value>  
    </context-param>

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
       <param-name>user-base-url</param-name>
       <param-value>/user/admin</param-value>
    </context-param>

    <context-param>
        <param-name>static-base-url</param-name>
        <param-value></param-value>
    </context-param>


    <listener>
        <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>com.cilogi.shiro.guice.ServeContextListener</listener-class>
    </listener>


    <mime-mapping>
        <extension>manifest</extension>
        <mime-type>text/cache-manifest</mime-type>
    </mime-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list> 

1 个答案:

答案 0 :(得分:4)

我意识到这个问题已经差不多一年了,但我最近遇到了与Shiro(注意:不是GAE-Shiro)和RESTEasy类似的问题,并认为我会发布我在这里使用的解决方案,这样它可能会让其他人受益谁遇到这个特殊的问题。

问题是RESTEasy的GuiceResteasyBootstrapServletContextListener正在尝试创建一个需要传递参数的模块(在我的例子中,它是一个需要ShiroWebModule参数的ServletContext。 ..在你的情况下,看起来com.cilogi.shiro.guice.ServeModule需要传递一个String。但是,当RESTEasy尝试创建模块时,它会通过调用no-args构造函数来实现,然后失败,因为没有。

我决定使用的解决方案是扩展GuiceResteasyBootstrapServletContextListener并覆盖getModules(ServletContext ctx)方法,这样如果存在ServletContext的构造函数,则使用它,否则,使用默认的no-args构造函数。

例如:

public class MyBootstrapServletContextListener extends GuiceResteasyBootstrapServletContextListener {

    @Override
    protected List getModules(final ServletContext context) {
        final List result = new ArrayList();
        final String modulesString = context.getInitParameter("resteasy.guice.modules");

        if (modulesString != null) {
            final String[] moduleStrings = modulesString.trim().split(",");

            for (final String moduleString : moduleStrings) {
                try {
                    log.info("Found module: {}", moduleString);
                    final Class cls = Thread.currentThread().getContextClassLoader().loadClass(moduleString.trim());
                    final Module module = createModule(cls, context);
                    result.add(module);
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (InstantiationException e) {
                    throw new RuntimeException(e);
                } catch (IllegalArgumentException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return result;
    }

    private Module createModule(Class cls, ServletContext context) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Constructor constructor = null;

        try {
            constructor = cls.getConstructor(ServletContext.class);
        } catch (NoSuchMethodException e) {
            log.info("Class {} has no constructor that takes just a ServletContext parameter; defaulting to no-args constructor.", cls);
        }

        return constructor == null ? (Module) cls.newInstance() : (Module) constructor.newInstance(context);
    }

}

完成此操作后,我将web.xml更改为使用MyBootstrapServletContextListener而不是RESTEasy的GuiceResteasyBootstrapServletContextListener,并且RESTEasy不再有问题。您应该能够执行类似的操作,以允许RESTEasy将所需的String传递给com.cilogi.shiro.guice.ServeModule

再一次,我意识到你可能不再需要一个解决方案,但我在研究与Shiro的类似问题时遇到了这个问题,并希望帮助其他试图将RESTEasy和Shiro结合在他们的网络应用程序中的人。 / p>