相关博文
JavaWeb-SpringBoot_一个类实现腾讯云SDK发送短信 传送门
系列博文
项目已上传至guthub 传送门
JavaWeb-SpringSecurity初认识 传送门
JavaWeb-SpringSecurity在数据库中查询登陆用户 传送门
JavaWeb-SpringSecurity自定义登陆页面 传送门
JavaWeb-SpringSecurity实现需求-判断请求是否以html结尾 传送门
JavaWeb-SpringSecurity自定义登陆配置 传送门
JavaWeb-SpringSecurity图片验证ImageCode 传送门
JavaWeb-SpringSecurity记住我功能 传送门
JavaWeb-SpringSecurity使用短信验证码登陆 传送门
在项目GaryRESTful.validate.code包下创建SmsCode.java
packagecom.Gary.GaryRESTful.validate.code;importjava.time.LocalDateTime;public classSmsCode {//发送短信code
privateString code;//当前系统时间
privateLocalDateTime expireTime;public SmsCode(String code,intexprieTime)
{this.code =code;this.expireTime =LocalDateTime.now().plusSeconds(exprieTime);
}publicSmsCode(String code,LocalDateTime expireTime)
{this.code =code;this.expireTime =expireTime;
}//判断当前时间是否存在过期之后
public booleanisExpired()
{returnLocalDateTime.now().isAfter(expireTime);
}publicString getCode() {returncode;
}public voidsetCode(String code) {this.code =code;
}publicLocalDateTime getExpireTime() {returnexpireTime;
}public voidsetExpireTime(LocalDateTime expireTime) {this.expireTime =expireTime;
}
}
SmsCode.java
优化ImageCode.java与SmsCode.java,将SmsCode.java重命名为ValidateCode.java,并用ImageCode.java继承ValidateCode.java
packagecom.Gary.GaryRESTful.validate.code;importjava.time.LocalDateTime;public classValidateCode {//发送短信code
privateString code;//当前系统时间
privateLocalDateTime expireTime;public ValidateCode(String code,intexpireTime)
{this.code =code;this.expireTime =LocalDateTime.now().plusSeconds(expireTime);
}publicValidateCode(String code,LocalDateTime expireTime)
{this.code =code;this.expireTime =expireTime;
}//判断当前时间是否存在过期之后
public booleanisExpired()
{returnLocalDateTime.now().isAfter(expireTime);
}publicString getCode() {returncode;
}public voidsetCode(String code) {this.code =code;
}publicLocalDateTime getExpireTime() {returnexpireTime;
}public voidsetExpireTime(LocalDateTime expireTime) {this.expireTime =expireTime;
}
}
ValidateCode.java
导入腾讯云发短信qcloudsms-1.0.5.jar包
ValidateCodeController.java中实现接收发送短信验证码的请求@GetMapping("/code/sms")
@GetMapping("/code/sms")public void createSmsCode(HttpServletRequest request,HttpServletResponse response) throwsServletRequestBindingException
{//生成短信的校验码
ValidateCode smsCode =createSmsCode();//将我们的校验码放入session域中
sessionStrategy.setAttribute(newServletWebRequest(request), sessionSmsKey, smsCode);//从request域中获取手机号
String mobile = ServletRequestUtils.getRequiredStringParameter(request, "mobile");//发短信(给mobile手机号发送smsCode验证码)
sendSms(mobile,smsCode.getCode());
}//发短信(给mobile手机号发送smsCode验证码)
private voidsendSms(String mobile, String code) {//1.腾讯云自己项目的AppID
int appid = 1400182502;
String appkey= "58f61b731363faba756087b9504bff46";int templateId =193928;
String smsSign= "Garyd公众号";
String phoneNumber=mobile;
String[] params= new String[1];
params[0] =code;//将验证码打印出来
System.out.println("验证码: "+code);
SmsSingleSender sender= newSmsSingleSender(appid,appkey);//86,手机号,模板id,验证码,smsSign
try{
SmsSingleSenderResult result= sender.sendWithParam("86", phoneNumber, templateId, params, smsSign, "", "");//打印是否发送成功
System.out.println(result);
}catch (JSONException | HTTPException |IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}//生成短信的校验码
privateValidateCode createSmsCode() {
Random r= newRandom();
String code= "" +r.nextInt(10)+r.nextInt(10)+r.nextInt(10)+r.nextInt(10);return new ValidateCode(code,120);
}
别忘了在SecurityConfig.java中的configure()方法中打开对/code/sms请求的拦截
//在访问我们的URL时,我们是不需要省份认证,可以立即访问
.antMatchers("/login.html","/require","/code/image","/code/sms").permitAll()
测试
发送验证码请求 localhost:8080/code/sms?mobile=17689470428
后台result信息输出
【出现result:1031错误表示短信包没钱了!( ╯□╰ )窘迫~】
当短信发送成功时,result返回值是0,返回值是"OK"
【当控制台输出result第一个参数为 0 后稍等几秒手机便会接收腾讯云发送的短信】
#datasource
spring.datasource.url=jdbc:mysql:///springsecurity?serverTimezone=UTC&characterEncoding=utf-8spring.datasou