使用SSL为Jetty配置JettyTestContainer [英] Configure JettyTestContainer with SSL for JerseyTest

查看:117
本文介绍了使用SSL为Jetty配置JettyTestContainer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是为我的团队的代码设置集成测试.这些测试将要求对Jersey 2.27中实现的REST端点执行HTTPS请求.在寻找如何执行这种测试的过程中,我偶然发现了 Baeldung的这篇文章,向我介绍了Jersey测试框架及其为此目的实现的提供程序容器.我选择了Jetty容器,因为我们的代码使用了滚动自己的" Jetty实例来完成所有这些工作.我开始实施我们的测试,并遇到了为JettyTestContainer配置SSL来解决其余请求的问题.我开始收到一个IllegalArgumentException异常,指出当不使用SSL时URI方案应为http".我对如何进行一无所知.

I've been tasked with setting up integration tests for my team's code. These tests will require performing HTTPS requests to REST endpoints implemented in Jersey 2.27. In my search for how to perform this kind of test, I stumbled upon this article from Baeldung that pointed me to the Jersey Test Framework and the provider containers they've implemented for this purpose. I chose the Jetty container, as our code was using a 'roll your own' Jetty instance to do all this. I began the implementation of our tests, and ran into difficulty configuring SSL for the JettyTestContainer that would serve up the rest requests. I began receiving an IllegalArgumentException stating that "The URI scheme should be http when not using SSL." I'm at a loss for how to proceed.

这是我的考试班:

public class ProjectTest extends JerseyTest {



 private SSLContext sslCtx;
    
    @Override
    protected Application configure() {
        ResourceConfig conf = new ResourceConfig(Sets.newHashSet(RestSuite1.class));
        SslConfigurator sslConfig = SslConfigurator.newInstance()
            .trustStoreFile()
            .trustStorePassword()
            .keyStoreFile()
            .keyStorePassword()
        sslCtx = sslConfig.createSSLContext();
    }

    @Override
    public URI getBaseUri() {
        return URI.create("https://localhost:1234")
    }

    @Override
    public Client getClient() {
        return ClientBuilder.newBuilder().sslContext(sslCtx).build();
    }

    @Test
    public void test1() throws Exception { 
        String testString = "HelloWorld";
        OurObject oo = target("/restSuite1")
            .request()
            .post(Entity.entity(testString, MediaType.APPLICATION_JSON), String.class);
    }
}

这是RestSuite1:

Here's a RestSuite1:

@Path("restSuite1")
@Produces("application/json")
public class RestSuite1 {
    
    @POST
    @Consumes("application/json")
    @Produces("application/json")    
    public String doSomething(String payload) {
        // Do Something 
    }
}

这是我的pom.xml:

Here's my pom.xml:

<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.sample</groupId>  
  <artifactId>my-application1</artifactId>  
  <version>1.0</version>  
  <packaging>war</packaging>  
  
  <name>my war</name>  
  <url>http://some-url.com</url>  
  
  <dependencies>  
    <dependency>  
      <groupId>javax.ws.rs</groupId>  
      <artifactId>javax.ws.rs-api</artifactId>  
      <version>2.1</version>  
    </dependency>
    <dependency>  
      <groupId>org.glassfish.jersey.containers</groupId>  
      <artifactId>jersey-container-servlet</artifactId>  
      <version>2.27</version>  
    </dependency>
    <dependency>  
      <groupId>org.glassfish.jersey.core</groupId>  
      <artifactId>jersey-server</artifactId>  
      <version>2.27</version>  
    </dependency>
    <dependency>  
      <groupId>org.glassfish.jersey.core</groupId>  
      <artifactId>jersey-client</artifactId>  
      <version>2.27</version>  
    </dependency>
    <dependency>  
      <groupId>org.glassfish.jersey.inject</groupId>  
      <artifactId>jersey-hk2</artifactId>  
      <version>2.27</version>  
    </dependency>
    <dependency>  
      <groupId>org.glassfish.jersey.media</groupId>  
      <artifactId>jersey-media-json-jackson</artifactId>  
      <version>2.27</version>  
    </dependency>
    <dependency>  
      <groupId>org.glassfish.jersey.test-framework</groupId>  
      <artifactId>jersey-test-framework-core</artifactId>  
      <version>2.27</version>
      <scope>test</scope>
    </dependency>
    <dependency>  
      <groupId>org.glassfish.jersey.test-framework.providers</groupId>  
      <artifactId>jersey-test-framework-provider-jetty</artifactId>  
      <version>2.27</version>
      <scope>test</scope>
    </dependency>
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>4.8.2</version>  
      <scope>test</scope>  
    </dependency>  
  </dependencies>  
