Skip to content

Commit 6706547

Browse files
authored
Ssl_QtClient_JavaServer
Ssl_QtClient_JavaServer
1 parent 1b338ea commit 6706547

21 files changed

Lines changed: 821 additions & 0 deletions
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "QSSLClient.h"
2+
#include <QSslSocket>
3+
#include <QSslCertificate>
4+
#include <QFile>
5+
#include <QSslKey>
6+
#include <QJsonDocument>
7+
#include <QList>
8+
#include <QDebug>
9+
10+
QSSLClient::QSSLClient(QObject *parent) : QObject(parent)
11+
{
12+
m_key = new QSslKey;
13+
m_privateCertificate = new QSslCertificate;
14+
m_client = new QSslSocket;
15+
loadCertificate();
16+
17+
connect(m_client, &QSslSocket::readyRead, this, &QSSLClient::rx);
18+
connect(m_client, &QSslSocket::disconnected, this, &QSSLClient::serverDisconnect);
19+
connect(m_client, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
20+
21+
m_client->addCaCertificates(m_publicCertificateList);
22+
m_client->setPrivateKey(*m_key);
23+
m_client->setLocalCertificate(*m_privateCertificate);
24+
m_client->setPeerVerifyMode(QSslSocket::VerifyPeer);
25+
26+
qDebug() << "QSSLClient load over";
27+
}
28+
29+
QSSLClient::~QSSLClient()
30+
{
31+
delete m_privateCertificate;
32+
delete m_client;
33+
delete m_key;
34+
}
35+
36+
void QSSLClient::connectServer()
37+
{
38+
m_client->connectToHostEncrypted("localhost", 19999);
39+
if(m_client->waitForEncrypted(5000)){
40+
41+
qDebug() << "Authentication Suceeded";
42+
}
43+
else{
44+
45+
qDebug("Unable to connect to server");
46+
exit(0);
47+
}
48+
}
49+
50+
void QSSLClient::sendMsg(const QString &msg)
51+
{
52+
m_client->write(msg.toUtf8());
53+
}
54+
55+
void QSSLClient::closeSocket()
56+
{
57+
if(m_client->disconnect()){
58+
59+
m_client->close();
60+
qDebug() << "close success";
61+
}
62+
}
63+
64+
void QSSLClient::loadCertificate()
65+
{
66+
QFile p12File(":/res/p_client.p12");
67+
if(!p12File.open(QIODevice::ReadOnly)){
68+
69+
qDebug() << "The certificate file open failed!";
70+
exit(0);
71+
}
72+
73+
bool ok = QSslCertificate::importPkcs12(&p12File, m_key, m_privateCertificate, &m_publicCertificateList, "cccccc");
74+
if(!ok){
75+
76+
qDebug() << "The certificate import error!";
77+
exit(0);
78+
}
79+
p12File.close();
80+
}
81+
82+
void QSSLClient::sslErrors(const QList<QSslError> &errors)
83+
{
84+
foreach (const QSslError &error, errors)
85+
qDebug() << error.errorString();
86+
}
87+
88+
void QSSLClient::rx()
89+
{
90+
QString getMsg = m_client->readAll();
91+
qDebug() << getMsg;
92+
}
93+
94+
void QSSLClient::serverDisconnect()
95+
{
96+
// m_client->close();
97+
// exit(0);
98+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef QSSLCLIENT_H
2+
#define QSSLCLIENT_H
3+
4+
#include <QObject>
5+
#include <QSslError>
6+
7+
QT_BEGIN_NAMESPACE
8+
class QSslCertificate;
9+
class QSslKey;
10+
class QSslSocket;
11+
QT_END_NAMESPACE
12+
13+
class QSSLClient : public QObject
14+
{
15+
Q_OBJECT
16+
public:
17+
QSSLClient(QObject *parent = nullptr);
18+
~QSSLClient();
19+
void connectServer();
20+
void sendMsg(const QString &msg);
21+
void closeSocket();
22+
23+
Q_SIGNALS:
24+
void disconnected(void);
25+
26+
protected:
27+
void loadCertificate();
28+
29+
private slots:
30+
void sslErrors(const QList<QSslError> &errors);
31+
void rx(void);
32+
void serverDisconnect(void);
33+
34+
private:
35+
QList<QSslCertificate> m_publicCertificateList;
36+
QSslCertificate *m_privateCertificate;
37+
QSslKey *m_key;
38+
QSslSocket *m_client;
39+
};
40+
41+
#endif // QSSLCLIENT_H
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
QT -= gui
2+
3+
QT += network
4+
5+
CONFIG += c++11 console
6+
CONFIG -= app_bundle
7+
8+
# The following define makes your compiler emit warnings if you use
9+
# any Qt feature that has been marked deprecated (the exact warnings
10+
# depend on your compiler). Please consult the documentation of the
11+
# deprecated API in order to know how to port your code away from it.
12+
DEFINES += QT_DEPRECATED_WARNINGS
13+
14+
# You can also make your code fail to compile if it uses deprecated APIs.
15+
# In order to do so, uncomment the following line.
16+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
17+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
18+
19+
SOURCES += \
20+
QSSLClient.cpp \
21+
main.cpp
22+
23+
# Default rules for deployment.
24+
qnx: target.path = /tmp/$${TARGET}/bin
25+
else: unix:!android: target.path = /opt/$${TARGET}/bin
26+
!isEmpty(target.path): INSTALLS += target
27+
28+
HEADERS += \
29+
QSSLClient.h
30+
31+
RESOURCES += \
32+
resources.qrc

Ssl_QtClient_JavaServer/SslClient/SSLClient.pro.user

Lines changed: 319 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <QCoreApplication>
2+
#include <QTimer>
3+
#include <QDebug>
4+
#include "QSSLClient.h"
5+
6+
int main(int argc, char *argv[])
7+
{
8+
QCoreApplication a(argc, argv);
9+
10+
QSSLClient sslClient;
11+
sslClient.connectServer();
12+
sslClient.sendMsg("Hello Server");
13+
QTimer::singleShot(2 * 1000, &sslClient, &QSSLClient::closeSocket);
14+
return a.exec();
15+
}
3.15 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>res/p_client.p12</file>
4+
</qresource>
5+
</RCC>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>SSLDemo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cn.it1995;
2+
3+
import javax.net.ssl.KeyManager;
4+
import javax.net.ssl.SSLSocket;
5+
import javax.net.ssl.TrustManager;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
9+
public class SslClient implements SslContextProvider{
10+
11+
12+
public static void main(String[] args) throws Exception {
13+
14+
new SslClient().run("127.0.0.1", 19999);
15+
}
16+
17+
public TrustManager[] getTrustManagers() throws Exception {
18+
19+
return SslUtil.createTrustManagers("D:\\IDEAProject\\SSLDemo\\src\\main\\resources\\client.jks", "cccccc");
20+
}
21+
22+
public KeyManager[] getKeyManagers() throws Exception {
23+
24+
return SslUtil.createKeyManagers("D:\\IDEAProject\\SSLDemo\\src\\main\\resources\\client.jks", "cccccc");
25+
}
26+
27+
public String getProtocol() {
28+
29+
return "TLSv1.2";
30+
}
31+
32+
private SSLSocket createSSLSocket(String host, Integer port) throws Exception{
33+
34+
return SslUtil.createSSLSocket(host, port, this);
35+
}
36+
37+
public void run(String host, Integer port) throws Exception {
38+
39+
try(SSLSocket sslSocket = createSSLSocket(host, port); OutputStream os = sslSocket.getOutputStream(); InputStream is = sslSocket.getInputStream()){
40+
41+
System.out.println("已成功连接到服务端.......");
42+
43+
os.write("Hello Server".getBytes());
44+
os.flush();
45+
46+
System.out.println("已发送 Hello Server 到服务端");
47+
48+
byte[] buf = new byte[1024];
49+
is.read(buf);
50+
51+
System.out.println("接收到服务端消息:" + new String(buf));
52+
}
53+
catch (Exception e){
54+
55+
e.printStackTrace();
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)