首页 > 编程语言 >Java安全管理____Java通过Https链接读取证书基本信息

Java安全管理____Java通过Https链接读取证书基本信息

时间:2023-09-13 11:34:17浏览次数:43  
标签:java sun ____ Https import Java net public out


import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * @author 18272
 * 证书过期定时任务校验
 */
public class CertUtil {

    public static void main(String[] args) {
    	getCertExpired("https://www.baidu.com/");
    	getCertExpired("https://192.168.**.**");
    	
    }
	
	public static void getCertExpired(String httpsUrl) {
		try {
			HttpsURLConnection.setDefaultHostnameVerifier(new CertUtil().new NullHostNameVerifier());
			SSLContext sslContext = SSLContext.getInstance("TLS");
				sslContext.init(null, trustAllCerts, new SecureRandom());
			HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
			URL url = new URL(httpsUrl);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
		        conn.setRequestMethod("GET");
		        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
		        conn.setConnectTimeout(3000);
		        conn.setReadTimeout(5000);
		        conn.connect();
	        Certificate[] certificates= conn.getServerCertificates();    
	        X509Certificate x509Certificate = (X509Certificate)certificates[0];    
	        System.out.println("证书版本:" + x509Certificate.getVersion());
	        System.out.println("证书编号:" + x509Certificate.getSerialNumber());
	        System.out.println("颁发机构:" + x509Certificate.getSubjectDN().getName());
	        System.out.println("颁发者:" + x509Certificate.getIssuerDN().getName());
	        System.out.println("证书开始时间:" + x509Certificate.getNotBefore());
	        System.out.println("有效期止" + x509Certificate.getNotAfter());
	        System.out.println("签名算法:" + x509Certificate.getSigAlgName());
	        System.out.println("证书公钥:" + x509Certificate.getPublicKey());
	        System.out.println("证书签名:" + x509Certificate.getSignature());
	        
	        conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

    static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
    public class NullHostNameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    }
    
    
}

//运行结果

证书版本:3
证书编号:93107898097560507230417716763171864450
颁发机构:CN=baidu.com, OU=service operation department., O="BeiJing Baidu Netcom Science Technology Co., Ltd", L=beijing, ST=beijing, C=CN
颁发者:CN=Symantec Class 3 Secure Server CA - G4, OU=Symantec Trust Network, O=Symantec Corporation, C=US
证书开始时间:Thu Jun 29 08:00:00 CST 2017
有效期止Sat Aug 18 07:59:59 CST 2018
签名算法:SHA256withRSA
证书公钥:Sun RSA public key, 2048 bits
  modulus: 25323137083552840337680365285567518017043090966414919730000096303105755967682312323769813552357674977926071311790901498785916885340572623697449152651795616655633819732133010536056346272158177448114923609565097454266899105159641618365943100969411621107565200221975906329750261571731141551103471932326035634081065879926654235552601838588760411036371026380461155920392227877205367115276228030914150627837450889527407745620739722613899361927463903258445600960377479218000748530613238621235540743168161273988035593117250255871216377672827331013764063630321905013182462661850681425347523159355169035963541625440760510793411
  public exponent: 65537
证书签名:[B@1d9a43d7
java.net.SocketTimeoutException: connect timed out
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:579)
	at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
	at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)
	at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
	at CertUtil.getCertExpired(CertUtil.java:39)
	at CertUtil.main(CertUtil.java:23)




标签:java,sun,____,Https,import,Java,net,public,out
From: https://blog.51cto.com/ratelcloud/7453310

相关文章

  • Python - unix timestamp 时间戳转换错误
    用python的时间转换函数,结果报错。想着这么基础的怎么会报错呢。fromdatetimeimportdatetime#timestampisnumberofsecondssince1970-01-01timestamp=1545730073#convertthetimestamptoadatetimeobjectinthelocaltimezonedt_object=datetime.from......
  • 佛祖保佑 永无bug 永不宕机
    _ooOoo_o8888888o88"."88(|-_-|)O\=/O____/`---'\____.'\\||//`./\\|||:|||//\......
  • Java数据类型详细总结
    ......
  • Maven配置阿里镜像
    1.解决maven配置的境外网站下载jar包慢的问题<mirror><id>nexus-aliyun</id><mirrorOf>central</mirrorOf><name>Nexusaliyun</name><url>http://maven.aliyun.com/nexus/content/groups/public</u......
  • linux下Nginx+Tomcat配置负载均衡
    1.安装tomcat可只本地安装一个tomcat 另一个用远程的    配置好默认直接访问单节点:8080是可以的 2.安装nginx上篇已讲     测试nginx安装成功http://localhost出现nginx主页面即可 3.配置nginx负载均衡    #usernobody;worker_processes1;#error_lo......
  • 15.3K Star,超好用的开源协作式数字白板:tldraw
    大家好,我是TJ今天给大家推荐一个开源协作式数字白板:tldraw。tldraw的编辑器、用户界面和其他底层库都是开源的,你可以在它的开源仓库中找到它们。它们也在NPM上分发,提供开发者使用。您可以使用tlDraw为您的产品创建一个临时白板,或者将其作为构建自己应用的工具来使用。在线体验......
  • Eclipse自定义右键New菜单
    定制new菜单,点Window–>CustomizePerspective:选择new菜单所显示的快捷菜单......
  • PHP中接口interface的作用
    对象接口使用接口(interface),可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。由于接口(interface)和类(class)、trait共享了命名空间,所以它们不能重名。接口就像定义一个标准的类一样,通过interface关键字替换掉class关键字来定义,但其中所有的方法都是空的。接......
  • Nginx(一)Nginx简介
    Nginx(enginex)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功......
  • Java树形菜单_轻量级js树形插件_jsTree树形插件
    //插件效果//代码<!DOCTYPEhtml><html><head><title>JS轻量级树形插件</title><metacharset="utf-8"><linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/def......