Feat: 临时提交

This commit is contained in:
fanxb 2019-09-20 17:08:40 +08:00
parent cb212e8cfc
commit cfe535e2a8
17 changed files with 428 additions and 108 deletions

View File

@ -1,5 +1,6 @@
HELP.md HELP.md
target target
static
!.mvn/wrapper/maven-wrapper.jar !.mvn/wrapper/maven-wrapper.jar
### STS ### ### STS ###

View File

@ -0,0 +1,24 @@
package com.fanxb.bookmark.business.user.constant;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/9/20 16:44
*/
public class RedisConstant {
/**
* 修改邮箱验证有效期
*/
public static final int UPDATE_EMAIL_EXPIRE = 120;
/**
* 获取redis key
* @param code code
*/
public static String getUpdateEmailKey(String code) {
return "update_email_" + code;
}
}

View File

@ -0,0 +1,29 @@
package com.fanxb.bookmark.business.user.constant;
/**
* 类功能简述校验相关常量
* 类功能详述
*
* @author fanxb
* @date 2019/9/20 14:49
*/
public interface ValidatedConstant {
/**
* 密码正则
*/
String PASSWORD_REG = "^(?=.*?\\d+.*?)(?=.*?[a-zA-Z]+.*?)[\\da-zA-Z]{6,20}$";
/**
* 密码校验错误提示语
*/
String PASSWORD_MESSAGE = "密码为6-20位数字和字母组合";
/**
* 用户名正则
*/
String USERNAME_REG = "[\\da-zA-Z]{1,10}";
/**
* 用户名错误提示语
*/
String USERNAME_MESSAGE = "用户名为1-10位的数字或字母组合";
}

View File

@ -0,0 +1,75 @@
package com.fanxb.bookmark.business.user.controller;
import com.fanxb.bookmark.business.user.entity.EmailUpdateBody;
import com.fanxb.bookmark.business.user.entity.UpdatePasswordBody;
import com.fanxb.bookmark.business.user.entity.UsernameBody;
import com.fanxb.bookmark.business.user.service.BaseInfoService;
import com.fanxb.bookmark.common.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* 类功能简述用户基本信息相关功能
*
* @author fanxb
* @date 2019/9/18 15:53
*/
@RestController
@RequestMapping("/baseInfo")
@Validated
public class BaseInfoController {
@Autowired
private BaseInfoService baseInfoService;
/**
* Description: 修改密码
*
* @param oldPass 旧密码
* @param newPass 新密码
* @return com.fanxb.bookmark.common.entity.Result
* @author fanxb
* @date 2019/9/18 15:49
*/
@PostMapping("/password")
public Result changePassword(@Valid @RequestBody UpdatePasswordBody body) {
this.baseInfoService.changePassword(body);
return Result.success(null);
}
/**
* Description: 修改用户名
*
* @param body body
* @return com.fanxb.bookmark.common.entity.Result
* @author fanxb
* @date 2019/9/18 15:42
*/
@PostMapping("/username")
public Result updateUsername(@Validated @RequestBody UsernameBody body) {
this.baseInfoService.updateUsername(body.getUsername());
return null;
}
/**
* Description: 修改邮箱需要先确认密码,获取checkId然后还需确认新邮箱
*
* @param email 新的邮箱地址
* @param checkId 确认id
* @return com.fanxb.bookmark.common.entity.Result
* @author fanxb
* @date 2019/9/18 15:41
*/
@PostMapping("/email")
public Result updateEmail(@Validated @RequestBody EmailUpdateBody body) {
return null;
}
}

View File

@ -75,4 +75,26 @@ public interface UserDao {
*/ */
@Update("update user set icon=#{icon} where userId=#{userId}") @Update("update user set icon=#{icon} where userId=#{userId}")
void updateUserIcon(@Param("userId") int userId, @Param("icon") String icon); void updateUserIcon(@Param("userId") int userId, @Param("icon") String icon);
/**
* Description: 根据用户id修改密码
*
* @param userId userId
* @param newPass newPass
* @author fanxb
* @date 2019/9/20 14:39
*/
@Update("update user set password=#{password} where userId=#{userId}")
void updatePasswordByUserId(@Param("userId") int userId, @Param("password") String newPass);
/**
* Description: 根据用户id修改用户名
*
* @author fanxb
* @date 2019/9/20 16:22
* @param userId userId
* @param username username
*/
@Update("update user set username=#{username} where userId=#{userId}")
void updateUsernameByUserId(@Param("userId") int userId, @Param("username") String username);
} }

