一.添加openssl库
configure相关命令:
-no-openssl ........ Do not compile support for OpenSSL.
-openssl ........... Enable run-time OpenSSL support.
-openssl-linked .... Enabled linked OpenSSL support.
这里采用动态编译
./configure -openssl
如果采用静态编译
OPENSSL_LIBS='-L/opt/ssl/lib -lssl -lcrypto' ./configure -openssl-linked
二.QWebView添加ssl处理
1.添加网络管理;
1 this->page()->setNetworkAccessManager(networkAccessManager);
2.继承QNetworkAccessManager,重写sslErrors()函数;
代码参考arora浏览器:
1 void myQNetworkAccessManager::sslErrors(QNetworkReply *reply, const QList<QSslError> &error) 2 { 3 QSettings settings; 4 QList<QSslCertificate> ca_merge = QSslCertificate::fromData(settings.value(QLatin1String("CaCertificates")).toByteArray()); 5 6 QList<QSslCertificate> ca_new; 7 QStringList errorStrings; 8 for (int i = 0; i < error.count(); ++i) { 9 if (ca_merge.contains(error.at(i).certificate())) 10 continue; 11 errorStrings += error.at(i).errorString(); 12 if (!error.at(i).certificate().isNull()) { 13 ca_new.append(error.at(i).certificate()); 14 } 15 } 16 if (errorStrings.isEmpty()) { 17 reply->ignoreSslErrors(); 18 return; 19 } 20 21 QString errors = errorStrings.join(QLatin1String("</li><li>")); 22 if (true) { 23 if (ca_new.count() > 0) { 24 QStringList certinfos; 25 if (true) { 26 ca_merge += ca_new; 27 28 QSslConfiguration sslCfg = QSslConfiguration::defaultConfiguration(); 29 QList<QSslCertificate> ca_list = sslCfg.caCertificates(); 30 ca_list += ca_new; 31 sslCfg.setCaCertificates(ca_list); 32 sslCfg.setProtocol(QSsl::AnyProtocol); 33 QSslConfiguration::setDefaultConfiguration(sslCfg); 34 reply->setSslConfiguration(sslCfg); 35 36 QByteArray pems; 37 for (int i = 0; i < ca_merge.count(); ++i) 38 pems += ca_merge.at(i).toPem() + '\n'; 39 settings.setValue(QLatin1String("CaCertificates"), pems); 40 } 41 } 42 reply->ignoreSslErrors(); 43 } 44 }
3.sslErrors()中可以用QNetworkReply::ignoreSslErrors()忽略掉所有错误,但是这样不安全;
还可以选择为网页注册一个新的SSL认证(通过QSslConfiguration::setCaCertificates())。
参考资料:
1.http://www.qtcentre.org/threads/14398-Use-of-QWebView-to-browse-quot-https-quot-secure-sites
2.http://code.google.com/p/arora/