Feat: [后台]:前台对接修改个人信息接口

This commit is contained in:
fanxb 2019-11-17 17:31:21 +08:00
parent df5fb48be0
commit a7f373101f
8 changed files with 41 additions and 30 deletions

View File

@ -20,14 +20,6 @@ public class FileConstant {
/** /**
* 用户头像目录 * 用户头像目录
*/ */
public static String iconPath = Paths.get("static", "public", "icon").toString(); public static String iconPath = Paths.get("files", "public", "icon").toString();
@PostConstruct
private void init() {
File file = Paths.get(Constant.fileSavePath, iconPath).toFile();
if (!file.exists()) {
file.mkdirs();
}
}
} }

View File

@ -35,7 +35,7 @@ public class BaseInfoController {
* @date 2019/9/18 15:49 * @date 2019/9/18 15:49
*/ */
@PostMapping("/password") @PostMapping("/password")
public Result changePassword(@Valid @RequestBody UpdatePasswordBody body) { public Result changePassword(@Validated @RequestBody UpdatePasswordBody body) {
this.baseInfoService.changePassword(body); this.baseInfoService.changePassword(body);
return Result.success(null); return Result.success(null);
} }

View File

@ -16,10 +16,8 @@ import javax.validation.constraints.Pattern;
*/ */
@Data @Data
public class EmailUpdateBody { public class EmailUpdateBody {
// @Pattern(regexp = ValidatedConstant.PASSWORD_REG, message = ValidatedConstant.PASSWORD_MESSAGE)
// private String oldPass;
@NotNull(message = "参数不为空") @NotNull(message = "参数不为空")
private String actionId; private String actionId;
@Email(message = "请输入有效邮箱地址") @Email(message = "请输入有效邮箱地址")
private String newEmail; private String email;
} }

View File

@ -15,8 +15,7 @@ import javax.validation.constraints.Pattern;
@Data @Data
public class UpdatePasswordBody { public class UpdatePasswordBody {
private String actionId;
@Pattern(regexp = ValidatedConstant.PASSWORD_REG, message = ValidatedConstant.PASSWORD_MESSAGE) @Pattern(regexp = ValidatedConstant.PASSWORD_REG, message = ValidatedConstant.PASSWORD_MESSAGE)
private String oldPass; private String password;
@Pattern(regexp = ValidatedConstant.PASSWORD_REG, message = ValidatedConstant.PASSWORD_MESSAGE)
private String newPass;
} }

View File

@ -37,11 +37,12 @@ public class BaseInfoService {
public void changePassword(UpdatePasswordBody body) { public void changePassword(UpdatePasswordBody body) {
int userId = UserContextHolder.get().getUserId(); int userId = UserContextHolder.get().getUserId();
String realOldPass = userDao.selectByUserId(userId).getPassword(); String checkAuthKey = com.fanxb.bookmark.common.constant.RedisConstant.getPasswordCheckKey(userId, body.getActionId());
if (!realOldPass.equals(HashUtil.getPassword(body.getOldPass()))) { String str = RedisUtil.get(checkAuthKey, String.class);
throw new FormDataException("旧密码错误"); if (str == null) {
throw new CustomException("密码校验失败,无法更新密码");
} }
userDao.updatePasswordByUserId(userId, HashUtil.getPassword(body.getNewPass())); userDao.updatePasswordByUserId(userId, HashUtil.getPassword(body.getPassword()));
} }
@ -75,10 +76,10 @@ public class BaseInfoService {
String secret = UUID.randomUUID().toString().replaceAll("-", ""); String secret = UUID.randomUUID().toString().replaceAll("-", "");
String url = VERIFY_EMAIL.replaceAll("XXXX", Constant.serviceAddress + VERIFY_EMAIL_PATH + secret); String url = VERIFY_EMAIL.replaceAll("XXXX", Constant.serviceAddress + VERIFY_EMAIL_PATH + secret);
log.debug(url); log.debug(url);
MailInfo info = new MailInfo(body.getNewEmail(), "验证邮箱", url); MailInfo info = new MailInfo(body.getEmail(), "验证邮箱", url);
MailUtil.sendMail(info, true); MailUtil.sendMail(info, true);
RedisUtil.set(RedisConstant.getUpdateEmailKey(secret), String.valueOf(userId), TimeUtil.DAY_MS); RedisUtil.set(RedisConstant.getUpdateEmailKey(secret), String.valueOf(userId), TimeUtil.DAY_MS);
userDao.updateNewEmailByUserId(userId, body.getNewEmail()); userDao.updateNewEmailByUserId(userId, body.getEmail());
} }
/** /**

View File

@ -16,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -182,8 +184,11 @@ public class UserService {
} }
int userId = UserContextHolder.get().getUserId(); int userId = UserContextHolder.get().getUserId();
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
String path = Paths.get(FileConstant.iconPath, userId + fileName.substring(fileName.lastIndexOf("."))).toString(); String path = Paths.get(FileConstant.iconPath, userId + "." + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."))).toString();
file.transferTo(Paths.get(Constant.fileSavePath, path)); Path realPath = Paths.get(Constant.fileSavePath, path);
FileUtil.ensurePathExist(realPath.getParent().toString());
file.transferTo(realPath);
path = File.separator + path;
userDao.updateUserIcon(userId, path); userDao.updateUserIcon(userId, path);
return path; return path;
} }
@ -205,7 +210,7 @@ public class UserService {
} }
String actionId = UUID.randomUUID().toString().replaceAll("-", ""); String actionId = UUID.randomUUID().toString().replaceAll("-", "");
String key = RedisConstant.getPasswordCheckKey(userId, actionId); String key = RedisConstant.getPasswordCheckKey(userId, actionId);
RedisUtil.set(key, "1", 10 * 60 * 1000); RedisUtil.set(key, "1", 5 * 60 * 1000);
return actionId; return actionId;
} }
} }

View File

@ -83,9 +83,14 @@ public class LoginFilter implements Filter {
return; return;
} }
String requestUrl = request.getRequestURI().replace(urlPrefix, "");
//放行静态文件
if (requestUrl.startsWith("/static")) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
UserContextHolder.remove(); UserContextHolder.remove();
List<Url> publicUrl = this.getPublicUrl(); List<Url> publicUrl = this.getPublicUrl();
String requestUrl = request.getRequestURI().replace(urlPrefix, "");
for (Url url : publicUrl) { for (Url url : publicUrl) {
if (url.getMethod().equalsIgnoreCase(requestMethod) && matcher.match(url.getUrl(), requestUrl)) { if (url.getMethod().equalsIgnoreCase(requestMethod) && matcher.match(url.getUrl(), requestUrl)) {
filterChain.doFilter(servletRequest, servletResponse); filterChain.doFilter(servletRequest, servletResponse);

View File

@ -2,10 +2,7 @@ package com.fanxb.bookmark.common.util;
import com.fanxb.bookmark.common.exception.CustomException; import com.fanxb.bookmark.common.exception.CustomException;
import java.io.BufferedInputStream; import java.io.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -45,4 +42,18 @@ public class FileUtil {
public static String streamToString(InputStream stream) { public static String streamToString(InputStream stream) {
return streamToString(stream, "utf-8"); return streamToString(stream, "utf-8");
} }
/**
* 功能描述: 路径
*
* @param path path
* @author fanxb
* @date 2019/11/14 0:27
*/
public static void ensurePathExist(String path) {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
}
} }