@RequestMapping("/updateMobile") @ResponseBody public Object updateMobile(String mobile, String code, HttpSession session) { Map<String, Object> map = new HashMap<String, Object>(); try { String m = (String) session.getAttribute("mobile"); if (!m.equals(mobile)) { map.put("success", false); map.put("message", "验证失败,手机号不一致"); return map; } UserSession userSession = (UserSession) session.getAttribute("userSession"); User user = userSession.getUser(); String smsCode = (String) session.getAttribute("smsCode"); String guid = (String) session.getAttribute("security"); SecurityVerification securityVerification = securityVerificationService.find(guid); Date now = new Date(); long time = now.getTime() - securityVerification.getVerificationTime().getTime(); if (time > securityVerification.getTimeout() * 60 * 1000) { map.put("message", "验证码超时,请重新验证"); map.put("success", true); } else if (code.trim().equals(smsCode)) { user.setMobile(mobile); user.setMobileStatus(true); userService.update(user); securityVerificationService.delete(securityVerification); map.put("message", "验证成功"); map.put("success", true); } map.put("success", true); } catch (Exception e) { LOG.error("手机验证失败", e); map.put("success", false); map.put("message", "未知错误"); } return map; }
public void sendEmail(String toMails, User user) throws MessagingException, UnsupportedEncodingException { GlobalSetting globalSetting = GlobalSetting.getInstance(); SecurityVerification securityVerification = securityVerificationService.findBySecurityVerificationAndType( user.getUid(), SecurityVerification.VERIFICATION_TYPE_EMAIL); if (securityVerification == null) { securityVerification = new SecurityVerification(); securityVerification.setUser(user); } Date now = Calendar.getInstance().getTime(); if (securityVerification.getCode() == null || now.getTime() - securityVerification.getVerificationTime().getTime() > securityVerification.getTimeout() * 60 * 1000) { String code = TokenUtil.getRandomString(8, 2); securityVerification.setValue(toMails); securityVerification.setCode(code); securityVerification.setStatus(SecurityVerification.VERIFICATION_STATUS_FAIL); securityVerification.setTimeout(Constants.EMAIL_TIMEOUT); securityVerification.setVerificationType(SecurityVerification.VERIFICATION_TYPE_EMAIL); securityVerification.setVerificationTime(new Date()); securityVerificationService.update(securityVerification); GlobalSetting setting = GlobalSetting.getInstance(); // 建立邮件消息 MimeMessage mailMessage = setting.getJavaMailSender().createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); // 设置收件人,寄件人 用数组发送多个邮件 messageHelper.setTo(toMails); String nick = javax.mail.internet.MimeUtility.encodeText(globalSetting.getAppName()); messageHelper.setFrom(new InternetAddress(nick + " <" + setting.getSmtpFrom() + ">")); messageHelper.setSubject(globalSetting.getSiteName() + "邮箱验证(请勿回复此邮件)"); messageHelper.setText( "<!doctype html>" + "<html>" + "<head>" + "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" + "<title>祝福之风邮箱验证</title>" + "</head>" + "<body>" + "<div style='margin:0 auto;width:650px;'>" + "<h3>尊敬的用户:</h3>" + "<p>请点击以下地址,完成邮箱验证:</p>" + "<p><a href='http://" + globalSetting.getAppUrl() + "/op/security/verification/goVerifyEmail?uid=" + user.getUid() + "&code=" + URLEncoder.encode(Base64.encode(code.getBytes()), "UTF-8") + "'>http://" + globalSetting.getAppUrl() + "/op/security/verification/goVerifyEmail?uid=" + user.getUid() + "&code=" + URLEncoder.encode(Base64.encode(code.getBytes()), "UTF-8") + "</a></p>" + "<p>此链接有效期为" + Constants.EMAIL_TIMEOUT / 60 + "小时<span style='color:#808080'>(如果您无法点击此链接,请将链接复制到浏览器地址栏后访问)</span>" + "</p>" + "</div>" + "</body>" + "</html>", true); setting.getJavaMailSender().send(mailMessage); } }