实现邮箱注册需要发送电子邮件,使用spring发送电子邮件会使用到两个类,JavaMailSenderImpl这个类用来发送电子邮件,org.springframework.mail.SimpleMailMessage这个类用来封装邮件信息
还需要javamail的jar包。所以首先需要添加依赖
javax.mail
mail
1.4.7
还需要配置以上两个类
applicationContext-mail.xml
true
javax.net.ssl.SSLSocketFactory
465
接下来编码实现邮箱注册
public void sendActivationMail(String mailTo,String activationCode);
@Resource
private MailSender mailSender;
@Resource
private SimpleMailMessage mailMessage;
public void sendActivationMail(String mailTo, String activationCode) {
mailMessage.setTo(mailTo);
mailMessage.setText("您的注册邮箱:"+mailTo+"激活码:"+activationCode);
mailSender.send(mailMessage);
}
/**
* 使用邮箱注册用户
* @param user
* @throws Exception *
* */
public void itriptxCreateUser(ItripUser user) throws Exception;
public void itriptxCreateUser(ItripUser user) throws Exception {
//生成激活码
String activationCode = MD5.getMd5(new Date().toLocaleString(),32);
//发送邮件
mailService.sendActivationMail(user.getUserCode(),activationCode);
//保存激活码到Redis
redisAPI.set("activition:"+user.getUserCode(),30*60,activationCode);
//保存用户信息
itripUserMapper.insertItripUser(user);
}
@RequestMapping(value="/doregister",method=RequestMethod.POST,
produces = "application/json")
public @ResponseBody Dto doRegister(@RequestBody ItripUserVO userVO) {
if(!validEmail(userVO.getUserCode()))
return DtoUtil.returnFail("请使用正确的邮箱地址注册",ErrorCode.AUTH_ILLEGAL_USERCODE);
try {
ItripUser user=new ItripUser();
user.setUserCode(userVO.getUserCode());
user.setUserPassword(userVO.getUserPassword());
user.setUserType(0);
user.setUserName(userVO.getUserName());
if (null == userService.findByUsername(user.getUserCode())) {
user.setUserPassword(MD5.getMd5(user.getUserPassword(), 32));
userService.itriptxCreateUser(user);
return DtoUtil.returnSuccess();
}else
{
return DtoUtil.returnFail("用户已存在,注册失败", ErrorCode.AUTH_USER_ALREADY_EXISTS);
}
} catch (Exception e) {
e.printStackTrace();
return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
}
}
/** *
* 合法E-mail地址:
* 1. 必须包含一个并且只有一个符号“@”
* 2. 第一个字符不得是“@”或者“.”
* 3. 不允许出现“@.”或者.@
* 4. 结尾不得是字符“@”或者“.”
* 5. 允许“@”前的字符中出现“+”
* 6. 不允许“+”在最前面,或者“+@”
*/
private boolean validEmail(String email){
String regex="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$" ;
return Pattern.compile(regex).matcher(email).find();
}
下面需要使用激活码激活用户
/**
* 邮箱激活
* @param email 用户注册油箱
* @param code 激活码
* @return
* @throws Exception
*/
public boolean activate(String email,String code) throws Exception;
@Override
public boolean activate(String email, String code) throws Exception {
//比对激活码
if(redisAPI.exist("activition:"+email)){
if(redisAPI.get("activition:"+email).equals(code)){
ItripUser user = this.findByUsername(email);
if(EmptyUtils.isNotEmpty(user)){
//更新用户
user.setFlatID(user.getId());
user.setActivated(1); //平台ID(根据不同登录用户,进行相应存入:自注册用户主键ID、微信ID、QQID、微博ID)
user.setUserType(0);//用户类型(标识:0 自注册用户 1 微信登录 2 QQ登录 3 微博登录)
itripUserMapper.updateItripUser(user);
return true;
}
}
}
return false;
}
@RequestMapping(value="/activate",method=RequestMethod.PUT,produces= "application/json")
public @ResponseBody Dto activate(@RequestParam String user,@RequestParam String code){
try {
if(userService.activate(user, code))
{
return DtoUtil.returnSuccess("激活成功");
}else{
return DtoUtil.returnSuccess("激活失败");
}
} catch (Exception e) {
e.printStackTrace();
return DtoUtil.returnFail("激活失败", ErrorCode.AUTH_ACTIVATE_FAILED);
}
}