VerificationCodeService
- package me.zhengjie.system.domain;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import org.hibernate.annotations.CreationTimestamp;
- import javax.persistence.*;
- import javax.validation.constraints.NotBlank;
- import java.sql.Timestamp;
- /**
- * @author jie
- * @date 2018-12-26
- */
- @Data
- @Entity
- @AllArgsConstructor
- @NoArgsConstructor
- @Table(name = "verification_code")
- public class VerificationCode {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- private String code;
- /**
- * 使用场景,自己定义
- */
- private String scenes;
- /**
- * true 为有效,false 为无效,验证时状态+时间+具体的邮箱或者手机号
- */
- private Boolean status = true;
- /**
- * 类型 :phone 和 email
- */
- @NotBlank
- private String type;
- /**
- * 具体的phone与email
- */
- @NotBlank
- private String value;
- /**
- * 创建日期
- */
- @CreationTimestamp
- private Timestamp createTime;
- public VerificationCode(String code, String scenes, @NotBlank String type, @NotBlank String value) {
- this.code = code;
- this.scenes = scenes;
- this.type = type;
- this.value = value;
- }
- }
- package me.zhengjie.system.rest;
- import me.zhengjie.common.utils.ElAdminConstant;
- import me.zhengjie.common.utils.RequestHolder;
- import me.zhengjie.core.security.JwtUser;
- import me.zhengjie.core.utils.JwtTokenUtil;
- import me.zhengjie.system.domain.VerificationCode;
- import me.zhengjie.system.service.VerificationCodeService;
- import me.zhengjie.tools.domain.vo.EmailVo;
- import me.zhengjie.tools.service.EmailService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.web.bind.annotation.*;
- /**
- * @author jie
- * @date 2018-12-26
- */
- @RestController
- @RequestMapping("api")
- public class VerificationCodeController {
- @Autowired
- private VerificationCodeService verificationCodeService;
- @Autowired
- private JwtTokenUtil jwtTokenUtil;
- @Autowired
- @Qualifier("jwtUserDetailsService")
- private UserDetailsService userDetailsService;
- @Autowired
- private EmailService emailService;
- @PostMapping(value = "/code/resetEmail")
- public ResponseEntity resetEmail(@RequestBody VerificationCode code) throws Exception {
- code.setScenes(ElAdminConstant.RESET_MAIL);
- EmailVo emailVo = verificationCodeService.sendEmail(code);
- emailService.send(emailVo,emailService.find());
- return new ResponseEntity(HttpStatus.OK);
- }
- @PostMapping(value = "/code/email/resetPass")
- public ResponseEntity resetPass() throws Exception {
- JwtUser jwtUser = (JwtUser)userDetailsService.loadUserByUsername(jwtTokenUtil.getUserName(RequestHolder.getHttpServletRequest()));
- VerificationCode code = new VerificationCode();
- code.setType("email");
- code.setValue(jwtUser.getEmail());
- code.setScenes(ElAdminConstant.RESET_MAIL);
- EmailVo emailVo = verificationCodeService.sendEmail(code);
- emailService.send(emailVo,emailService.find());
- return new ResponseEntity(HttpStatus.OK);
- }
- @GetMapping(value = "/code/validated")
- public ResponseEntity validated(VerificationCode code){
- verificationCodeService.validated(code);
- return new ResponseEntity(HttpStatus.OK);
- }
- }
- package me.zhengjie.system.service;
- import me.zhengjie.system.domain.VerificationCode;
- import me.zhengjie.tools.domain.vo.EmailVo;
- /**
- * @author jie
- * @date 2018-12-26
- */
- public interface VerificationCodeService {
- /**
- * 发送邮件验证码
- * @param code
- */
- EmailVo sendEmail(VerificationCode code);
- /**
- * 验证
- * @param code
- */
- void validated(VerificationCode code);
- }
- package me.zhengjie.system.service.impl;
- import cn.hutool.core.util.RandomUtil;
- import me.zhengjie.common.exception.BadRequestException;
- import me.zhengjie.common.utils.ElAdminConstant;
- import me.zhengjie.system.domain.VerificationCode;
- import me.zhengjie.system.repository.VerificationCodeRepository;
- import me.zhengjie.system.service.VerificationCodeService;
- import me.zhengjie.tools.domain.vo.EmailVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.Arrays;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- /**
- * @author jie
- * @date 2018-12-26
- */
- @Service
- @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
- public class VerificationCodeServiceImpl implements VerificationCodeService {
- @Autowired
- private VerificationCodeRepository verificationCodeRepository;
- @Value("${code.expiration}")
- private Integer expiration;
- @Override
- @Transactional(rollbackFor = Exception.class)
- public EmailVo sendEmail(VerificationCode code) {
- EmailVo emailVo = null;
- String content = "";
- VerificationCode verificationCode = verificationCodeRepository.findByScenesAndTypeAndValueAndStatusIsTrue(code.getScenes(),code.getType(),code.getValue());
- // 如果不存在有效的验证码,就创建一个新的
- if(verificationCode == null){
- code.setCode(RandomUtil.randomNumbers (6));
- content = ElAdminConstant.EMAIL_CODE + code.getCode() + "</p>";
- emailVo = new EmailVo(Arrays.asList(code.getValue()),"eladmin后台管理系统",content);
- timedDestruction(verificationCodeRepository.save(code));
- // 存在就再次发送原来的验证码
- } else {
- content = ElAdminConstant.EMAIL_CODE + verificationCode.getCode() + "</p>";
- emailVo = new EmailVo(Arrays.asList(verificationCode.getValue()),"eladmin后台管理系统",content);
- }
- return emailVo;
- }
- @Override
- public void validated(VerificationCode code) {
- VerificationCode verificationCode = verificationCodeRepository.findByScenesAndTypeAndValueAndStatusIsTrue(code.getScenes(),code.getType(),code.getValue());
- if(verificationCode == null || !verificationCode.getCode().equals(code.getCode())){
- throw new BadRequestException("无效验证码");
- } else {
- verificationCode.setStatus(false);
- verificationCodeRepository.save(verificationCode);
- }
- }
- /**
- * 定时任务,指定分钟后改变验证码状态
- * @param verifyCode
- */
- private void timedDestruction(VerificationCode verifyCode) {
- //以下示例为程序调用结束继续运行
- ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
- try {
- executorService.schedule(() -> {
- verifyCode.setStatus(false);
- verificationCodeRepository.save(verifyCode);
- }, expiration * 60 * 1000L, TimeUnit.MILLISECONDS);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- package me.zhengjie.system.repository;
- import me.zhengjie.system.domain.VerificationCode;
- import org.springframework.data.jpa.repository.JpaRepository;
- /**
- * @author jie
- * @date 2018-12-26
- */
- public interface VerificationCodeRepository extends JpaRepository<VerificationCode, Long> {
- /**
- * 获取有效的验证码
- * @param scenes 业务场景,如重置密码,重置邮箱等等
- * @param type
- * @param value
- * @return
- */
- VerificationCode findByScenesAndTypeAndValueAndStatusIsTrue(String scenes,String type,String value);
- }
VerificationCodeService的更多相关文章
随机推荐
- 生成私钥、公钥,配置到Git上
ssh-keygen -t rsa -C "1032671220@qq.com" 输入完毕指令,输入密码.然后会在 /z/.ssh/文件夹下生成一个私钥rsa_id.公钥rsa_p ...
- 基于仿生算法的智能系统I
仿生算法仿生算法是什么? 什么是仿生? 蜜蜂会造房子,人类就学习蜜蜂盖房子的方法,之后便有了航空建造工程的蜂窝结构. 仿生是模仿生物系统的功能和行为,来建造技术系统的一种科学方法.生活仿生作品现代的飞 ...
- golang实现单链表
package main import "fmt" type Object interface{} type Node struct { data Object next *Nod ...
- 干货 | 京东云托管Kubernetes集成镜像仓库并部署原生DashBoard
在上一篇"Cloud Native 实操系列"文章中,我们为大家介绍了如何通过京东云原生容器实现Eureka的部署(
- python刷LeetCode:5. 最长回文子串
难度等级:中等 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad"输出: "bab& ...
- Qt5学习笔记(1)-环境配置(win+64bit+VS2013)
Qt5学习笔记(1)-环境配置 工欲善其事必先-不装-所以装软件 久不露面,赶紧打下酱油. 下载 地址:http://download.qt.io/ 这个小网页就可以下载到跟Qt有关的几乎所有大部分东 ...
- cppcheck下载及使用
一.参考文档 1.Ubuntu下安装Cppcheck源码操作步骤 2.cppcheck std.cfg not found error when std.cfg file is available 3 ...
- LeetCode——456.132模式
给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...
- Java线程——线程习题(一)子线程执行10次后,主线程再运行5次,这样交替执行三遍
题目:子线程执行10次后,主线程再运行5次,这样交替执行三遍 代码如下: package com.itheima.gan; /** * 子线程执行10次后,主线程再运行5次,这样交替执行三遍 * @a ...
- javaweb学习——会话技术(二)
文中部分借鉴了:https://www.cnblogs.com/xdp-gacl/p/3855702.html https://blog.csdn.net/p744174529/article/det ...