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%\jre\lib\security目录下,重启web服务即可

原文地址:https://www.cnblogs.com/xiehuazhen/p/9531315.html

时间: 2024-11-15 00:33:51

spring邮件发送需要ssl证书的相关文章

Spring邮件发送

项目中经常用到邮件功能,在这里简单的做一下笔记,方便日后温习. 首先需要在配置文件jdbc.properties添加: #------------ Mail ------------ mail.smtp.auth=true mail.smtp.timeout=25000 mail.smtp.starttls.enable=false 二.配置applicationContext.xml文件 <!-- 邮件发送 --> <bean id="javaMailSender"

Spring邮件发送2

前言:上一篇博文讲解了邮件发送的基础用法(数据是写死的),然而在实际开发中,大多数情况下邮件内容都是根据业务来动态生成的.所以在此篇博文中,我们将讲解邮件发送携带数据的几种方案. 一.解析自定义占位符 实现方法: 通过解析自定义占位符,将传递到邮件中的数据,转换成html内容,进行发送. 1)占位符替换函数 /** * Replaces place holder ("${}") string in template with values in Map * * @param templ

Spring温习--邮件发送实现

Spring对邮件发送支持的很好,我们只要配置好邮件发送器,写好邮件发送具体操作类的方法,那么实现就不是很难的事,不过这个过程中经常会出现这样或那样的错误,所以待讲完本篇后我会总体总结下spring邮件发送容易发生的错误及解决办法 关于邮件发送,不外乎有这么几种情况,一种是纯文本的发送,一种是HTML形式的发送,一种就是发送时带有附件,一种就是发送时采取的事群发,针对这几个我将一一来讲下,我的代码中将第一中归为一大类,后面三种归为一代类,所以将会出现两种配置文件 第一大类邮件发送[纯文本发送]

使用Spring的JAVA Mail支持简化邮件发送(转)

闲来无事,翻看<Spring in Action>,发现Spring集成了对JAVA Mail的支持,有点小激动的看了一遍,嗯,话说真的简单了很多. Spring的邮件发送的核心是MailSender接口,在Spring3.0中提供了一个实现类JavaMailSenderImpl,这个类是发送邮件的核心类.可以通过在配置文件中配置使用,当然也可以自己硬编码到代码中(方便起见,下面的演示代码都是硬编码到代码中,省得配置麻烦). Spring提供的邮件发送不仅支持简单邮件的发送.添加附件,而且还可

spring各种邮件发送

参考地址一 参考地址二 Spring邮件抽象层的主要包为org.springframework.mail.它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc,subject,text. 包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象. 要获得关于邮件异常层次的更丰富的信息,请参考Javadocs. 为了使用JavaMail

使用Spring+观察者模式处理绩效评定流程状态变更及邮件发送

绩效评定流程在每月所有员工绩效计划打分结束后,由系统自动按评定关系表(树状结构,父结点是子结点的评定人,各部门总监为最底层评定人)发起任务,将最底层评定人下属的信息组装为表插入审批表单中,平行的最底层评定人为并行的子任务.最后一个平行最底层评定人评定结束后,系统会自动将新建子任务给平行最底层评定人的评定关系上层,并将所有平行最底层评定人的评定信息汇总作为审批表单,由此迭代直至找到平台级最终评定关系为止,流程结束.由于该流程极其特殊,既不是普通的并行或串行,也不是普通的子任务或会签,除发起结点.结

spring实现java的邮件发送

由于项目需要使用邮件发送,所以最近学习了一下利用spring机制来发送邮件.这里大致记录一下其实现. 首先,需要在项目中导入两个包,一个是mailapi-XX.jar,一个是smtp-xx.jar.其中xx代表版本号.倘若使用maven更简单了,直接添加: <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>

java mail邮件发送(带附件) 支持SSL

java mail邮件发送(带附件)有三个类 MailSenderInfo.java package mail; import java.util.Properties; import java.util.Vector; public class MailSenderInfo { // 发送邮件的server的IP和端口 private String mailServerHost; private String mailServerPort = "25"; // 邮件发送者的地址 pr

Java和Spring邮件的发送

方法一: java发送电子邮件:这里以发送qq邮件为例: package test; import java.util.Properties; import javax.mail.Authenticator;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transpor