Jetty Container Configuration HTTPS

Source: Internet
Author: User
Tags deprecated dname

Configuring the Jetty Container as a Https Connector

Jetty Version: 9.2.22.v20170606

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>learnJetty</groupId>

<artifactId>learnJetty</artifactId>

<version>1.0-SNAPSHOT</version>

<dependencies>

<dependency>

<groupId>org.eclipse.jetty.aggregate</groupId>

<artifactId>jetty-all</artifactId>

<version>9.2.22.v20170606</version>

</dependency>

<!--generating JSON strings--

<dependency>

<groupId>com.fasterxml.jackson.module</groupId>

<artifactId>jackson-module-scala_2.10</artifactId>

<version>2.4.4</version>

</dependency>

</dependencies>

<build>

<finalName>JettyHttps</finalName>

<plugins>

<!--used to generate keystore-->

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>keytool-maven-plugin</artifactId>

<version>1.5</version>

<executions>

<execution>

<phase>generate-resources</phase>

<id>clean</id>

<goals>

<goal>clean</goal>

</goals>

</execution>

<execution>

<phase>generate-resources</phase>

<id>genkey</id>

<goals>

<goal>generateKeyPair</goal>

</goals>

</execution>

</executions>

<configuration>

<keystore>${project.build.directory}/jetty-ssl.keystore</keystore>

<dname>cn=127.0.0.1</dname><!--Put your CN here--

<keypass>dypBdX1NB3gXA0DXCy9nfyJ4jqUDlaydgbo9OU12g</keypass>

<storepass>dypBdX1NB3gXA0DXCy9nfyJ4jqUDlaydgbo9OU12g</storepass>

<alias>jetty</alias>

<keyalg>RSA</keyalg>

</configuration>

</plugin>

</plugins>

</build>

</project>

Description: Keytool-maven-plugin is used to generate Jetty-ssl.keystore, which is generated by default in the target directory.

Jetty-https.xml

<?xml version= "1.0"?>

<! DOCTYPE Configure Public "-//jetty//configure//en" "Http://www.eclipse.org/jetty/configure_9_0.dtd" >

<configure id= "Server" class= "Org.eclipse.jetty.server.Server" >

<call id= "Httpsconnector" name= "AddConnector" >

<Arg>

<new class= "Org.eclipse.jetty.server.ServerConnector" >

<arg name= "Server" ><ref refid= "Server"/></arg>

<arg name= "Factories" >

<array type= "Org.eclipse.jetty.server.ConnectionFactory" >

<Item>

<new class= "Org.eclipse.jetty.server.SslConnectionFactory" >

<Arg>

<new class= "Org.eclipse.jetty.util.ssl.SslContextFactory" >

<set name= "Keystorepath" >\ Cloud Transformation \myscalatest3\jetty-ssl.keystore</set>

<set name= "Truststorepath" >\ Cloud Transformation \myscalatest3\jetty-ssl.keystore</set>

<set name= "Keystorepassword" ><property name= "Jetty.keystore.password" default= " dypbdx1nb3gxa0dxcy9nfyj4jqudlaydgbo9ou12g "/></set>

<set name= "Keymanagerpassword" ><property name= "Jetty.keymanager.password" default= " dypbdx1nb3gxa0dxcy9nfyj4jqudlaydgbo9ou12g "/></set>

<set name= "Truststorepassword" ><property name= "Jetty.truststore.password" default= " dypbdx1nb3gxa0dxcy9nfyj4jqudlaydgbo9ou12g "/></set>

<set name= "Endpointidentificationalgorithm" ></Set>

<set name= "Excludeciphersuites" >

<array type= "String" >

<Item>SSL_RSA_WITH_DES_CBC_SHA</Item>

<Item>SSL_DHE_RSA_WITH_DES_CBC_SHA</Item>

<Item>SSL_DHE_DSS_WITH_DES_CBC_SHA</Item>

<Item>SSL_RSA_EXPORT_WITH_RC4_40_MD5</Item>

<Item>SSL_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>

<Item>SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA</Item> <item>ssl_dhe_dss_export_with_des40_cbc_ Sha</item>

</Array>

</Set>

</New>

</Arg>

<arg name= "Next" >HTTP/1.1</Arg>

</New>

</Item>

<Item>

<new class= "Org.eclipse.jetty.server.HttpConnectionFactory" >

<Arg>

<new class= "Org.eclipse.jetty.server.HttpConfiguration" >

<set name= "Securescheme" ><property name= "jetty.httpConfig.secureScheme" default= "https"/></set>

<set name= "Secureport" ><property name= "Jetty.httpConfig.securePort" deprecated= "Jetty.secure.port" default= "61361"/></set>

<set name= "outputbuffersize" ><property name= "jetty.httpConfig.outputBufferSize" deprecated= " Jetty.output.buffer.size "default=" 32768 "/></set>

<set name= "outputaggregationsize" ><property name= "jetty.httpConfig.outputAggregationSize" deprecated= " Jetty.output.aggregation.size "default=" 8192 "/></set>

