-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
注册加入邮箱验证,验证码验证功能
- Loading branch information
Showing
15 changed files
with
527 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.tomoya.module.code.dao; | ||
|
||
import cn.tomoya.module.code.entity.Code; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by tomoya on 17-6-6. | ||
*/ | ||
@Repository | ||
public interface CodeDao extends JpaRepository<Code, Integer> { | ||
|
||
List<Code> findByCodeAndType(String code, String type); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cn.tomoya.module.code.entity; | ||
|
||
import javax.persistence.*; | ||
import java.util.Date; | ||
|
||
/** | ||
* Created by tomoya on 17-6-6. | ||
*/ | ||
@Entity | ||
@Table(name = "pybbs_code") | ||
public class Code { | ||
|
||
@Id | ||
@GeneratedValue | ||
private int id; | ||
|
||
@Column(unique = true) | ||
private String code; | ||
|
||
@Column(name = "expire_time") | ||
private Date expireTime; | ||
|
||
private String type; | ||
|
||
@Column(name = "is_used") | ||
private boolean isUsed; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public Date getExpireTime() { | ||
return expireTime; | ||
} | ||
|
||
public void setExpireTime(Date expireTime) { | ||
this.expireTime = expireTime; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public boolean isUsed() { | ||
return isUsed; | ||
} | ||
|
||
public void setUsed(boolean used) { | ||
isUsed = used; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cn.tomoya.module.code.entity; | ||
|
||
/** | ||
* Created by tomoya on 17-6-6. | ||
*/ | ||
public enum CodeEnum { | ||
|
||
EMAIL, | ||
SMS | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/cn/tomoya/module/code/service/CodeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cn.tomoya.module.code.service; | ||
|
||
import cn.tomoya.module.code.dao.CodeDao; | ||
import cn.tomoya.module.code.entity.Code; | ||
import cn.tomoya.module.code.entity.CodeEnum; | ||
import cn.tomoya.util.DateUtil; | ||
import cn.tomoya.util.StrUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by tomoya on 17-6-6. | ||
*/ | ||
@Service | ||
@Transactional | ||
public class CodeService { | ||
|
||
@Autowired | ||
private CodeDao codeDao; | ||
|
||
public Code findByCodeAndType(String code, CodeEnum type) { | ||
List<Code> codes = codeDao.findByCodeAndType(code, type.name()); | ||
if(codes.size() > 0) return codes.get(0); | ||
return null; | ||
} | ||
|
||
public void save(Code code) { | ||
codeDao.save(code); | ||
} | ||
|
||
public String genEmailCode() { | ||
String genCode = StrUtil.randomString(6); | ||
Code code = findByCodeAndType(genCode, CodeEnum.EMAIL); | ||
if(code != null) { | ||
return genEmailCode(); | ||
} else { | ||
code = new Code(); | ||
code.setCode(genCode); | ||
code.setExpireTime(DateUtil.getMinuteAfter(new Date(), 10)); | ||
code.setType(CodeEnum.EMAIL.name()); | ||
code.setUsed(false); | ||
save(code); | ||
return genCode; | ||
} | ||
} | ||
|
||
public int validateCode(String code, CodeEnum type) { | ||
Code code1 = findByCodeAndType(code, type); | ||
if(code1 == null) return 1;// 验证码不正确 | ||
if(DateUtil.isExpire(code1.getExpireTime())) return 2; // 过期了 | ||
code1.setUsed(true); | ||
save(code1); | ||
return 0; // 正常 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,26 @@ | |
|
||
import cn.tomoya.common.BaseController; | ||
import cn.tomoya.common.config.SiteConfig; | ||
import cn.tomoya.exception.ApiException; | ||
import cn.tomoya.exception.Result; | ||
import cn.tomoya.module.code.entity.Code; | ||
import cn.tomoya.module.code.entity.CodeEnum; | ||
import cn.tomoya.module.code.service.CodeService; | ||
import cn.tomoya.module.topic.entity.Topic; | ||
import cn.tomoya.module.topic.service.TopicService; | ||
import cn.tomoya.module.user.entity.User; | ||
import cn.tomoya.module.user.service.UserService; | ||
import cn.tomoya.util.FileUploadEnum; | ||
import cn.tomoya.util.FileUtil; | ||
import cn.tomoya.util.PageWrapper; | ||
import cn.tomoya.util.StrUtil; | ||
import cn.tomoya.util.identicon.Identicon; | ||
import org.apache.log4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
|
@@ -40,6 +49,8 @@ | |
@Controller | ||
public class IndexController extends BaseController { | ||
|
||
private Logger log = Logger.getLogger(IndexController.class); | ||
|
||
@Autowired | ||
private TopicService topicService; | ||
@Autowired | ||
|
@@ -50,6 +61,12 @@ public class IndexController extends BaseController { | |
private Identicon identicon; | ||
@Autowired | ||
private FileUtil fileUtil; | ||
@Autowired | ||
private JavaMailSender javaMailSender; | ||
@Autowired | ||
private CodeService codeService; | ||
@Autowired | ||
private Environment env; | ||
|
||
/** | ||
* 首页 | ||
|
@@ -75,6 +92,7 @@ public String index(String tab, Integer p, Model model) { | |
|
||
/** | ||
* 搜索 | ||
* | ||
* @param p | ||
* @param q | ||
* @param model | ||
|
@@ -123,30 +141,57 @@ public String toRegister(HttpServletResponse response) { | |
* @return | ||
*/ | ||
@PostMapping("/register") | ||
public String register(String username, String password, HttpServletResponse response, Model model) { | ||
public String register(String username, String password, String email, String emailCode, String code, | ||
HttpSession session, HttpServletResponse response, Model model) { | ||
String genCaptcha = (String) session.getAttribute("index_code"); | ||
if (StringUtils.isEmpty(code)) { | ||
model.addAttribute("errors", "验证码不能为空"); | ||
return render("/register"); | ||
} | ||
if (!genCaptcha.toLowerCase().equals(code.toLowerCase())) { | ||
model.addAttribute("errors", "验证码错误"); | ||
return render("/register"); | ||
} | ||
User user = userService.findByUsername(username); | ||
if (user != null) { | ||
model.addAttribute("errors", "用户名已经被注册"); | ||
} else if (StringUtils.isEmpty(username)) { | ||
return render("/register"); | ||
} | ||
if (StringUtils.isEmpty(username)) { | ||
model.addAttribute("errors", "用户名不能为空"); | ||
} else if (StringUtils.isEmpty(password)) { | ||
return render("/register"); | ||
} | ||
if (StringUtils.isEmpty(password)) { | ||
model.addAttribute("errors", "密码不能为空"); | ||
} else { | ||
Date now = new Date(); | ||
String avatarName = UUID.randomUUID().toString(); | ||
identicon.generator(avatarName); | ||
user = new User(); | ||
user.setUsername(username); | ||
user.setPassword(new BCryptPasswordEncoder().encode(password)); | ||
user.setInTime(now); | ||
user.setBlock(false); | ||
user.setToken(UUID.randomUUID().toString()); | ||
user.setAvatar(siteConfig.getStaticUrl() + "avatar/" + avatarName + ".png"); | ||
user.setAttempts(0); | ||
userService.save(user); | ||
return redirect(response, "/login?s=reg"); | ||
return render("/register"); | ||
} | ||
return render("/register"); | ||
User user_email = userService.findByEmail(email); | ||
if (user_email != null) { | ||
model.addAttribute("errors", "邮箱已经被使用"); | ||
return render("/register"); | ||
} | ||
int validateResult = codeService.validateCode(emailCode, CodeEnum.EMAIL); | ||
if(validateResult == 1) { | ||
model.addAttribute("errors", "邮箱验证码不正确"); | ||
return render("/register"); | ||
} | ||
if(validateResult == 2) { | ||
model.addAttribute("errors", "邮箱验证码已过期"); | ||
return render("/register"); | ||
} | ||
Date now = new Date(); | ||
String avatarName = UUID.randomUUID().toString(); | ||
identicon.generator(avatarName); | ||
user = new User(); | ||
user.setUsername(username); | ||
user.setPassword(new BCryptPasswordEncoder().encode(password)); | ||
user.setInTime(now); | ||
user.setBlock(false); | ||
user.setToken(UUID.randomUUID().toString()); | ||
user.setAvatar(siteConfig.getStaticUrl() + "avatar/" + avatarName + ".png"); | ||
user.setAttempts(0); | ||
userService.save(user); | ||
return redirect(response, "/login?s=reg"); | ||
} | ||
|
||
/** | ||
|
@@ -192,6 +237,7 @@ public String wangEditorUpload(@RequestParam("file") MultipartFile file) { | |
|
||
/** | ||
* 关于 | ||
* | ||
* @param model | ||
* @return | ||
*/ | ||
|
@@ -207,11 +253,12 @@ public String about(Model model) { | |
private int xx = 22; | ||
private int fontHeight = 26; | ||
private int codeY = 25; | ||
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', | ||
'V', 'W', 'X', 'Y', '3', '4', '5', '6', '7', '8' }; | ||
char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', | ||
'V', 'W', 'X', 'Y', '3', '4', '5', '6', '7', '8'}; | ||
|
||
/** | ||
* 验证码生成 | ||
* | ||
* @param req | ||
* @param resp | ||
* @throws IOException | ||
|
@@ -281,4 +328,24 @@ public void getCode(HttpServletRequest req, HttpServletResponse resp) throws IOE | |
sos.close(); | ||
} | ||
|
||
@GetMapping("/sendEmailCode") | ||
@ResponseBody | ||
public Result sendEmailCode(String email) throws ApiException { | ||
if (!StrUtil.isEmail(email)) throw new ApiException("请输入正确的Email"); | ||
try { | ||
String genCode = codeService.genEmailCode(); | ||
SimpleMailMessage message = new SimpleMailMessage(); | ||
System.out.println(env.getProperty("spring.mail.username")); | ||
message.setFrom(env.getProperty("spring.mail.username")); | ||
message.setTo("[email protected]"); | ||
message.setSubject("注册验证码 - " + siteConfig.getName()); | ||
message.setText("你的验证码为: " + genCode + " , 请在10分钟内使用!"); | ||
javaMailSender.send(message); | ||
return Result.success(); | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
return Result.error("邮件发送失败"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.