Search code examples
tomcattomcat7tomcat8tomcat9tomcat10

How to remotely deploy a secure websocket service on Tomcat10 through Maven


I have burned myself trying to set up a secure WebSocket service using my remote Tomcat10 server but without any success. I keep on getting from Postman "Unexpected server response: 404" after hitting "wss://mydomain.org:4123/ws". I attach the most important settings in case someone could hopefully spot what I am doing wrong.

I would like to mention that I am deploying with Maven the project from 127.0.0.1 via a Tunnel.

server.xml

...
<Service name="Catalina">
<Connector executor="tomcatThreadPool"
           port="5123"
           compression="off"
           protocol="HTTP/1.1"
           connectionTimeout="20000"
           />

<Connector port="4123"
           protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="100"
           compression="off"
           connectionTimeout="20000"
           SSLEnabled="true"
           scheme="https"
           secure="true">

  <SSLHostConfig >
    <Certificate  certificateFile="path/to/certificateFile"
                  certificateKeyFile="path/to/certificateKeyFile"
                  certificateChainFile="path/to/certificateChainFile" />
  </SSLHostConfig>
</Connector>

<Engine name="Catalina" defaultHost="localhost">
...
  <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
...
  </Host>
</Engine>

pom.xml

...
<dependencies>
    <dependency>
        <groupId>com.lambdaworks</groupId>
        <artifactId>scrypt</artifactId>
        <version>1.4.0</version>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.3.1</version>
    </dependency>

    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <finalName>My_WebService</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://127.0.0.1:5123/manager/text</url>
                <path>/</path>
                <server>My-Server</server>
                <finalName>My_WebService</finalName>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.2</version>
        </plugin>
    </plugins>
</build>

web.xml

...

<display-name>WS Engine</display-name>
<description>Desc.</description>

<resource-ref>
    <description>My Database</description>
    <res-ref-name>jdbc/my-database</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

<context-param>
    <param-name>org.apache.tomcat.websocket.binaryBufferSize</param-name>
    <param-value>65536</param-value>
</context-param>

MainWsClass

@ServerEndpoint("/ws")
public class MainWsClass {..}

Solution

  • I eventually ended up using the following POM which works:

    ...
    <dependencies>
    
    <dependency>
      <groupId>org.codehaus.cargo</groupId>
      <artifactId>cargo-maven3-plugin</artifactId>
      <version>1.9.9</version>
    </dependency>
    
    <dependency>
      <groupId>com.lambdaworks</groupId>
      <artifactId>scrypt</artifactId>
      <version>1.4.0</version>
    </dependency>
    
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.3.1</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-core</artifactId>
      <version>10.1.0-M10</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-websocket</artifactId>
      <version>10.1.0-M10</version>
    </dependency>
    
    </dependencies>
    
    <build>
    <finalName>My_WebService</finalName>
    <plugins>
    
        <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven3-plugin</artifactId>
        <version>1.9.9</version>
        <configuration>
          <container>
            <containerId>tomcat10x</containerId>
            <type>remote</type>
          </container>
          <configuration>
            <type>runtime</type>
            <properties>
              <cargo.server.settings>My_WebService</cargo.server.settings>
            </properties>
          </configuration>
        </configuration>
        </plugin>
    
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
          <webResources>
            <resource>
              <directory>src/main/webapp</directory>
              <filtering>true</filtering>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    
    </plugins>
    </build>