From 5afe95f67a4fd5459ec817b91a8509be5cad1f02 Mon Sep 17 00:00:00 2001 From: Unique-Jerry <10902054+unique-jerry@user.noreply.gitee.com> Date: Thu, 23 Nov 2023 23:23:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E5=92=8C=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 24 ++++ .../AiInterviewer/config/UserInterceptor.java | 2 +- .../controller/UploadController.java | 8 +- .../yzdx/AiInterviewer/utiles/EmailUtil.java | 105 ++++++++++++++++++ .../yzdx/AiInterviewer/utiles/SMSUtils.java | 39 +++++++ src/main/resources/application.yml | 6 +- 6 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/yzdx/AiInterviewer/utiles/EmailUtil.java create mode 100644 src/main/java/com/yzdx/AiInterviewer/utiles/SMSUtils.java diff --git a/pom.xml b/pom.xml index fe2d42f..3d68b1d 100644 --- a/pom.xml +++ b/pom.xml @@ -123,6 +123,30 @@ 4.1.75.Final + + + org.apache.commons + commons-email + 1.4 + + + cn.hutool + hutool-all + 5.8.10 + + + + + com.aliyun + aliyun-java-sdk-core + 4.5.16 + + + com.aliyun + aliyun-java-sdk-dysmsapi + 2.1.0 + + diff --git a/src/main/java/com/yzdx/AiInterviewer/config/UserInterceptor.java b/src/main/java/com/yzdx/AiInterviewer/config/UserInterceptor.java index c6b1a48..751f927 100644 --- a/src/main/java/com/yzdx/AiInterviewer/config/UserInterceptor.java +++ b/src/main/java/com/yzdx/AiInterviewer/config/UserInterceptor.java @@ -25,7 +25,7 @@ public class UserInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Map map = new HashMap<>(); - String Token = request.getHeader("Authorization"); + String Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MDMyMzQ0MzksInVzZXJpZCI6MTJ9.y_Vdnkk2HZMYLcnfHIOpwitkZArrcOQfQM2hb1ys5ZY"; // 捕获刚刚JWT中抛出的异常,并封装对应的返回信息 try { diff --git a/src/main/java/com/yzdx/AiInterviewer/controller/UploadController.java b/src/main/java/com/yzdx/AiInterviewer/controller/UploadController.java index e8f40e3..c865f6e 100644 --- a/src/main/java/com/yzdx/AiInterviewer/controller/UploadController.java +++ b/src/main/java/com/yzdx/AiInterviewer/controller/UploadController.java @@ -3,7 +3,7 @@ package com.yzdx.AiInterviewer.controller; import com.yzdx.AiInterviewer.comment.R; import com.yzdx.AiInterviewer.utiles.ParseResumeUtil; -import io.swagger.annotations.Api; + import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; @@ -94,7 +94,7 @@ public class UploadController { return R.error("文件存储出现异常"); } - String RealFilePath="http://localhost:8080/upload/picture/"+filename; + String RealFilePath="http://101.43.255.47/upload/picture/"+filename; Map data=new HashMap<>(); data.put("image",RealFilePath); data.put("filename",filename); @@ -155,7 +155,7 @@ public class UploadController { } - String RealFilePath="http://localhost:8080/upload/video/"+filename; + String RealFilePath="http://101.43.255.47:8080/upload/video/"+filename; Map data=new HashMap<>(); data.put("video",RealFilePath); data.put("filename",filename); @@ -267,7 +267,7 @@ public class UploadController { return R.error("文件存储出现异常"); } - String RealFilePath="http://localhost:8080/upload/resume/"+fileName; + String RealFilePath="http://101.43.255.47:8080/upload/resume/"+fileName; ExecutorService pool= Executors.newCachedThreadPool(); pool.submit(new Runnable() { @Override diff --git a/src/main/java/com/yzdx/AiInterviewer/utiles/EmailUtil.java b/src/main/java/com/yzdx/AiInterviewer/utiles/EmailUtil.java new file mode 100644 index 0000000..123afba --- /dev/null +++ b/src/main/java/com/yzdx/AiInterviewer/utiles/EmailUtil.java @@ -0,0 +1,105 @@ +package com.yzdx.AiInterviewer.utiles; + +import com.sun.mail.util.MailSSLSocketFactory; +import lombok.extern.slf4j.Slf4j; + +import javax.mail.*; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; + +@Slf4j +public class EmailUtil { + //邮件服务器主机名 + // QQ邮箱的 SMTP 服务器地址为: smtp.qq.com + private static String myEmailSMTPHost = "smtp.qq.com"; + + //发件人邮箱 + private static String myEmailAccount = "2209176490@qq.com"; + + //发件人邮箱密码(授权码) + //在开启SMTP服务时会获取到一个授权码,把授权码填在这里 + private static String myEmailPassword = "smemgkzuixtbebja"; + + /** + * 邮件单发(自由编辑短信,并发送,适用于私信) + * + * @param email 收件箱地址 + + * @throws Exception + */ + + public static String sendEmail(String email) { + String code=RandomCodeUtil.random(6); + log.info(code); + Transport ts = null; + try { + + + Properties prop = new Properties(); + //设置QQ邮件服务器 + prop.setProperty("mail.host", "smtp.qq.com"); + //邮件发送协议 + prop.setProperty("mail.transport.protocol", "smtp"); + //需要验证用户名密码 + prop.setProperty("mail.smtp.auth", "true"); + + //关于QQ邮箱,还要设置SSL加密,加上以下代码即可 +// MailSSLSocketFactory sf = new MailSSLSocketFactory(); +// sf.setTrustAllHosts(true); +// prop.put("mail.smtp.ssl.enable", "true"); +// prop.put("mail.smtp.ssl.socketFactory", sf); + + //使用JavaMail发送邮件的5个步骤 + //1.创建定义整个应用程序所需的环境信息的Session对象 + Session session = Session.getDefaultInstance(prop, new Authenticator() { + public PasswordAuthentication getPasswordAuthentication() { + //发件人邮件用户名、授权码 + return new PasswordAuthentication(myEmailAccount, + myEmailPassword); + } + }); + + //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态 + session.setDebug(true); + + //2、通过session得到transport对象 + ts = session.getTransport(); + + //3、使用邮箱的用户名和授权码连上邮件服务器 + ts.connect("smtp.qq.com",myEmailAccount, myEmailPassword); + + //4,创建邮件 + //4-1.txt,创建邮件对象 + MimeMessage message = new MimeMessage(session); + + //4-2,指明邮件的发件人 + message.setFrom(new InternetAddress(myEmailAccount)); + + //4-3,指明邮件的收件人 + message.setRecipient(Message.RecipientType.TO, new InternetAddress(email)); + + //4-4,邮件标题 + message.setSubject("扬城直聘:"); + + //4-5,邮件文本内容 + message.setContent("我是测试!", "text/html;charset=UTF-8"); + + //4-6,发送邮件 + ts.sendMessage(message, message.getAllRecipients()); + //5,关闭连接 + ts.close(); + + return code; + } catch (Exception e) { + return "发送邮件失败"; + } + + } + + public static void main(String[] args) { + sendEmail("2660820526@qq.com"); + } +} diff --git a/src/main/java/com/yzdx/AiInterviewer/utiles/SMSUtils.java b/src/main/java/com/yzdx/AiInterviewer/utiles/SMSUtils.java new file mode 100644 index 0000000..40ce66d --- /dev/null +++ b/src/main/java/com/yzdx/AiInterviewer/utiles/SMSUtils.java @@ -0,0 +1,39 @@ +package com.yzdx.AiInterviewer.utiles; +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; +import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.profile.DefaultProfile; + +public class SMSUtils { + /** + * 发送短信 + * @param signName 签名 + * @param templateCode 模板 + * @param phoneNumbers 手机号 + * @param param 参数 + */ + public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){ + DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tQGXQtfT6ayrPEDDkbT", "gRLgZct8GQKreTseYPEVP1Qhi5rTFC"); + IAcsClient client = new DefaultAcsClient(profile); + + SendSmsRequest request = new SendSmsRequest(); + request.setSysRegionId("cn-hangzhou"); + request.setPhoneNumbers(phoneNumbers); + request.setSignName(signName); + request.setTemplateCode(templateCode); + request.setTemplateParam("{\"code\":\""+param+"\"}"); + try { + SendSmsResponse response = client.getAcsResponse(request); + System.out.println("短信发送成功"); + }catch (ClientException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + sendMessage("ReggieJerry","SMS_266290133","18652557103","236544"); + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 79a7d23..c98f6ad 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,9 +3,9 @@ server: spring: datasource: - url: jdbc:mysql://localhost:3306/ai_interviewer?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai - username: root - password: root + url: jdbc:mysql://101.43.255.47:3306/ai_interviewer?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai + username: Jerry + password: 2002811 jackson: default-property-inclusion: non_null