java.lang.NoSuchFieldError:在Tomcat上部署shiro时出现subjectDAO异常
Posted
技术标签:
【中文标题】java.lang.NoSuchFieldError:在Tomcat上部署shiro时出现subjectDAO异常【英文标题】:java.lang.NoSuchFieldError: subjectDAO exception while deploying shiro on Tomcat 【发布时间】:2013-01-05 22:35:30 【问题描述】:我是 shiro 的新手,我正在尝试在 Tomcat 上运行我的 Web 应用程序。我在 web.xml 中的 shiro 特定配置如下:
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.IniShiroFilter</filter-class>
<!-- no init-param means load the INI config from classpath:shiro.ini -->
</filter>
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
我的 shiro.ini 如下所示:
[main]
credentialsMatcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName = SHA-256
credentialsMatcher.storedCredentialsHexEncoded = false
credentialsMatcher.hashIterations = 1024
localRealm = com.stratus.avance.multisite.services.ShiroMongoRealm
localRealm.credentialsMatcher = $credentialsMatcher
securityManager.realms = $localRealm
[urls]
/** = anon
我在部署应用程序时看到以下异常
INFO: Initializing Shiro environment
Jan 22, 2013 3:40:32 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.apache.shiro.web.env.EnvironmentLoaderListener
java.lang.NoSuchFieldError: subjectDAO
at org.apache.shiro.web.mgt.DefaultWebSecurityManager.<init>(DefaultWebSecurityManager.java:72)
at org.apache.shiro.web.config.WebIniSecurityManagerFactory.createDefaultInstance(WebIniSecurityManagerFactory.java:65)
at org.apache.shiro.config.IniSecurityManagerFactory.createDefaults(IniSecurityManagerFactory.java:155)
at org.apache.shiro.web.config.WebIniSecurityManagerFactory.createDefaults(WebIniSecurityManagerFactory.java:71)
at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:118)
at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:97)
at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:83)
at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:41)
at org.apache.shiro.config.IniFactorySupport.createInstance(IniFactorySupport.java:123)
at org.apache.shiro.util.AbstractFactory.getInstance(AbstractFactory.java:47)
at org.apache.shiro.web.env.IniWebEnvironment.createWebSecurityManager(IniWebEnvironment.java:203)
at org.apache.shiro.web.env.IniWebEnvironment.configure(IniWebEnvironment.java:99)
at org.apache.shiro.web.env.IniWebEnvironment.init(IniWebEnvironment.java:92)
at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:45)
at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:40)
at org.apache.shiro.web.env.EnvironmentLoader.createEnvironment(EnvironmentLoader.java:226)
at org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:138)
at org.apache.shiro.web.env.EnvironmentLoaderListener.contextInitialized(EnvironmentLoaderListener.java:58)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:963)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:537)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1468)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:791)
at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1436)
at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:673)
at org.apache.catalina.manager.ManagerServlet.doPut(ManagerServlet.java:431)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
我不理解这个异常,因为 subjectDAO 显然是 DefaultSecurityManager 类的定义字段。我错过了什么?
【问题讨论】:
【参考方案1】:此异常表明您的 web 应用的运行时类路径中有多个不兼容的版本化 Shiro JAR 文件。浏览 webapp 的运行时类路径所涵盖的所有文件夹(至少 webapp 的 /WEB-INF/lib
、Tomcat 的 /lib
、JRE 的 /lib
和 /lib/ext
)并确保在一个正确的位置只有一个相同的版本( /WEB-INF/lib
)。
就目前而言,当手动修改构建路径而不是依赖于例如 Maven 时,应该 suffice 删除 only /WEB-INF/lib
中的 shiro-all.jar
。不需要其他与 Shiro 相关的 JAR(当然,除了 Commons BeanUtils)。如果您需要最少的库代码,请使用两个 JAR 文件 shiro-core.jar
和 shiro-web.jar
。其他没有必要。请注意,您不应将其中任何一个与shiro-all.jar
混合使用。
【讨论】:
是的,出于某种原因,我的 WEB-INF/lib 中有 2 个版本的 shiro-core,1.1 和 1.2.1。我的错。应该做 mvn clean。 请注意,shiro-all
可能会在 Shiro 的未来版本中被删除。对于 Maven、Gradle、SBT、Ant+Ivy 等,现在没有理由不使用这些工具之一来保证正确的依赖关系解析。
@Les:Shiro 是否有借口不与 Java EE 5/6 一起前进? :)
@BalusC 不,一点也不——如果有人愿意为它提供支持,我们很乐意支持它!我不能代表开发团队中的其他人发言,但我个人只是没有时间自己为此献身。但我想看看!
@Les:今晚我已经blogged 谈过了。目前浏览量超过 1000 次。以上是关于java.lang.NoSuchFieldError:在Tomcat上部署shiro时出现subjectDAO异常的主要内容,如果未能解决你的问题,请参考以下文章