</project>

我应该注意,在尝试配置SSL之前,我能够成功到达POST/restSuite1端点.

I should note that prior to attempting the SSL configuration, I was able to reach the POST /restSuite1 end point successfully.

我发现了这个问题,但它看起来不像是在某个服务器中使用的Grizzly SSL配置 JerseyTest方案.

I found this question, but it doesn't look like a Grizzly SSL configuration one would use in a JerseyTest scenario.

我发现了这个问题,但它看起来像是设置用户角色和不是实际的SSL配置.

I found this question, but it looks like setting user roles and not an actual SSL configuration.

然后我发现了这个问题,问这个问题的人似乎也遇到了同样的问题可以,但是没有人回答这个问题.

I then found this question, and the person that asked it seems to have the same problem I do, however no one ever answered the question.

请帮助!

推荐答案

似乎该选项在JerseyTest中不可用.我创建了一个拉取请求以添加此选项.请在这里看看: https://github.com/eclipse-ee4j/jersey/pull/4573 我不确定他们批准和发布的速度有多快...

It looks like this option is not available within JerseyTest. I created a pull request to add this option. Please have a look here: https://github.com/eclipse-ee4j/jersey/pull/4573 I am not quite sure how fast they will approve and publish it...

我知道这不是您要寻找的答案...但是,如果您仍想使用ssl配置服务器并使用https测试您的应用程序,而不必等到请求请求获得批准,您可以看看我的github项目,其中有一个在没有JerseyTest的情况下使用grizzly测试球衣服务器的基本示例:集成测试

I know it was not the answer you are looking for... However if you still want to configure your server with ssl and test your application with https without waiting till the pull request got approved, you can have a look at my github project which has a basic example of testing a jersey server with grizzly without JerseyTest: Application & IntegrationTest

===============更新#1 20-12-2020 ===============

=============== UPDATE #1 20-12-2020 ===============

拉取请求已被批准,合并和释放.现在从版本2.33开始可用.现在可以提供带有sslparameters的sslcontext来配置球衣测试.设置示例为:

The pull request has been approved, merged and released. It is now available starting from version 2.33. It is now possible to provide a sslcontext with sslparameters to configure your jersey test. An example setup would be:

public class SecuredJerseyTest extends JerseyTest {

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new GrizzlyTestContainerFactory();
    }

    @Path("hello")
    public static class TestResource {
        @GET
        public String hello() {
            return "hello";
        }
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(TestResource.class);
    }

    @Override
    protected URI getBaseUri() {
        return UriBuilder
                .fromUri("https://localhost")
                .port(getPort())
                .build();
    }

    @Override
    protected Optional<SSLContext> getSslContext() {
        SSLContext sslContext = ... // your initialised server sslContext 
        return Optional.of(sslContext);
    }

    @Override
    protected Optional<SSLParameters> getSslParameters() {
        serverSslParameters = new SSLParameters();
        serverSslParameters.setNeedClientAuth(false);
        return Optional.of(serverSslParameters);
    }

    @Test
    public void testHello() {
        SSLContext sslContext = ... // your initialised client sslContext 

        Client client = ClientBuilder.newBuilder()
                .sslContext(sslContext)
                .build();

        WebTarget target = client.target(getBaseUri()).path("hello");

        String s = target.request().get(String.class);
        Assert.assertEquals("hello", s);
    }
}

这篇关于使用SSL为Jetty配置JettyTestContainer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