spring邮件发送需要ssl证书

Posted 谢哥在彼方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring邮件发送需要ssl证书相关的知识,希望对你有一定的参考价值。

需要将下列代码编译运行:

  1 import java.io.BufferedReader;
  2 import java.io.File;
  3 import java.io.FileInputStream;
  4 import java.io.FileOutputStream;
  5 import java.io.InputStream;
  6 import java.io.InputStreamReader;
  7 import java.io.OutputStream;
  8 import java.security.KeyStore;
  9 import java.security.MessageDigest;
 10 import java.security.cert.CertificateException;
 11 import java.security.cert.X509Certificate;
 12 
 13 import javax.net.ssl.SSLContext;
 14 import javax.net.ssl.SSLException;
 15 import javax.net.ssl.SSLSocket;
 16 import javax.net.ssl.SSLSocketFactory;
 17 import javax.net.ssl.TrustManager;
 18 import javax.net.ssl.TrustManagerFactory;
 19 import javax.net.ssl.X509TrustManager;
 20 
 21 public class InstallCert {
 22 
 23     public static void main(String[] args) throws Exception {
 24         String host;
 25         int port;
 26         char[] passphrase;
 27         if ((args.length == 1) || (args.length == 2)) {
 28             String[] c = args[0].split(":");
 29             host = c[0];
 30             port = (c.length == 1) ? 443 : Integer.parseInt(c[1]);
 31             String p = (args.length == 1) ? "changeit" : args[1];
 32             passphrase = p.toCharArray();
 33         } else {
 34             System.out
 35                     .println("Usage: java InstallCert <host>[:port] [passphrase]");
 36             return;
 37         }
 38 
 39         File file = new File("jssecacerts");
 40         if (file.isFile() == false) {
 41             char SEP = File.separatorChar;
 42             File dir = new File(System.getProperty("java.home") + SEP + "lib"
 43                     + SEP + "security");
 44             file = new File(dir, "jssecacerts");
 45             if (file.isFile() == false) {
 46                 file = new File(dir, "cacerts");
 47             }
 48         }
 49         System.out.println("Loading KeyStore " + file + "...");
 50         InputStream in = new FileInputStream(file);
 51         KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
 52         ks.load(in, passphrase);
 53         in.close();
 54 
 55         SSLContext context = SSLContext.getInstance("TLS");
 56         TrustManagerFactory tmf = TrustManagerFactory
 57                 .getInstance(TrustManagerFactory.getDefaultAlgorithm());
 58         tmf.init(ks);
 59         X509TrustManager defaultTrustManager = (X509TrustManager) tmf
 60                 .getTrustManagers()[0];
 61         SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
 62         context.init(null, new TrustManager[] { tm }, null);
 63         SSLSocketFactory factory = context.getSocketFactory();
 64 
 65         System.out
 66                 .println("Opening connection to " + host + ":" + port + "...");
 67         SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
 68         socket.setSoTimeout(10000);
 69         try {
 70             System.out.println("Starting SSL handshake...");
 71             socket.startHandshake();
 72             socket.close();
 73             System.out.println();
 74             System.out.println("No errors, certificate is already trusted");
 75         } catch (SSLException e) {
 76             System.out.println();
 77             e.printStackTrace(System.out);
 78         }
 79 
 80         X509Certificate[] chain = tm.chain;
 81         if (chain == null) {
 82             System.out.println("Could not obtain server certificate chain");
 83             return;
 84         }
 85 
 86         BufferedReader reader = new BufferedReader(new InputStreamReader(
 87                 System.in));
 88 
 89         System.out.println();
 90         System.out.println("Server sent " + chain.length + " certificate(s):");
 91         System.out.println();
 92         MessageDigest sha1 = MessageDigest.getInstance("SHA1");
 93         MessageDigest md5 = MessageDigest.getInstance("MD5");
 94         for (int i = 0; i < chain.length; i++) {
 95             X509Certificate cert = chain[i];
 96             System.out.println(" " + (i + 1) + " Subject "
 97                     + cert.getSubjectDN());
 98             System.out.println("   Issuer  " + cert.getIssuerDN());
 99             sha1.update(cert.getEncoded());
100             System.out.println("   sha1    " + toHexString(sha1.digest()));
101             md5.update(cert.getEncoded());
102             System.out.println("   md5     " + toHexString(md5.digest()));
103             System.out.println();
104         }
105 
106         System.out
107                 .println("Enter certificate to add to trusted keystore or ‘q‘ to quit: [1]");
108         String line = reader.readLine().trim();
109         int k;
110         try {
111             k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1;
112         } catch (NumberFormatException e) {
113             System.out.println("KeyStore not changed");
114             return;
115         }
116 
117         X509Certificate cert = chain[k];
118         String alias = host + "-" + (k + 1);
119         ks.setCertificateEntry(alias, cert);
120 
121         OutputStream out = new FileOutputStream("jssecacerts");
122         ks.store(out, passphrase);
123         out.close();
124 
125         System.out.println();
126         System.out.println(cert);
127         System.out.println();
128         System.out
129                 .println("Added certificate to keystore ‘jssecacerts‘ using alias ‘"
130                         + alias + "‘");
131     }
132 
133     private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();
134 
135     private static String toHexString(byte[] bytes) {
136         StringBuilder sb = new StringBuilder(bytes.length * 3);
137         for (int b : bytes) {
138             b &= 0xff;
139             sb.append(HEXDIGITS[b >> 4]);
140             sb.append(HEXDIGITS[b & 15]);
141             sb.append(‘ ‘);
142         }
143         return sb.toString();
144     }
145 
146     private static class SavingTrustManager implements X509TrustManager {
147 
148         private final X509TrustManager tm;
149         private X509Certificate[] chain;
150 
151         SavingTrustManager(X509TrustManager tm) {
152             this.tm = tm;
153         }
154 
155         @Override
156         public X509Certificate[] getAcceptedIssuers() {
157             return new X509Certificate[0];
158             //throw new UnsupportedOperationException();  
159         }
160 
161         public void checkClientTrusted(X509Certificate[] chain, String authType)
162                 throws CertificateException {
163             throw new UnsupportedOperationException();
164         }
165 
166         public void checkServerTrusted(X509Certificate[] chain, String authType)
167                 throws CertificateException {
168             this.chain = chain;
169             tm.checkServerTrusted(chain, authType);
170         }
171     }
172 
173 }  

javac InstallCert.java

java InstallCert www.***.com:xx

                                网址       端口

然后将生成的jssecacerts文件放在%JAVA_HOME%jrelibsecurity目录下,重启web服务即可

以上是关于spring邮件发送需要ssl证书的主要内容,如果未能解决你的问题,请参考以下文章

异步发送邮件时的“SSL_read:证书已在哈希表中”

服务器证书 怎么申请 或者 怎么查询

Spring javamailsender 发送 SSL 电子邮件

SSL证书代码签名证书文档签名证书及邮件签名证书有什么区别

Spring(非boot)通过ssl发送邮件

SMTP - SSL 证书问题 - C# - 为啥此代码有效?