`
dreamoftch
  • 浏览: 492779 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

maven jetty tomcat https ssl 入门

阅读更多

 

 参考链接

 

Java Tomcat SSL 服务端/客户端双向认证(一)

 

Java Tomcat SSL 服务端/客户端双向认证のApache HttpClient(二)

 

如何让jetty支持Https :

 

下面是一个完整的pom(一个web的maven项目)

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.26</version>
        <configuration>
          <jvmArgs>-Xmx2048m -Xms1536m -XX:PermSize=128m -XX:MaxPermSize=256m</jvmArgs>
          <!-- http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin -->
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <connectors>
            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              <port>18080</port>
            </connector>
            <connector implementation="org.mortbay.jetty.security.SslSocketConnector">
              <port>443</port>
              <keystore>${project.build.directory}/jetty-ssl.keystore</keystore>
              <password>jetty6</password>
              <keyPassword>jetty6</keyPassword>
            </connector>
          </connectors>
          <contextPath>/</contextPath>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>keytool-maven-plugin</artifactId>
        <version>1.3</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=my.hostname.tld</dname>
          <keypass>jetty6</keypass>
          <storepass>jetty6</storepass>
          <alias>jetty6</alias>
          <keyalg>RSA</keyalg>
        </configuration>
      </plugin>
    </plugins>

    <finalName>my-webapp</finalName>
  </build>
</project>

 

 

然后maven jetty:run

 

接下来就可以通过浏览器访问了,https://localhost:443

 

只不过这时候浏览器会提示不信任的证书。。。没关系,至少我们的服务器端已经支持https了

 

 或者通过下面的代码访问:

 

/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package org.apache.http.examples.client;

import java.io.File;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

/**
 * This example demonstrates how to create secure connections with a custom SSL
 * context.
 */
public class ClientCustomSSL {

    public final static void main(String[] args) throws Exception {
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom()
                .loadTrustMaterial(new File("keystore_path"), "keystore_password".toCharArray(),
                        new TrustSelfSignedStrategy())
                .build();
        // Allow TLSv1 protocol only
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                new String[] { "TLSv1" },
                null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .build();
        try {
            HttpGet httpget = new HttpGet("https://127.0.0.1:443/test");

            System.out.println("executing request " + httpget.getRequestLine());

            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();

                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(entity));
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

}

 

 

分享到:
评论

相关推荐

    tomcat7,8,maven

    - **更好的SSL配置**:Tomcat 8的SSL配置更友好,支持更多的加密套件。 3. **Maven简介**:Maven是Apache的另一个项目,用于管理Java项目的构建、报告和文档生成过程。它通过POM(Project Object Model)文件来...

    Jetty中文手册

    这个wiki提供jetty的入门教程、基础配置、功能特性、优化、安全、JavaEE、监控、常见问题、故障排除帮助等等。它包含教程、使用手册、视频、特征描述、参考资料以及常见问题。 Jetty文档 ---------------- 入门...

    jetty-6.1.26.zip

    Jetty是一款轻量级、高性能的Java Web服务器和Servlet容器,与Tomcat相似,它为开发和部署Web应用程序提供了一种高效的选择。Jetty在设计上注重灵活性和可扩展性,使得它在处理HTTP协议、WebSocket协议以及部署各种...

    java web开发部署神器--jetty插件

    - SSL/TLS:Jetty可以配置为支持HTTPS,提供安全的数据传输。 - 热部署:当检测到Web应用的更新时,Jetty可以自动重新加载更改,无需重启服务器。 总的来说,Jetty作为一款Java Web开发的利器,凭借其轻量级、快速...

    java jetty容器

    6. **管理工具**: Jetty提供了jetty-maven-plugin,使得通过Maven进行部署和管理变得更加方便。此外,Jetty还有一套命令行工具,可用于启动、停止和监控服务器状态。 7. **社区活跃**: Jetty拥有一个活跃的开发者...

    jetty-distribution-9.2.10.v20150310 官方下载

    Jetty是一款轻量级、高性能的Java Web服务器和Servlet容器,与Tomcat相似,它为开发人员提供了在Java环境中部署Web应用的便捷途径。标题提到的"jetty-distribution-9.2.10.v20150310 官方下载"指的是Jetty的9.2.10...

    jetty-distribution-9.3.12.v20160915.tar.gz

    Jetty的轻量级特性体现在其设计中,它不依赖任何重型的Java EE容器,比如Apache Tomcat,这使得启动和运行Jetty非常快速。 在解压"jetty-distribution-9.3.12.v20160915"后,你会看到以下几个关键目录: 1. **bin*...

    jetty-distribution-9.4.8.v20171121.tar.gz

    用户还可以通过Maven或Gradle等构建工具将Jetty作为依赖项添加到项目中,实现嵌入式部署。 总的来说,Jetty 9.4.8是一个强大且灵活的Java Web服务器,适合用于各种规模的Web应用,无论是小型项目还是大型企业级系统...

    struts2集成struts-ssl-plugin插件实现https访问例子

    在Web服务器(如Tomcat、Jetty等)的配置中,需要为HTTPS设置监听端口和SSL密钥库。例如,在Tomcat的`server.xml`中: ```xml SSLEnabled="true" maxThreads="150" minSpareThreads="25" maxSpareThreads="75...

    apache-tomcat-8.5.38-src.zip

    源代码可以帮助开发者理解和增强Tomcat的安全性,如配置SSL/TLS以实现安全的HTTPS通信,或者添加安全策略限制恶意访问。 **6. 性能优化** 通过源码,开发者可以了解性能瓶颈并进行优化,比如调整线程池大小、内存...

    即时通讯,单聊,和群聊,maven 工程,直接部署

    WebSocket服务端通常基于Java的WebSocket API(如Jetty或Tomcat)实现,它监听特定端口,接收并响应来自客户端的WebSocket连接请求。 2. **用户认证与会话管理**:为了确保通信的安全性,系统可能集成了用户登录...

    springboot2.0发布代码时支持https

    如果需要使用 Jetty 作为嵌入式容器替代默认的 Tomcat,可以这样配置 Maven 依赖: ```xml &lt;!-- 排除 spring-boot-starter-tomcat --&gt; &lt;groupId&gt;org.springframework.boot &lt;artifactId&gt;spring-boot-starter-web ...

    beacon-mirror:一个 Google Glass Mirror API 应用程序,用于参与和分享来自 Beacon Crawl http 的照片

    建议使用 Apache Maven 和 Tomcat 7 来运行此 Java 应用程序(而不是 Jetty,正如 Google 在其快速入门中推荐的那样)。 这部分是因为应用程序的 HTTPS/SSL 要求。 要部署,请确保从与 pom.xml 文件相同的

    spring boot maven 连接mysql实现增删改查

    Spring Boot通过内嵌Servlet容器(如Tomcat或Jetty)并自动配置Spring框架,使得开发者可以快速地开始项目开发,无需繁琐的配置工作。 接下来,`Maven`是一个项目管理和综合工具,它管理项目的构建、报告和文档,...

    Maven下SpringMVC+MyBatis最精简的例子

    完成上述步骤后,可以通过Maven的`mvn tomcat7:run`或`mvn jetty:run`命令启动项目,然后访问预设的URL进行测试。 总结,SpringMVC与MyBatis的整合让开发更加便捷,通过注解和配置文件,我们可以快速实现业务逻辑与...

    cxf入门例子

    你可以选择适合你项目的方式,将服务部署到应用服务器上,如Tomcat或Jetty。这里提到的"webserver"可能就是一个简单的应用服务器实例,用于运行和测试CXF服务。 5. **客户端调用**:在"webclient1"中,我们可以假设...

    SpringBoot拦截器实现+HTTPSdemo

    在完成开发和测试后,Spring Boot应用通常被打包为可执行的JAR或WAR文件,然后部署到服务器上,如Tomcat、Jetty等。对于HTTPS配置,服务器也需要配置相应的证书和密钥库。 通过学习和实践这个Demo,开发者将能够...

    java-http2-springboot-gradle

    它默认配置了很多常见的功能,如嵌入式Servlet容器(Tomcat或Jetty)、自动配置Spring组件、Maven或Gradle插件等。 3. **Gradle**:Gradle是一个强大的构建自动化工具,支持多语言、多项目构建,通过Groovy或Kotlin...

    mysql57驱动jar包详情下载.doc

    - **配置应用服务器**:在使用Tomcat、Jetty等应用服务器时,将jar包放入服务器的`lib`目录,这样服务器启动时会自动加载驱动。 4. **JDBC连接池** 在生产环境中,推荐使用连接池如HikariCP、C3P0或Apache DBCP来...

    我的第一个博客,请打击大达到发发打发打发打发的

    Java Web应用通常运行在应用服务器上,如Tomcat、Jetty或WebLogic。 **二、服务器配置** 1. **Apache**: Apache HTTP Server是最流行的开源HTTP服务器,可以用于部署静态内容和反向代理。你可以通过配置虚拟主机来...

Global site tag (gtag.js) - Google Analytics