2 files modified
2 files added
| | |
| | | |
| | | import com.nq.utils.PropertiesUtil; |
| | | |
| | | import com.nq.utils.email.EmailValidator; |
| | | import com.nq.utils.email.GmailSender; |
| | | import com.nq.utils.redis.CookieUtils; |
| | | |
| | | import com.nq.utils.redis.JsonUtil; |
| | |
| | | return this.iUserService.reg(yzmCode, agentCode, phone, userPwd, httpServletRequest); |
| | | } |
| | | |
| | | //发送邮件 |
| | | @RequestMapping(value = {"sendmail.do"}, method = {RequestMethod.POST}) |
| | | @ResponseBody |
| | | public ServerResponse sendmail(@RequestParam("email") String email,HttpServletRequest request) { |
| | | try { |
| | | if(!EmailValidator.isValidEmail(email)){ |
| | | return ServerResponse.createByErrorMsg("请输入正确的邮箱格式",request); |
| | | } |
| | | String code = GmailSender.generateSecureSixDigitCode(); |
| | | String keys = "emailCode:" + email; |
| | | RedisShardedPoolUtils.setEx(keys, code, 60); |
| | | GmailSender.sendEmail(email,"verification code",code); |
| | | return ServerResponse.createByErrorMsg("发送成功!",request); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return ServerResponse.createByErrorMsg("验证码发送失败",request); |
| | | } |
| | | } |
| | | |
| | | //登录 |
| | | @RequestMapping(value = {"login.do"}, method = {RequestMethod.POST}) |
| | | @ResponseBody |
| | |
| | | import com.nq.pojo.reponse.RUserAssets; |
| | | import com.nq.service.*; |
| | | import com.nq.utils.UserPointUtil; |
| | | import com.nq.utils.email.EmailValidator; |
| | | import com.nq.utils.redis.RedisKeyUtil; |
| | | import com.nq.utils.timeutil.DateTimeUtil; |
| | | import com.nq.utils.PropertiesUtil; |
| | |
| | | StringUtils.isBlank(userPwd) || StringUtils.isBlank(yzmCode)) { |
| | | return ServerResponse.createByErrorMsg("注册失败。该参数不能为空",request); |
| | | } |
| | | if(!EmailValidator.isValidEmail(phone)){ |
| | | return ServerResponse.createByErrorMsg("请输入正确的邮箱格式",request); |
| | | } |
| | | |
| | | |
| | | String keys = "AliyunSmsCode:" + phone; |
| | | String keys = "emailCode:" + phone; |
| | | String redis_yzm = RedisShardedPoolUtils.get(keys); |
| | | |
| | | log.info("redis_yzm = {},yzmCode = {}", redis_yzm, yzmCode); |
| | | if (!yzmCode.equals(redis_yzm) && !"6666".equals(yzmCode)) { |
| | | return ServerResponse.createByErrorMsg("由于验证码不正确,注册失败。过程",request); |
| | | return ServerResponse.createByErrorMsg("验证码不正确或已过期!,注册失败",request); |
| | | } |
| | | |
| | | |
| | | AgentUser agentUser = this.iAgentUserService.findByCode(agentCode); |
| | | if (agentUser == null) { |
| | |
| | | |
| | | public ServerResponse login(String phone, String userPwd, HttpServletRequest request) { |
| | | if (StringUtils.isBlank(phone) || StringUtils.isBlank(userPwd)) { |
| | | return ServerResponse.createByErrorMsg("手机号码和密码不能为空",request); |
| | | return ServerResponse.createByErrorMsg("账号和密码不能为空",request); |
| | | } |
| | | userPwd = SymmetricCryptoUtil.encryptPassword(userPwd); |
| | | User user = this.userMapper.login(phone, userPwd); |
| New file |
| | |
| | | package com.nq.utils.email; |
| | | |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * @program: dabaogp |
| | | * @description: |
| | | * @create: 2025-09-16 12:00 |
| | | **/ |
| | | public class EmailValidator { |
| | | |
| | | |
| | | // 邮箱正则表达式 |
| | | private static final String EMAIL_REGEX = |
| | | "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@" + |
| | | "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; |
| | | |
| | | // 编译正则表达式模式 |
| | | private static final Pattern pattern = Pattern.compile(EMAIL_REGEX); |
| | | |
| | | /** |
| | | * 验证邮箱格式是否有效 |
| | | * @param email 待验证的邮箱地址 |
| | | * @return 如果邮箱格式有效返回true,否则返回false |
| | | */ |
| | | public static boolean isValidEmail(String email) { |
| | | if (email == null) { |
| | | return false; |
| | | } |
| | | |
| | | Matcher matcher = pattern.matcher(email); |
| | | return matcher.matches(); |
| | | } |
| | | |
| | | // 测试方法 |
| | | public static void main(String[] args) { |
| | | // 测试用例 |
| | | String[] testEmails = { |
| | | "test@example.com", |
| | | "user.name@example.com", |
| | | "user.name+tag@example.com", |
| | | "user@sub.example.co.uk", |
| | | "invalid-email", |
| | | "missing@domain", |
| | | "@missingusername.com", |
| | | "user@.com" |
| | | }; |
| | | |
| | | for (String email : testEmails) { |
| | | System.out.println(email + " : " + (isValidEmail(email) ? "有效" : "无效")); |
| | | } |
| | | } |
| | | |
| | | } |
| New file |
| | |
| | | package com.nq.utils.email; |
| | | |
| | | import javax.mail.*; |
| | | import javax.mail.internet.InternetAddress; |
| | | import javax.mail.internet.MimeMessage; |
| | | import java.security.SecureRandom; |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | | * @program: dabaogp |
| | | * @description: |
| | | * @create: 2025-09-16 13:44 |
| | | **/ |
| | | public class GmailSender { |
| | | |
| | | public static void sendEmail(String to, String subject, String body) { |
| | | // 发件人Gmail地址 |
| | | String from = "coinzne.com@gmail.com"; |
| | | // 使用应用专用密码,而不是常规的Gmail密码 |
| | | String password = "pqupwyxoqedhxlfq"; |
| | | |
| | | // 设置邮件服务器属性 |
| | | Properties props = new Properties(); |
| | | props.put("mail.smtp.host", "smtp.gmail.com"); |
| | | props.put("mail.smtp.port", "587"); |
| | | props.put("mail.smtp.auth", "true"); |
| | | props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密 |
| | | |
| | | // 获取Session对象 |
| | | Session session = Session.getInstance(props, new javax.mail.Authenticator() { |
| | | protected PasswordAuthentication getPasswordAuthentication() { |
| | | return new PasswordAuthentication(from, password); |
| | | } |
| | | }); |
| | | |
| | | try { |
| | | // 创建MimeMessage对象 |
| | | Message message = new MimeMessage(session); |
| | | |
| | | // 设置发件人 |
| | | message.setFrom(new InternetAddress(from)); |
| | | |
| | | // 设置收件人 |
| | | message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); |
| | | |
| | | // 设置邮件主题 |
| | | message.setSubject(subject); |
| | | |
| | | // 设置邮件正文 |
| | | message.setText(body); |
| | | |
| | | // 发送邮件 |
| | | Transport.send(message); |
| | | |
| | | System.out.println("邮件发送成功!"); |
| | | |
| | | } catch (MessagingException e) { |
| | | throw new RuntimeException("邮件发送失败: " + e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 生成6位随机数字验证码(使用安全随机数生成器) |
| | | * @return 6位数字字符串 |
| | | */ |
| | | public static String generateSecureSixDigitCode() { |
| | | SecureRandom random = new SecureRandom(); |
| | | int code = random.nextInt(900000) + 100000; |
| | | return String.valueOf(code); |
| | | } |
| | | } |