如何在端口 80 上执行 SSL 操作

Posted

技术标签:

【中文标题】如何在端口 80 上执行 SSL 操作【英文标题】:How to perform SSL operations on port 80 【发布时间】:2014-06-13 12:38:51 【问题描述】:

我有一个 server.java 文件:

import javax.net.ssl.*;
import java.io.*;
//*****************
public class Server

    public static void main(String[] args)
    
        try 
            SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
            SSLServerSocket sslserversocket =(SSLServerSocket)sslserversocketfactory.createServerSocket(80);

            SSLSocket sslsocket = (SSLSocket)
            sslserversocket.accept();

            InputStream is = sslsocket.getInputStream();
            InputStreamReader isr = new
            InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);
            String string = null;

            while ((string = br.readLine()) != null)
            
            System.out.println(string);
            System.out.flush();
            
         

        catch (Exception e)
            e.printStackTrace();
        
    

和一个 Client.java 文件为:

import javax.net.ssl.*;
import java.io.*;
//*****************
public class Client

    public static void main(String[] args)
    
        try 
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("127.0.0.1", 80);

            InputStream is = System.in;
            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);
            OutputStream os = sslsocket.getOutputStream();

            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);

            String string = null;
            while ((string = br.readLine()) != null)
            
            bw.write(string + '\n');
            bw.flush();
            
        
        catch (Exception e)
            e.printStackTrace();
        
    

我也像这样创建了自己的证书:

C:\Temp>keytool -genkey -keystore myCertificate -keyalg RSA
Enter keystore password: abcdefg
Re-enter new password: abcdefg
What is your first and last name?
[Unknown]: first last
What is the name of your organizational unit?
[Unknown]: cs
What is the name of your organization?
[Unknown]: ***
What is the name of your City or Locality?
[Unknown]: NYC
What is the name of your State or Province?
[Unknown]: NY
What is the two-letter country code for this unit?
[Unknown]: us
Is CN=first last, OU=cs, O=***, L=NYC, ST=NY, C=us correct?
[no]: yes
Enter key password for <mykey>
(RETURN if same as keystore password):

但无论我做什么,我都会不断收到错误消息:

main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during
handshake
main, SEND TLSv1 ALERT:  fatal, description = handshake_failure
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
        at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
        at sun.security.ssl.AppOutputStream.write(Unknown Source)
        at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
        at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
        at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
        at sun.nio.cs.StreamEncoder.flush(Unknown Source)
        at java.io.OutputStreamWriter.flush(Unknown Source)
        at java.io.BufferedWriter.flush(Unknown Source)
        at Client.main(Client.java:25)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
        at sun.security.ssl.InputRecord.read(Unknown Source)
        ... 11 more

我担心的是,如果我将端口从 80 更改为其他端口,代码可以正常工作,但我必须专门针对此端口本身进行工作。证书和两个 java 文件都在同一个文件夹中。你们能帮我找出一种在端口 80 上执行 SSL 操作的方法吗?

【问题讨论】:

我知道它没有为 @Java1 配置,但我要求一种方法来实现它,以便该操作可以工作 @BalusC 你能帮我解决这个问题吗 【参考方案1】:

我想出了一个办法。

基本上我所做的是使用 java 的 keystore 生成一个 keystore.jks 文件,然后我使用 SSLContext 使我的 keystore 证书有效,因此在端口 80 上执行 SSL .

感谢大家的帮助。

【讨论】:

【参考方案2】:

SSL 运行在 443 端口。并且是默认的,和 80 一样,地址栏上没有显示。

【讨论】:

但我想使用端口 80 @lidl 执行操作 这不是浏览器代码。 OP 编写了自己的服务器/客户端代码。 对不起,我没有认真阅读。尝试阅读***.com/questions/15589880/…

以上是关于如何在端口 80 上执行 SSL 操作的主要内容,如果未能解决你的问题,请参考以下文章

在端口 80 上为 Tomcat 和在 8080 上的 Apache 使用相同的 SSL 证书

我配置好一个tomcat,ssl证书,如何给同一台机器上的其他tomcat配置证书,默认的80,443端口已经被占用。

如何解决mac占用80端口的问题

apache+ssl 在linux上搭建https超文本传输,关闭80端口

Citrix DDC添加443端口证书通过SSL加密访问

亚马逊 ec2 如何设置 https?