java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not

时间:2022-10-12 07:40:07

这是一个jar包冲突引起的Java程序错误,而且是runtime的错误,编译的时候不出错,一运行起来就挂。

大致的错误信息如下:

Thread[main,5,main] exit with uncaught error java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package

java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package

at java.lang.ClassLoader.checkCerts(ClassLoader.java:895)

at java.lang.ClassLoader.preDefineClass(ClassLoader.java:665)

at java.lang.ClassLoader.defineClass(ClassLoader.java:758)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)

at java.net.URLClassLoader.access$100(URLClassLoader.java:73)

at java.net.URLClassLoader$1.run(URLClassLoader.java:368)

at java.net.URLClassLoader$1.run(URLClassLoader.java:362)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:361)

at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

网上一搜就知道,这是jar包冲突引起的,也就是说,classpath中的两个版本不同的jar包都含有FilterRegistration类的实现,并且它们不兼容。 

那为什么会产生冲突呢?虽然你的pom.xml文件(Maven的工程)里只包含有一个javax-servlet-api的依赖,但是有其他的jar包会间接依赖于其他版本的 servlet-api,从而可能会产生冲突。

为了找出这个冲突的jar包,你可以使用IntelliJ IDEA的Maven插件 Maven Helper 来帮忙,或者你不愿装新插件的话,直接用如下命令来导出各jar包的依赖关系:

mvn dependency:tree > dependency.txt

在这里,我把结果输出到了一个文件 x.txt 中,内容类似于(仅部分):

[INFO] +- org.apache.hadoop:hadoop-client:jar:2.6.0:compile
[INFO] |  +- org.apache.hadoop:hadoop-common:jar:2.6.0:compile
[INFO] |  |  +- commons-cli:commons-cli:jar:1.2:compile
[INFO] |  |  +- xmlenc:xmlenc:jar:0.52:compile
[INFO] |  |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  |  +- commons-configuration:commons-configuration:jar:1.6:compile
[INFO] |  |  |  +- commons-digester:commons-digester:jar:1.8:compile
[INFO] |  |  |  |  \- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] |  |  |  \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
[INFO] |  |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] |  |  +- com.google.code.gson:gson:jar:2.2.4:compile
[INFO] |  |  +- org.apache.curator:curator-client:jar:2.6.0:compile
[INFO] |  |  +- org.htrace:htrace-core:jar:3.0.4:compile
[INFO] |  |  \- org.apache.commons:commons-compress:jar:1.4.1:compile
[INFO] |  |     \- org.tukaani:xz:jar:1.0:compile
[INFO] |  +- org.apache.hadoop:hadoop-hdfs:jar:2.6.0:compile
[INFO] |  |  +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile
[INFO] |  |  +- io.netty:netty:jar:3.6.2.Final:compile
[INFO] |  |  \- xerces:xercesImpl:jar:2.9.1:compile
[INFO] |  |     \- xml-apis:xml-apis:jar:1.3.04:compile
[INFO] |  +- org.apache.hadoop:hadoop-mapreduce-client-app:jar:2.6.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-common:jar:2.6.0:compile
[INFO] |  |  |  +- org.apache.hadoop:hadoop-yarn-client:jar:2.6.0:compile
[INFO] |  |  |  \- org.apache.hadoop:hadoop-yarn-server-common:jar:2.6.0:compile
[INFO] |  |  \- org.apache.hadoop:hadoop-mapreduce-client-shuffle:jar:2.6.0:compile
[INFO] |  +- org.apache.hadoop:hadoop-yarn-api:jar:2.6.0:compile
[INFO] |  +- org.apache.hadoop:hadoop-mapreduce-client-core:jar:2.6.0:compile
[INFO] |  |  \- org.apache.hadoop:hadoop-yarn-common:jar:2.6.0:compile
[INFO] |  |     +- javax.xml.bind:jaxb-api:jar:2.2.2:compile
[INFO] |  |     |  +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] |  |     |  \- javax.activation:activation:jar:1.1:compile
[INFO] |  |     +- javax.servlet:servlet-api:jar:2.5:compile
[INFO] |  |     +- com.sun.jersey:jersey-client:jar:1.9:compile
[INFO] |  |     +- org.codehaus.jackson:jackson-jaxrs:jar:1.9.13:compile
[INFO] |  |     \- org.codehaus.jackson:jackson-xc:jar:1.9.13:compile
[INFO] |  +- org.apache.hadoop:hadoop-mapreduce-client-jobclient:jar:2.6.0:compile
[INFO] |  \- org.apache.hadoop:hadoop-annotations:jar:2.6.0:compile
[INFO] +- org.apache.spark:spark-core_2.10:jar:1.6.0:provided
[INFO] |  +- org.apache.avro:avro-mapred:jar:hadoop2:1.7.7:provided
[INFO] |  |  +- org.apache.avro:avro-ipc:jar:1.7.7:compile
[INFO] |  |  \- org.apache.avro:avro-ipc:jar:tests:1.7.7:provided
[INFO] |  +- com.twitter:chill_2.10:jar:0.5.0:compile
[INFO] |  |  \- com.esotericsoftware.kryo:kryo:jar:2.21:compile
[INFO] |  |     +- com.esotericsoftware.reflectasm:reflectasm:jar:shaded:1.07:compile
[INFO] |  |     +- com.esotericsoftware.minlog:minlog:jar:1.2:compile
[INFO] |  |     \- org.objenesis:objenesis:jar:1.2:compile
[INFO] |  +- com.twitter:chill-java:jar:0.5.0:compile
[INFO] |  +- org.apache.xbean:xbean-asm5-shaded:jar:4.4:provided
[INFO] |  +- org.apache.spark:spark-launcher_2.10:jar:1.6.0:provided
[INFO] |  +- org.apache.spark:spark-network-common_2.10:jar:1.6.0:provided
[INFO] |  +- org.apache.spark:spark-network-shuffle_2.10:jar:1.6.0:provided
[INFO] |  |  +- org.fusesource.leveldbjni:leveldbjni-all:jar:1.8:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.4:compile
[INFO] |  +- org.apache.spark:spark-unsafe_2.10:jar:1.6.0:compile
[INFO] |  +- net.java.dev.jets3t:jets3t:jar:0.7.1:provided
[INFO] |  +- org.apache.curator:curator-recipes:jar:2.4.0:compile
[INFO] |  |  \- org.apache.curator:curator-framework:jar:2.4.0:compile
[INFO] |  +- org.eclipse.jetty.orbit:javax.servlet:jar:3.0.0.v201112011016:provided
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.3.2:provided
[INFO] |  +- org.apache.commons:commons-math3:jar:3.4.1:compile

可见有两处用到 javax.servlet 而且版本不一致

于是我们需要在pom.xml中,视情况把不同版本的servlet-api给“除掉”,这里我选择的是除掉hadoop-common中的旧版本servlet-api:

<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.6.0</version>
			<exclusions>
			<exclusion>
			  <groupId>javax.servlet</groupId>
			  <artifactId>*</artifactId>
			</exclusion>
			</exclusions>
</dependency>
重新打包使用或者直接运行。