当前位置: 首页 > news >正文

项目遇到的实际需求: java从信任所有证书到对server证书进行校验

最近项目上开发了一个rest api,放在了一台linux服务器上,并且启用了https连接;在另一台服务器上写了一个功能需要去调用linux机器上的api
项目里面自己封装了一个HttpsClient的类,用来发送https请求,并且在里面重写了TrustManager,方法体都为空,这样就不会对server的证书以及client的证书进行校验,能够顺利的从另一台服务器调用linux上的api

/*** * A default TrustManager which will trust any certificate.**/
private static class DefaultTrustManager implements X509TrustManager {@Overridepublic void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}
}

但是上面的这种方法是不安全的,对server的证书以没有进行校验,就不能确定和自己进行通信的server,到底是不是真正的那个我想要通信的server,有可能是一个中间的,黑客部署的server,这样就会导致数据的安全问题。
于是需要在另一台这端对linux server端的证书进行认证,确认server是不是真正想要的server
解决方案:
linux服务器上,使用openssl生成了一个自签名的ssl证书(如何生成ssl证书),用这个证书来启用linux serverhttps证书,并且将这个证书放到另一台的某个目录,然后另一台系统上的java代码,在发送请求的时候,将证书放到keyStore里面,这样java就能对这个证书进行认证。
下面是ChatGPT给出的示例代码: 读取指定的每一个路径上的证书,放到JKS格式的keyStore里面,然后用这个keyStore初始化TrustManager,最后用TrustManager创建sslcontext

    private SSLContext getSSLContext(JSONArray certificates, String protocol) throws Exception {SSLContext sc = null;if (certificates != null) {String certFileName = null;try {// Create a temp keystore object to be used to make the HTTPS callKeyStore keystore = KeyStore.getInstance("JKS");keystore.load(null,null);for (int i=0; i < certificates.size(); i++) {certFileName = (String)certificates.get(i);try (BufferedInputStream bis = FileFactory.newBufferedInputStream((String)certificates.get(i))) {CertificateFactory cf = CertificateFactory.getInstance("X.509");Certificate cert = cf.generateCertificate(bis);keystore.setCertificateEntry("cert" + i, cert);}}TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());tmf.init(keystore);// Create and initialize the SSL context that will be used by the HTTPS connectionsc = SSLContext.getInstance(protocol);sc.init(null, tmf.getTrustManagers(), null);} catch(IOException e) {throw e;} catch(CertificateException e) {throw e;} catch(KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) {throw e;}}return sc;}

更多关于https的文章,请参考我的https专栏:https://blog.csdn.net/u011069294/category_11083017.html?spm=1001.2014.3001.5482

http://www.lryc.cn/news/89389.html

相关文章:

  • 使用JS来实现轮播图的效果
  • Springboot +spring security,自定义认证和授权异常处理器
  • Dockerfile(1) - FROM 指令详解
  • 【嵌入式Linux】源码菜单配置 | 编译 | 菜单配置的实现 | 源码编译的实现
  • python自动化爬虫实战
  • LVGL-最新版本及其版本定义标准
  • ORB_SLAM2算法中如何计算右目和左目两个特征点的是否匹配?
  • Android 12.0系统Settings主页去掉搜索框
  • 电脑数据丢失如何恢复
  • 大数据分析案例-基于决策树算法构建世界杯比赛预测模型
  • Python 图形界面框架 PyQt5 使用指南
  • 代码随想录算法训练营第四十二天 | 二维dp数组01背包, 力扣 416. 分割等和子集
  • 【1110. 删点成林】
  • 第三章 JVM内存概述
  • 基于SpringBoot的企业客户信息反馈平台的设计与实现
  • 【SA8295P 源码分析】01 - SA8295P 芯片介绍
  • 扩展1:Ray Core详细介绍
  • day08 Spring MVC
  • c++中的extern “C“
  • python异常处理名称整理
  • SpringMVC拦截器
  • Python第八章作业(初级)
  • chatgpt赋能python:Python中如何取消列表
  • Java中List排序的3种方法
  • flutter-读写二进制文件到设备
  • C语言基础知识:内存分配
  • 【Simulink】示波器图形数据导入Matlab重新绘图(论文)
  • 汇编调试及学习
  • Linux - 第19节 - 网络基础(传输层二)
  • web实现日历、阳历农历之间相互转换、npm、push、unshift、includes、innerHTML