Java实现发送手机验证码功能(短信+语音)
2019-04-11 09:52:15来源:博客园 阅读 ()
利用第三方平台可以实现发送手机短信验证码和语音验证码的功能,本文使用框架是struts2+spring+hibernate,现就action层给出核心代码功能。
public class VerifyAction extends BaseAction<VerifyRequest> { private static final long serialVersionUID = 1L; private CommonConfigService commonConfigService; private UserPhoneVerifyService userPhoneVerifyService; private UserVerifyService userVerifyService; private PhoneDetectService phoneDetectService; private AccountService accountService; // 发送短信验证码 public String sensSms() { try { result.setResult(JsonResult.SUCCESS); if (model.getType() == PhoneVerifyType.REGISTER) { if (userVerifyService.checkRegisterPhone(model.getPhone())) { result.setErrorMessage("该手机号已注册"); result.setResult(JsonResult.ERROR); return SUCCESS; } } if (!phoneDetectService.check(model.getPhone())) { result.setErrorMessage("该手机号异常,不能进行验证操作"); result.setResult(JsonResult.ERROR); return SUCCESS; } Dh3TongSmsOperator dh3TongSmsOperator = new Dh3TongSmsOperator( commonConfigService.getString("dh3tong", "account"), DigestUtil.md5(commonConfigService.getString("dh3tong", "password"))); String code = RandomUtil.randomNum(6); if (dh3TongSmsOperator.send(UUIDGenerator.generate(), model.getPhone(), String.format("尊敬的用户,您的验证码是:%s", code), "【XX系统名】")) { userPhoneVerifyService.record(model.getPhone(), model.getType(), code, model.getUserId()); } else { result.setErrorMessage("很抱歉,发送短信失败,请稍后再试"); result.setResult(JsonResult.ERROR); return SUCCESS; } result.setErrorMessage( "短信验证码发送成功,请在5分钟内及时使用,谢谢!"); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } // 发送语音验证码 public String sensVoice() { try { result.setResult(JsonResult.SUCCESS); if (model.getType() == PhoneVerifyType.REGISTER) { if (userVerifyService.checkRegisterPhone(model.getPhone())) { result.setErrorMessage("该手机号已注册"); result.setResult(JsonResult.ERROR); return SUCCESS; } } if (!phoneDetectService.check(model.getPhone())) { result.setErrorMessage("该手机号异常,不能进行验证操作"); result.setResult(JsonResult.ERROR); return SUCCESS; } String code = RandomUtil.randomNum(6); Dh3TongVoiceOperator dh3TongVoiceOperator = new Dh3TongVoiceOperator( commonConfigService.getString("dh3tong", "account"), DigestUtil.md5(commonConfigService.getString("dh3tong", "password"))); if (dh3TongVoiceOperator.send(UUIDGenerator.generate(), request.getPhone(), code)) { userPhoneVerifyService.record(request.getPhone(), request.getType(), code, request.getUserId()); } else { result.setErrorMessage("很抱歉,发送语音失败,请稍后再试"); result.setResult(JsonResult.ERROR); return SUCCESS; } result.setErrorMessage("语音验证码发送成功,请在5分钟内及时使用,谢谢!"); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public void setCommonConfigService(CommonConfigService commonConfigService) { this.commonConfigService = commonConfigService; } public void setUserPhoneVerifyService(UserPhoneVerifyService userPhoneVerifyService) { this.userPhoneVerifyService = userPhoneVerifyService; } public void setUserVerifyService(UserVerifyService userVerifyService) { this.userVerifyService = userVerifyService; } public void setPhoneDetectService(PhoneDetectService phoneDetectService) { this.phoneDetectService = phoneDetectService; } public void setAccountService(AccountService accountService) { this.accountService = accountService; } }
以上是干货分享,已测试验证过!
Java相关学习视频资料分享:
https://www.cnblogs.com/bingyimeiling/p/10289805.html
原文链接:https://www.cnblogs.com/bingyimeiling/p/10685461.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- DES/3DES/AES 三种对称加密算法实现 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash