java web短信验证码_JavaWeb-SpringSecurity使用短信验证码登陆

相关博文

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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包

d2137165985d54ed394c1d53d2266456.png

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

877bba9647f3142f755083b86d7efa83.png

后台result信息输出

f92d1d06071386c91f4458182bb8872f.png

【出现result:1031错误表示短信包没钱了!( ╯□╰ )窘迫~】

当短信发送成功时,result返回值是0,返回值是"OK"

1c0a8f93fe3cfc063d92b513834863d8.png

【当控制台输出result第一个参数为 0 后稍等几秒手机便会接收腾讯云发送的短信】

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

#datasource

spring.datasource.url=jdbc:mysql:///springsecurity?serverTimezone=UTC&characterEncoding=utf-8spring.datasou

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值