gpt4 book ai didi

testing - DropWizard JAR 签名漏洞

转载 作者:行者123 更新时间:2023-11-28 20:15:07 26 4
gpt4 key购买 nike

我正在尝试对我的 DropWizard 0.7.0 应用程序进行单元测试,就像 docs recommend 一样:

@Test
public void runRegisertsLocationResource() {
given:
Environment environment = Mockito.mock(Environment)
JerseyEnvironment jersey = Mockito.mock(JerseyEnvironment)
MyApplication fixture = new MyApplication()
MyConfiguration config = new MyConfiguration()

Mockito.when(environment.jersey()).thenReturn(jersey)

when:
fixture.run(config, environment)

then:
Mockito.verify(jersey).register(Mockito.any(LocationResource))
}

当我运行它时,我得到:

java.lang.SecurityException: class "javax.servlet.DispatcherType"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:952)
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:666)
at java.lang.ClassLoader.defineClass(ClassLoader.java:794)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at io.dropwizard.Configuration.<init>(Configuration.java:62)
at io.auditcloud.micros.backend.service.config.BackendConfiguration.<init>(BackendConfiguration.groovy)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:186)
at io.auditcloud.micros.backend.service.BackendApplicationTest.runRegisertsAllProviders(BackendApplicationTest.groovy:26)
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:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

这是我在 build.gradle 中定义的依赖项:

dependencies {
compile (
'com.codahale.metrics:metrics-core:3.0.2',
'com.codahale.metrics:metrics-json:3.0.1',
'com.wordnik:swagger-jaxrs_2.10:1.3.4',
'io.dropwizard:dropwizard-core:0.7.0',
'io.dropwizard:dropwizard-auth:0.7.0',
'org.apache.commons:commons-lang3:3.3.2',
'io.dropwizard:dropwizard-client:0.7.0',
'org.eclipse.jetty:jetty-servlets:8.1.14.v20131031',
'com.google.inject:guice:3.0'
)

testCompile(
'org.mockito:mockito-all:1.9.0'
)
}

这是怎么回事?


更新:如何重现(一步一步)

  1. 创建一个新的 DropWizard 项目并使用上面提到的确切依赖项(我使用 Gradle,但任何工作都应该没问题)。
  2. 创建一个新的 JUnit 测试类,比如说,在 src/test/java 下,让它看起来和我上面的 JUnit 测试完全一样,except 替换 MyApplication 为您的 DropWizard 应用程序主类的名称,并将 MyConfiguration 替换为相应的配置实现。 注意:我的应用程序是用 Groovy 编写的,因此使用 Groovy 语法;您可能需要对我上面的代码片段进行“Java 化”才能编译。
  3. 运行测试;您应该得到与我在上面发布的相同的异常。

最佳答案

编辑:请参阅下面的更新

这不是答案。请不要投票。将在 OP 确认后删除

所以我想知道您是否亲自测试了上述步骤。我按照您所说的步骤进行操作,当然创建了我自己的类( super 简单,几乎没有实现,因为您没有指定任何实现。而且我没有得到您遇到的结果。我也在使用 Maven和 Java。我不确定问题是否出在 groovy 上,因为我不是 groovy 用户。

下面是我的 Minimal, Complete, and Verifiable example (MCVE)逐步实现。 我建议您创建类似的东西来重现该问题。如您所述,从头开始创建一个项目(可能使用下面的示例作为起点),然后开始添加功能,直到它重现问题。这也是您自己发现问题根源的好方法。

我会从一个简单的 Groovy 项目开始,如果一个简单的项目(如下所示)导致问题,那么它可能是一个 Groovy 问题,您应该使用 MCVE 编辑您的帖子,以便其他熟悉 Groovy 的人可以尝试并找到问题所在。

如果这不是 Groovy 问题,并且您通过在我的 MCVE 之上构建发现问题,并且您无法找出原因以及如何解决它,那么请发布MCVE 也是。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow.spring</groupId>
<artifactId>dropwizard-stackoverflow</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<dropwizard.version>0.7.0</dropwizard.version>
</properties>

<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-auth</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-client</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-json</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

我的配置

import io.dropwizard.Configuration;

public class MyConfiguration extends Configuration {}

我的申请

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class MyApplication extends Application<MyConfiguration> {
@Override
public void initialize(Bootstrap<MyConfiguration> btstrp) {}

@Override
public void run(MyConfiguration t, Environment e) throws Exception {
e.jersey().register(new LocationResource());
}
}

位置资源

import javax.ws.rs.Path;

@Path("/location")
public class LocationResource {}

测试

import dropwizard.stackoverflow.resource.LocationResource;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.setup.Environment;
import org.junit.Test;
import org.mockito.Mockito;

public class SimpleDropWizardTest {
@Test
public void runRegisertsLocationResource() throws Exception {

Environment environment = Mockito.mock(Environment.class);
JerseyEnvironment jersey = Mockito.mock(JerseyEnvironment.class);
MyApplication fixture = new MyApplication();
MyConfiguration config = new MyConfiguration();

Mockito.when(environment.jersey()).thenReturn(jersey);

fixture.run(config, environment);

Mockito.verify(jersey).register(Mockito.any(LocationResource.class));
}
}

测试结果

enter image description here


更新

所以我好像把我的MCVE弄错了。由于某种原因我错过了

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>8.1.14.v20131031</version>
</dependency>

它恰好发生了,这似乎是确凿的证据。当我将这个依赖添加到上面的项目时,我会得到一个类似的异常,它并不完全相同,但在类加载过程中详细描述了相同的问题。

如果你去掉这个依赖,上面的例子就可以工作了。好像是版本冲突。 Dropwizard 已经依赖 Jetty 9.0.7.v20131107。可以看到Maven依赖树

[INFO] +- io.dropwizard:dropwizard-core:jar:0.7.0:compile
...
[INFO] | +- io.dropwizard:dropwizard-jetty:jar:0.7.0:compile
[INFO] | | +- com.codahale.metrics:metrics-jetty9:jar:3.0.1:compile
[INFO] | | +- org.eclipse.jetty:jetty-server:jar:9.0.7.v20131107:compile
[INFO] | | | \- org.eclipse.jetty:jetty-io:jar:9.0.7.v20131107:compile
[INFO] | | +- org.eclipse.jetty:jetty-servlet:jar:9.0.7.v20131107:compile
[INFO] | | | \- org.eclipse.jetty:jetty-security:jar:9.0.7.v20131107:compile
[INFO] | | +- org.eclipse.jetty:jetty-servlets:jar:9.0.7.v20131107:compile
[INFO] | | | \- org.eclipse.jetty:jetty-continuation:jar:9.0.7.v20131107:compile
[INFO] | | \- org.eclipse.jetty:jetty-http:jar:9.0.7.v20131107:compile

基本上,当您声明一个顶级依赖项时(就像您在 Jetty 中所做的那样),它将取代任何传递依赖项。因此,您有一个 Jetty 9.0.7 工件试图与您声明的 8.1.14 servlet 工件进行交互。

关于testing - DropWizard JAR 签名漏洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com