View File

@ -0,0 +1,24 @@
package com.fanxb.bookmark.business.user.entity;
import com.fanxb.bookmark.business.user.constant.ValidatedConstant;
import lombok.Data;
import javax.validation.constraints.Email;
import javax.validation.constraints.Pattern;
/**
* 类功能简述修改邮箱表单
* 类功能详述
*
* @author fanxb
* @date 2019/9/20 16:35
*/
@Data
public class EmailUpdateBody {
@Pattern(regexp = ValidatedConstant.PASSWORD_REG, message = ValidatedConstant.PASSWORD_MESSAGE)
private String oldPass;
@Email
private String newEmail;
private String code;
}

View File

@ -0,0 +1,16 @@
package com.fanxb.bookmark.business.user.entity;
import lombok.Data;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/9/20 16:47
*/
@Data
public class EmailUpdateRedis {
private int userId;
private String email;
}

View File

@ -0,0 +1,22 @@
package com.fanxb.bookmark.business.user.entity;
import com.fanxb.bookmark.business.user.constant.ValidatedConstant;
import lombok.Data;
import javax.validation.constraints.Pattern;
/**
* 类功能简述修改密码表单
* 类功能详述
*
* @author fanxb
* @date 2019/9/20 16:13
*/
@Data
public class UpdatePasswordBody {
@Pattern(regexp = ValidatedConstant.PASSWORD_MESSAGE, message = ValidatedConstant.PASSWORD_MESSAGE)
private String oldPass;
@Pattern(regexp = ValidatedConstant.PASSWORD_MESSAGE, message = ValidatedConstant.PASSWORD_MESSAGE)
private String newPass;
}

View File

@ -0,0 +1,20 @@
package com.fanxb.bookmark.business.user.entity;
import com.fanxb.bookmark.business.user.constant.ValidatedConstant;
import lombok.Data;
import javax.validation.constraints.Pattern;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/9/20 15:46
*/
@Data
public class UsernameBody {
@Pattern(regexp = ValidatedConstant.USERNAME_REG, message = ValidatedConstant.USERNAME_MESSAGE)
private String username;
}

View File

@ -0,0 +1,60 @@
package com.fanxb.bookmark.business.user.service;
import com.fanxb.bookmark.business.user.dao.UserDao;
import com.fanxb.bookmark.business.user.entity.EmailUpdateBody;
import com.fanxb.bookmark.business.user.entity.UpdatePasswordBody;
import com.fanxb.bookmark.common.constant.Constant;
import com.fanxb.bookmark.common.entity.MailInfo;
import com.fanxb.bookmark.common.exception.FormDataException;
import com.fanxb.bookmark.common.util.HashUtil;
import com.fanxb.bookmark.common.util.MailUtil;
import com.fanxb.bookmark.common.util.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/9/18 15:54
*/
@Service
public class BaseInfoService {
@Autowired
private UserDao userDao;
public void changePassword(UpdatePasswordBody body) {
int userId = UserContextHolder.get().getUserId();
String realOldPass = userDao.selectByUserId(userId).getPassword();
if (!realOldPass.equals(HashUtil.getPassword(body.getOldPass()))) {
throw new FormDataException("旧密码错误");
}
userDao.updatePasswordByUserId(userId, HashUtil.getPassword(body.getNewPass()));
}
/**
* Description: 修改用户名
*
* @param username 用户名
* @author fanxb
* @date 2019/9/20 16:18
*/
public void updateUsername(String username) {
userDao.updateUsernameByUserId(UserContextHolder.get().getUserId(), username);
}
public void updateEmail(EmailUpdateBody body){
int userId = UserContextHolder.get().getUserId();
String realPass=userDao.selectByUserId(userId).getPassword();
if (!realPass.equals(HashUtil.getPassword(body.getOldPass()))) {
throw new FormDataException("旧密码错误");
}
MailInfo info = new MailInfo(body.getNewEmail(),"验证邮箱",);
MailUtil.sendTextMail();
Constant
}
}

View File