<set name= "requestheadersize" ><property name= "jetty.httpConfig.requestHeaderSize" deprecated= " Jetty.request.header.size "default=" 8192 "/></set>

<set name= "responseheadersize" ><property name= "jetty.httpConfig.responseHeaderSize" deprecated= " Jetty.response.header.size "default=" 8192 "/></set>

<set name= "sendserverversion" ><property name= "jetty.httpConfig.sendServerVersion" deprecated= " Jetty.send.server.version "default=" true "/></set>

<set name= "Senddateheader" ><property name= "Jetty.httpConfig.sendDateHeader" deprecated= " Jetty.send.date.header "default=" false "/></set>

<set name= "headercachesize" ><property name= "jetty.httpConfig.headerCacheSize" default= "Up"/></set >

<set name= "delaydispatchuntilcontent" ><property name= "Jetty.httpConfig.delayDispatchUntilContent" Deprecated= "Jetty.delaydispatchuntilcontent" default= "true"/></set>

</New>

</Arg>

</New>

</Item>

</Array>

</Arg>

<set name= "host" ><property name= "Jetty.host"/></set>

<set name= "Port" ><property name= "Jetty.https.port" default= "61361"/></set>

<set name= "IdleTimeout" >60000</Set>

</New>

</Arg>

</Call>

</Configure>

Description

1. The value of the Set field inside can be modified. Pay particular attention to the values of Keystorepath and Truststorepath, and be sure that jetty-ssl.keystore exists.

2. The jetty-https.xml in this tutorial is placed by default in the MAVEN project directory (sibling to Pom.xml).

3.jetty-https.xml is not required, and the entire configuration can be implemented using code.

Embeddedviaxml.scala

Import Java.io.File

Import Java.net.URL

Import Java.util

Import Org.eclipse.jetty.server.Server

Import Org.eclipse.jetty.server.handler.ContextHandler

Import org.eclipse.jetty.xml.XmlConfiguration;

Object Embeddedviaxml

{

def main (args:array[string]): Unit = {

Try

{

println (System.getproperty ("User.dir"))

Val configuration:xmlconfiguration = new Xmlconfiguration (New File ("Jetty-https.xml"). Touri (). Tourl ())

Val obj = configuration.configure ()

Val server:server = Obj.asinstanceof[server]

Server.setstopatshutdown (True)

Val Contexthandler = new Contexthandler ()

println ("Contexthandler ...")

Contexthandler.setmaxformcontentsize (51200000)

Contexthandler.setcontextpath ("/jtenv0y")

println ("setcontextpath/jtenv0y")

Val handler = new Jthandler ()

Contexthandler.sethandler (Handler)

Server.sethandler (Contexthandler)

Server.start ()

println ("Start Server")

Server.join ()

}catch {

Case e:throwable = E.printstacktrace ()

}

}

}

Jthandler.scala

Import Javax.servlet.http. {httpservletrequest, HttpServletResponse}

Import Org.eclipse.jetty.server.Request

Import Org.eclipse.jetty.server.handler.AbstractHandler

Class Jthandler extends abstracthandler{

Override Def handle (target:string,

Req:request,

Request:httpservletrequest,

Response:httpservletresponse) = {

Response.setcontenttype ("Application/json; Charset=utf-8 ")

println (target)

println (Req.getcontextpath)

println (Req.getcontext)

if (Target.contains ("jtenv0y") | | Req.getcontextpath () = = "/jtenv0y") {

Val str = Common.getjson (Array ("Test succeeded"))

Common.printstring (Str,response)

}

else {

Common.printstring ("Please enter the correct route", response)

}

}

}

Common.scala

Import Javax.servlet.http.HttpServletResponse

Import Com.fasterxml.jackson.databind.ObjectMapper

Import Com.fasterxml.jackson.module.scala.DefaultScalaModule

Object Common {

def Getjson (res:any): String ={

New Objectmapper (). Registermodule (Defaultscalamodule). writevalueasstring (RES)

}

/**

* Output JSON string with response

*/

def printstring (str:string, response:httpservletresponse): Unit = {

try {

Val out = Response.getwriter

Out.print (str)

Out.flush ()

Out.close ()

} catch {

Case e:exception = println ("Exception message:", E)

}

}

}

Test

Run Embeddedviaxml, and then enter in the browser: https://localhost:61361/jtEnv0y/586

Note: There is also a way to jetty.xml,jetty-https.xml,jetty-ssl.xml three configuration files, the jetty-https.xml of this tutorial is equivalent to bringing together three configuration files.

Reference:

http://juplo.de/configure-https-for-jetty-maven-plugin-9-0-x/#comment-53352

https://stackoverflow.com/questions/3794892/howto-use-https-ssl-with-maven-mortbay-jetty-plugin/31241196?s=1| 10.4523#31241196

Http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html

https://stackoverflow.com/questions/17817375/ How-can-i-programmatically-start-a-jetty-server-with-multiple-configuration-file?noredirect=1

Jetty Container Configuration HTTPS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.