Configuring the Jetty Container as a Https Connector

Jetty版本: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>

<!--生成json串-->

<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>

<!--用来生成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>

说明:keytool-maven-plugin是用来生成jetty-ssl.keystore,默认生成在target目录下。

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">\云化转型\myScalaTest3\jetty-ssl.keystore</Set>

<Set name="TrustStorePath">\云化转型\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="512" /></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>

说明:

1.里面set字段的值都可以修改。尤其要注意KeyStorePath和TrustStorePath的值,一定要确保jetty-ssl.keystore存在。

2.本教程中的jetty-https.xml默认放置在Maven工程目录下(与pom.xml同级)。

3.jetty-https.xml也不是必须的,里面的全部配置可以使用代码来实现。

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("测试成功"))

Common.printString(str,response)

}

else {

Common.printString("请输入正确的路由",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)

}

/**

* 用response输出json串

*/

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)

}

}

}

测试

运行EmbeddedViaXml,然后在浏览器输入:https://localhost:61361/jtEnv0y/586

说明:还有一种方法,需要jetty.xml,jetty-https.xml,jetty-ssl.xml三个配置文件,本教程的jetty-https.xml相当于把三个配置文件集合到了一起。

参考:

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容器配置https的更多相关文章

  1. 容器配置https

    生成秘钥库 通过jdk的keytool工具生成秘钥库 keytool -genkeypair -alias "localhost" -keyalg "RSA" ...

  2. solr基于jetty容器配置后台登陆角色用户名密码

    solr版本4.0.0 我的solr下载后解压在d盘,你的随意. 在D:\apache-solr-4.0.0\apache-solr-4.0.0\example\etc目录下新建role.proper ...

  3. Kubernetes 使用 ingress 配置 https 集群(十五)

    目录 一.背景 1.1 需求 1.2 Ingress 1.3 环境介绍 二.安装部署 2.1.创建后端 Pod 应用 2.2 创建后端 Pod Service 2.3.创建 ingress 资源 2. ...

  4. 在容器服务kubernetes上配置https

    当前容器服务Kubernetes集群支持多种应用访问的形式,最常见形式如SLB:Port,NodeIP:NodePort和域名访问等.但是Kubernetes集群默认不支持HTTPS访问,如果用户希望 ...

  5. Jetty:配置概览-须要配置什么

    上一节讲述了怎么配置Jetty,这节将告诉你使用Jetty你须要配置些什么. 配置Server Server实例是Jetty服务端的中心协调对象,它为全部其它Jetty服务端组件提供服务和生命周期管理 ...

  6. Jetty:配置安全

    用${jetty.home}和${jetty.base}配置安全 Jetty 9.1中:  1)${jetty.home}是jetty公布(二进制)的文件夹路径:  2)${jetty.base}是用 ...

  7. SolrCloud7.4(Jetty容器)+mysql oracle 部署与应用

    SolrCloud7.4(Jetty容器)搭建 1.Zookeeper搭建 版本:zookeeper-3.4.10.tar.gz 1.把zookeeper安装包上传到服务器 2.zookeeper解压 ...

  8. 图文详解 IntelliJ IDEA 15 创建 Maven 构建的 Java Web 项目(使用 Jetty 容器)

    图文详解 IntelliJ IDEA 15 创建 maven 的 Web 项目 搭建 maven 项目结构 1.使用 IntelliJ IDEA 15 新建一个项目.  2.设置 GAV 坐标  3. ...

  9. cargo实现自动化部署远程jetty容器(非安全模式)

    cargo实现自动化部署应用至远程jetty容器 (非安全模式) 一.准备: WAR包:Deployer Web application for the Jetty remote containers ...

随机推荐

  1. BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...

  2. BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级 分层图最短路 + Dijkstra

    Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i ...

  3. 下拉框处理(select)

    在UI自动化测试过程中,经常会遇到一些下拉框,我们有三种可选方式来操作下拉框. 第一种方法 基于webdriver的两次click,很容易出现问题,不建议使用.(由于部分下拉框在点击一次后,失去焦点再 ...

  4. POJ3069 Saruman's Army【贪心】

    Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...

  5. [luogu 4886] 快递员

    传送门 Solution 虽然不是点分治但用类似点分治的方法不断接近正确结果 Code // luogu-judger-enable-o2 #include <cstdio> #inclu ...

  6. Linux放弃到入门

    流星,因为短暂而美丽,划过黑寂的夜空,释放出那一闪而逝的光芒,虽然微弱,但却没有人能无视它的存在.人生如同流星,充满了精彩与传奇,如同一支美丽的传说,究竟能否想流星那样短暂,别人决定不了,上天也决定不 ...

  7. 【Codeforces 342A】Xenia and Divisors

    [链接] 我是链接,点我呀:) [题意] [题解] 最后a,b,c只有以下3种情况 1,2,4 1,2,6 1,3,6 那么用cnt[8]统计每个数字出现的次数. 输出cnt[4]次1,2,4 (如果 ...

  8. 【hihoCoder挑战赛28 A】异或排序

    [题目链接]:http://hihocoder.com/problemset/problem/1509 [题意] [题解] 每次找到相邻两个数的二进制形式中; 不同的最高位; 显然S在这一位必然是确定 ...

  9. (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...

  10. POJ2116 Death to Binary?

    /* POJ2116 Death to Binary? http://poj.org/problem?id=2116 齐肯多夫定理 */ #include <cstdio> #includ ...