maven jetty tomcat https ssl 입문


 참조 링크
 
Java Tomcat SSL 서버 / 클 라 이언 트 양 방향 인증 (1)
 
Java Tomcat SSL 서버 / 클 라 이언 트 양 방향 인증 의 Apache HttpClient (2)
 
어떻게 하면 Jetty 가 Https 를 지원 할 수 있 습 니까?
 
다음은 완전한 pom (웹 의 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();
        }
    }

}

 

좋은 웹페이지 즐겨찾기