@ -149,7 +149,7 @@ public class UserService {
throw new FormDataException("验证码错误"); throw new FormDataException("验证码错误");
} }
RedisUtil.delete(codeKey); RedisUtil.delete(codeKey);
String newPassword = HashUtil.sha1(HashUtil.md5(body.getPassword())); String newPassword = HashUtil.getPassword(body.getPassword());
userDao.resetPassword(newPassword, body.getEmail()); userDao.resetPassword(newPassword, body.getEmail());
} }

View File

@ -35,11 +35,7 @@
</update> </update>
<select id="selectByUserId" resultType="com.fanxb.bookmark.common.entity.User"> <select id="selectByUserId" resultType="com.fanxb.bookmark.common.entity.User">
select select *
userId,
username,
email,
icon
from user from user
where userId = #{userId} where userId = #{userId}
</select> </select>

View File

@ -19,10 +19,6 @@ public class Constant {
*/ */
public static final String JWT_KEY = "jwt-token"; public static final String JWT_KEY = "jwt-token";
/**
* jwt 过期时间ms
*/
public static int JWT_EXPIRE_TIME = 60 * 60 * 60 * 1000;
/** /**
* 验证码过期时间 * 验证码过期时间

View File

@ -1,5 +1,6 @@
package com.fanxb.bookmark.common.entity; package com.fanxb.bookmark.common.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
/** /**
@ -16,6 +17,7 @@ public class User {
private String username; private String username;
private String email; private String email;
private String icon; private String icon;
@JsonIgnore
private String password; private String password;
private long createTime; private long createTime;
private long lastLoginTime; private long lastLoginTime;

View File

@ -3,6 +3,7 @@ package com.fanxb.bookmark.common.exception;
import com.fanxb.bookmark.common.entity.Result; import com.fanxb.bookmark.common.entity.Result;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
@ -21,7 +22,9 @@ public class ExceptionHandle {
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public Result handleException(Exception e) { public Result handleException(Exception e) {
CustomException ce; CustomException ce;
if (e instanceof CustomException) { if (e instanceof MethodArgumentNotValidException) {
ce = new FormDataException(((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors().get(0).getDefaultMessage());
} else if (e instanceof CustomException) {
logger.error("捕获到自定义错误:{}", e.getMessage(), e); logger.error("捕获到自定义错误:{}", e.getMessage(), e);
ce = (CustomException) e; ce = (CustomException) e;
} else { } else {

View File

@ -13,6 +13,21 @@ import java.security.MessageDigest;
*/ */
public class HashUtil { public class HashUtil {
/**
* Description: 生成密码
*
* @param pass 明文
* @return java.lang.String
* @author fanxb
* @date 2019/9/20 14:34
*/
public static String getPassword(String pass) {
if (StringUtil.isEmpty(pass)) {
return "";
}
return HashUtil.sha1(HashUtil.md5(pass));
}
/** /**
* Description: md5 签名字符串 * Description: md5 签名字符串
* *

View File

@ -1,10 +1,12 @@
package com.fanxb.bookmark.common.util; package com.fanxb.bookmark.common.util;
import com.fanxb.bookmark.common.entity.MailInfo; import com.fanxb.bookmark.common.entity.MailInfo;
import com.fanxb.bookmark.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -14,6 +16,7 @@ import org.springframework.stereotype.Component;
* Time: 16:08 * Time: 16:08
*/ */
@Component @Component
@Slf4j
public class MailUtil { public class MailUtil {
private static JavaMailSender mailSender; private static JavaMailSender mailSender;
@ -29,13 +32,25 @@ public class MailUtil {
MailUtil.from = from; MailUtil.from = from;
} }
public static void sendMail(MailInfo info, boolean isHtml) {
try {
MimeMessageHelper helper = new MimeMessageHelper(mailSender.createMimeMessage());
helper.setFrom(from);
helper.setTo(info.getReceiver());
helper.setSubject(info.getSubject());
helper.setText(info.getContent(), isHtml);
mailSender.send(helper.getMimeMessage());
} catch (Exception e) {
throw new CustomException("发送邮件失败:" + e.getMessage(), e);
}
}
public static void sendTextMail(MailInfo info) { public static void sendTextMail(MailInfo info) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); sendMail(info, false);
simpleMailMessage.setFrom(from);
simpleMailMessage.setTo(info.getReceiver());
simpleMailMessage.setSubject(info.getSubject());
simpleMailMessage.setText(info.getContent());
mailSender.send(simpleMailMessage);
} }
} }