parent
05e9786c62
commit
67dba3d9dd
24 changed files with 886 additions and 57 deletions
@ -0,0 +1,33 @@ |
||||
package com.msdw.tms.config; |
||||
|
||||
import com.msdw.tms.config.quartz.CheckCodeQuartz; |
||||
import org.quartz.*; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
@Configuration |
||||
public class CheckCodeQuartzConfig { |
||||
//定义任务详情
|
||||
@Bean |
||||
public JobDetail orderjobDetail() { |
||||
//指定job的名称和持久化保存任务
|
||||
return JobBuilder |
||||
.newJob(CheckCodeQuartz.class) //自定义任务
|
||||
.withIdentity("checkCodeQuartz") //为任务定义名称
|
||||
.storeDurably() |
||||
.build(); |
||||
} |
||||
//定义触发器
|
||||
@Bean |
||||
public Trigger orderTrigger() { |
||||
//通知调度器,程序多久执行一次
|
||||
CronScheduleBuilder scheduleBuilder |
||||
// = CronScheduleBuilder.cronSchedule("5/20 * * ? * *"); //时间表达式 每分钟的5,25秒执行任务
|
||||
= CronScheduleBuilder.cronSchedule("0 0 18 * * ? "); //时间表达式,每天的18点执行任务
|
||||
return TriggerBuilder |
||||
.newTrigger() |
||||
.forJob(orderjobDetail()) |
||||
.withIdentity("checkCodeQuartz") |
||||
.withSchedule(scheduleBuilder).build(); |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
package com.msdw.tms.config; |
||||
|
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
import java.util.Random; |
||||
|
||||
public class CheckCodeUtils { |
||||
|
||||
/** |
||||
* 获取六位随机数验证码 |
||||
* @return |
||||
*/ |
||||
public String sendCode(){ |
||||
|
||||
Random random = new Random(); |
||||
String code=""; |
||||
for (int i=0;i<6;i++) |
||||
{ |
||||
code+=random.nextInt(10); |
||||
} |
||||
return code; |
||||
} |
||||
|
||||
/** |
||||
* 生成时间 |
||||
*/ |
||||
public String letTimes(int letTime){ |
||||
|
||||
Calendar instance = Calendar.getInstance(); |
||||
instance.add(Calendar.MINUTE,+letTime); |
||||
Date time = instance.getTime(); |
||||
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); |
||||
String format1 = myFmt.format(time); |
||||
|
||||
return format1; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,52 @@ |
||||
package com.msdw.tms.config; |
||||
|
||||
import org.apache.commons.mail.HtmlEmail; |
||||
|
||||
import java.util.Random; |
||||
|
||||
/** |
||||
* 邮箱验证工具 |
||||
*/ |
||||
public class EmailUtils { |
||||
|
||||
//服务器地址
|
||||
private static final String SERVER_ADDRESS = "smtp.163.com"; |
||||
//邮箱地址
|
||||
private static final String EMAILADDRESS = "mzh820631607@163.com"; |
||||
//发送人姓名
|
||||
private static final String EMAILUSERNAME = "michonne"; |
||||
//授权码
|
||||
private static final String AUTHORIZATION_CODE = "ZPAASKPCYJDIFFDR"; |
||||
//邮件标题
|
||||
private static final String EMAILHEAD = "京喜"; |
||||
//邮件信息(后接验证码)头部信息
|
||||
private static final String EMAILMASSAGEHEAD = "尊敬的用户您好,您本次注册的验证码是: "; |
||||
//邮件信息(后接验证码)尾部信息
|
||||
private static final String EMAILMASSAGETAIL = ",有效时间为5分钟"; |
||||
|
||||
|
||||
//邮箱验证码
|
||||
public static boolean sendEmail(String emailaddress,String code){ |
||||
try { |
||||
HtmlEmail email = new HtmlEmail(); |
||||
email.setHostName(SERVER_ADDRESS); |
||||
email.setCharset("UTF-8"); |
||||
email.addTo(emailaddress); |
||||
|
||||
email.setFrom(EMAILADDRESS, EMAILUSERNAME); |
||||
|
||||
email.setAuthentication(EMAILADDRESS, AUTHORIZATION_CODE); |
||||
|
||||
email.setSubject(EMAILHEAD); |
||||
email.setMsg( EMAILMASSAGEHEAD + code + EMAILMASSAGETAIL); |
||||
|
||||
email.send(); |
||||
return true; |
||||
} |
||||
catch(Exception e){ |
||||
e.printStackTrace(); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,91 @@ |
||||
package com.msdw.tms.config; |
||||
|
||||
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.http.MethodType; |
||||
import com.aliyuncs.profile.DefaultProfile; |
||||
import com.aliyuncs.profile.IClientProfile; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
@Slf4j |
||||
public class SendSMS { |
||||
|
||||
// 短信模板id,此处将其封装为变量,是为了由该方法的调用者决定自己来发送哪种类型的短信验证码,比如注册,重置密码类的
|
||||
private static final String PHONSMSTEMPLATE = "SMS_204155294"; |
||||
|
||||
public static void main(String[] args){ |
||||
String mobile="17376370737";//需要接受短信验证码的手机号
|
||||
// String sendTemplate="SMS_204155294";//短信模板ID,需要去阿里云平台申请,审核通过后方能使用,通常是以"SMS_"开头字符串
|
||||
String code = (int)((Math.random() * 9 + 1) * 100000)+ "";//要发送给用户的短信验证码,行业通常使用六位纯数字
|
||||
|
||||
boolean result= sendCode(mobile,code); |
||||
if(result){ |
||||
System.out.println("短信发送成功"); |
||||
}else{ |
||||
System.out.println("短信发送失败"); |
||||
} |
||||
|
||||
} |
||||
/** |
||||
* 通过阿里云平台发送短信,如果发送成功,返回"OK" ;否则返回的是错误信息 |
||||
* @param mobile 要接收该短信的用户手机号 |
||||
* @param |
||||
* @param code 您将发送到用户手机的短信验证码 |
||||
*/ |
||||
public static Boolean sendCode(String mobile,String code){ |
||||
boolean isSend = false; |
||||
//请求失败这里会抛ClientException异常
|
||||
String result=""; |
||||
try { |
||||
//设置超时时间-可自行调整
|
||||
System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); |
||||
System.setProperty("sun.net.client.defaultReadTimeout", "10000"); |
||||
//初始化ascClient需要的几个参数
|
||||
final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改)
|
||||
final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改)
|
||||
//替换成你的AK
|
||||
final String accessKeyId = "LTAI4FzqQHnk4rozqLZ8jCNj";//你的accessKeyId,参考本文档步骤2
|
||||
final String accessKeySecret = "mveW7B1OyFoKUkHm8WsxmrjHmkJWHq";//你的accessKeySecret,参考本文档步骤2
|
||||
//初始化ascClient,暂时不支持多region(请勿修改)
|
||||
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, |
||||
accessKeySecret); |
||||
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); |
||||
IAcsClient acsClient = new DefaultAcsClient(profile); |
||||
//组装请求对象
|
||||
SendSmsRequest request = new SendSmsRequest(); |
||||
//使用post提交
|
||||
request.setMethod(MethodType.POST); |
||||
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
|
||||
request.setPhoneNumbers(mobile); |
||||
//必填:短信签名-可在短信控制台中找到
|
||||
request.setSignName("职站");//注意要与你在短信控制台中的签名一致
|
||||
//必填:短信模板-可在短信控制台中找到
|
||||
request.setTemplateCode(PHONSMSTEMPLATE); |
||||
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
||||
//友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
|
||||
// request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");
|
||||
request.setTemplateParam("{\"code\":\""+code+"\"}"); |
||||
//可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
|
||||
//request.setSmsUpExtendCode("90997");
|
||||
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
|
||||
// request.setOutId("yourOutId");
|
||||
|
||||
SendSmsResponse response = acsClient.getAcsResponse(request); |
||||
result=response.getCode(); |
||||
if(result!=null&&"OK".equals(result.toUpperCase())){ |
||||
System.out.println("短信发送成功"); |
||||
isSend = true; |
||||
}else{ |
||||
isSend = false; |
||||
System.out.println("短信发送失败"); |
||||
} |
||||
} catch (Exception e) { |
||||
result=e.getMessage(); |
||||
isSend = false; |
||||
} |
||||
return isSend; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,24 @@ |
||||
package com.msdw.tms.config.quartz; |
||||
|
||||
import com.msdw.tms.service.UserService; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.quartz.Job; |
||||
import org.quartz.JobExecutionContext; |
||||
import org.quartz.JobExecutionException; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
public class CheckCodeQuartz implements Job { |
||||
|
||||
@Autowired |
||||
private UserService userService; |
||||
|
||||
@Override |
||||
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { |
||||
boolean clean = userService.cleanUp(); |
||||
log.info("清理过期验证码"); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
package com.msdw.tms.entity.vo; |
||||
|
||||
import lombok.Data; |
||||
import lombok.experimental.Accessors; |
||||
|
||||
@Data |
||||
@Accessors(chain = true) |
||||
public class CheckVo { |
||||
//用户id
|
||||
private Integer userId; |
||||
//用户的邮箱地址
|
||||
private String email; |
||||
//验证码
|
||||
private String checkCode; |
||||
//用户电话号码
|
||||
private String phone; |
||||
//验证码存入时间(实际时间-允许验证时间)
|
||||
private String setTime; |
||||
//验证码类型(1邮箱验证码,2手机验证码)
|
||||
private Integer types; |
||||
|
||||
//用户输入的验证码
|
||||
private String code; |
||||
//用户设置的新邮箱地址
|
||||
private String emailAddress; |
||||
//用户设置的新手机号
|
||||
private String mobile; |
||||
|
||||
} |
@ -0,0 +1,27 @@ |
||||
package com.msdw.tms; |
||||
|
||||
import com.msdw.tms.config.EmailUtils; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
||||
import java.util.Random; |
||||
|
||||
@SpringBootTest |
||||
public class EmaiTest { |
||||
|
||||
@Test |
||||
public void sendEmailTest(){ |
||||
String emailaddress = "820631607@qq.com"; |
||||
|
||||
//获取六位随机数验证码
|
||||
Random random = new Random(); |
||||
String code=""; |
||||
for (int i=0;i<6;i++) |
||||
{ |
||||
code+=random.nextInt(10); |
||||
} |
||||
System.out.println(code); |
||||
boolean b = EmailUtils.sendEmail(emailaddress, code); |
||||
System.out.println(b?"发送成功":"发送失败"); |
||||
} |
||||
} |
Loading…
Reference in new issue