|
|
@@ -0,0 +1,360 @@
|
|
|
+package com.miekir.shibei.controller.api;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.miekir.shibei.bean.ResponseResult;
|
|
|
+import com.miekir.shibei.bean.ResultCode;
|
|
|
+import com.miekir.shibei.bean.User;
|
|
|
+import com.miekir.shibei.repository.UserRepository;
|
|
|
+import com.miekir.shibei.tool.CodeGenerator;
|
|
|
+import com.miekir.shibei.tool.TextUtils;
|
|
|
+import com.miekir.shibei.tool.TokenGenerator;
|
|
|
+import com.miekir.shibei.tool.email.EmailTool;
|
|
|
+import com.miekir.shibei.tool.secure.SecureManager;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Enumeration;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+@Controller
|
|
|
+public class UserController {
|
|
|
+ private static final long TIME_MILLIS_CLEAR = 5 * 60 * 1000;
|
|
|
+
|
|
|
+ // email, timeMillis-code
|
|
|
+ private ConcurrentHashMap<String, String> mCodeMap = new ConcurrentHashMap<String, String>();
|
|
|
+ // 自动装配
|
|
|
+ @Autowired
|
|
|
+ private UserRepository userRepository;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ HttpServletRequest request;
|
|
|
+
|
|
|
+ // 每隔5分钟去清理验证码
|
|
|
+ @Scheduled(cron = "* 0/5 * * * *")
|
|
|
+ public void clearMapTask() {
|
|
|
+ Enumeration<String> keys = mCodeMap.keys();
|
|
|
+ while (keys.hasMoreElements()) {
|
|
|
+ String key = keys.nextElement();
|
|
|
+ String generateMillisCode = mCodeMap.get(key);
|
|
|
+ if (!TextUtils.isEmpty(generateMillisCode)) {
|
|
|
+ long generateMillis = Long.valueOf(generateMillisCode.split("-")[0]);
|
|
|
+ if (System.currentTimeMillis() - generateMillis > TIME_MILLIS_CLEAR) {
|
|
|
+ // 验证码已过期
|
|
|
+ mCodeMap.remove(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加@ResponseBody注解就可以返回json了
|
|
|
+ /**
|
|
|
+ * 获取验证码,返回aes给客户端
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/code", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String getAuthCode(String email) {
|
|
|
+ ResponseResult<String> responseResult = new ResponseResult<String>();
|
|
|
+ responseResult.setMessage("获取验证码失败");
|
|
|
+ String code = CodeGenerator.getInstance().getRandomCode();
|
|
|
+ int result = new EmailTool().sendEmail("吾记温馨提醒您,您的6位验证码(5分钟内有效)是:" + code, email);
|
|
|
+ if (result == EmailTool.SUCCESS) {
|
|
|
+ mCodeMap.put(email, System.currentTimeMillis()+"-"+code);
|
|
|
+ responseResult.setResultCode(ResultCode.SUCCESS);
|
|
|
+ responseResult.setMessage("获取验证码成功");
|
|
|
+ responseResult.setExtra(SecureManager.KEY_FOR_CLIENT + "=" + SecureManager.IV_FOR_CLIENT);
|
|
|
+ } else {
|
|
|
+ responseResult.setResultCode(ResultCode.FAILED_COMMON);
|
|
|
+ responseResult.setMessage("获取验证码失败");
|
|
|
+ }
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/register", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String register(String email, String password, String code, String nickName) {
|
|
|
+ ResponseResult<User> responseResult = new ResponseResult<User>();
|
|
|
+ responseResult.setResultCode(ResultCode.FAILED_COMMON);
|
|
|
+ responseResult.setMessage("注册失败");
|
|
|
+
|
|
|
+ // 从数据库查询,如果没有这个email并且不在黑名单里,才可以继续注册
|
|
|
+ User dbUser = userRepository.findUserByEmail(email);
|
|
|
+ if (dbUser != null) {
|
|
|
+ responseResult.setResultCode(ResultCode.FAILED_USER_ALREADY_EXIST);
|
|
|
+ responseResult.setMessage("该邮箱已被使用");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查看是否已发送验证码
|
|
|
+ if (!mCodeMap.containsKey(email)) {
|
|
|
+ responseResult.setMessage("请重新获取验证码");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查看服务器发送的验证码和用户发过来的验证码是否一致
|
|
|
+ String content = mCodeMap.get(email);
|
|
|
+ String realCode = content.substring(content.indexOf("-")+1);
|
|
|
+ if (!code.equals(realCode)) {
|
|
|
+ responseResult.setMessage("验证码错误");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始注册,插入数据库
|
|
|
+ User user = new User();
|
|
|
+ user.setEmail(email);
|
|
|
+ user.setNickName(nickName);
|
|
|
+ user.setRegisterTimeMillis(System.currentTimeMillis());
|
|
|
+ user.setPassword(password);
|
|
|
+ try {
|
|
|
+ String token = TokenGenerator.getInstance().getToken(email);
|
|
|
+ if (token == null || token.equals("")) {
|
|
|
+ responseResult.setMessage("注册失败,请检查数据格式");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注册成功,生成token给用户
|
|
|
+ user.setToken(token);
|
|
|
+ userRepository.save(user);
|
|
|
+
|
|
|
+ user.setPassword("");
|
|
|
+ responseResult.setResultCode(ResultCode.SUCCESS);
|
|
|
+ responseResult.setMessage("注册成功");
|
|
|
+ responseResult.setResultObj(user);
|
|
|
+
|
|
|
+ // 注册成功,从内存中移除相应的验证码,节省资源
|
|
|
+ mCodeMap.remove(email);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ responseResult.setMessage("注册失败,请检查数据格式");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录之前先获取AES
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/aes", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String getAES() {
|
|
|
+ ResponseResult responseResult = new ResponseResult();
|
|
|
+ responseResult.setExtra(SecureManager.KEY_FOR_CLIENT + "=" + SecureManager.IV_FOR_CLIENT);
|
|
|
+ responseResult.setResultCode(ResultCode.SUCCESS);
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录:email+加密的密码
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/login/normal", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String loginNormal(String email, String password) {
|
|
|
+ ResponseResult<User> responseResult = new ResponseResult<User>();
|
|
|
+ responseResult.setMessage("登录失败");
|
|
|
+ User user = userRepository.findUserByEmail(email);
|
|
|
+ if (user == null) {
|
|
|
+ responseResult.setMessage("用户不存在");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ String encryptPassword = user.getPassword();
|
|
|
+
|
|
|
+ if (!password.equals(encryptPassword)) {
|
|
|
+ responseResult.setMessage("请检查账号和密码");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 登录成功,生成token给用户
|
|
|
+ String token = TokenGenerator.getInstance().getToken(email);
|
|
|
+ if (token == null || token.equals("")) {
|
|
|
+ responseResult.setMessage("登录失败,请检查数据格式");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+ user.setToken(token);
|
|
|
+ userRepository.save(user);
|
|
|
+
|
|
|
+ user.setPassword("");
|
|
|
+ responseResult.setResultCode(ResultCode.SUCCESS);
|
|
|
+ responseResult.setMessage("登录成功");
|
|
|
+ responseResult.setResultObj(user);
|
|
|
+
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录:email+token
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/login/token", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String loginToken(String email, String token) {
|
|
|
+ ResponseResult responseResult = new ResponseResult();
|
|
|
+ responseResult.setMessage("请重新登录");
|
|
|
+ User user = userRepository.findUserByEmail(email);
|
|
|
+ if (user == null || !TextUtils.equals(token, user.getToken())) {
|
|
|
+ // 拿到token就可以拿到token的有效时间,以后可以做过期校验
|
|
|
+ responseResult.setMessage("请重新登录");
|
|
|
+ responseResult.setResultCode(ResultCode.FAILED_NEED_RE_LOGIN);
|
|
|
+ }
|
|
|
+
|
|
|
+ responseResult.setMessage("token登录成功");
|
|
|
+ responseResult.setResultCode(ResultCode.SUCCESS);
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重置密码
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/password", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String resetPassword(String email, String code, String password) {
|
|
|
+ ResponseResult<User> responseResult = new ResponseResult<User>();
|
|
|
+ responseResult.setResultCode(ResultCode.FAILED_COMMON);
|
|
|
+ responseResult.setMessage("密码重置失败");
|
|
|
+
|
|
|
+ // 从数据库查询,如果有这个email并且不在黑名单里,才可以继续
|
|
|
+ User dbUser = userRepository.findUserByEmail(email);
|
|
|
+ if (dbUser == null) {
|
|
|
+ responseResult.setResultCode(ResultCode.FAILED_USER_NOT_EXIST);
|
|
|
+ responseResult.setMessage("用户不存在");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查看是否已发送验证码
|
|
|
+ if (!mCodeMap.containsKey(email)) {
|
|
|
+ responseResult.setMessage("请重新获取验证码");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查看服务器发送的验证码和用户发过来的验证码是否一致
|
|
|
+ String content = mCodeMap.get(email);
|
|
|
+ String realCode = content.substring(content.indexOf("-")+1);
|
|
|
+ if (!code.equals(realCode)) {
|
|
|
+ responseResult.setMessage("验证码错误");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始重设密码,插入数据库
|
|
|
+ try {
|
|
|
+ dbUser.setPassword(password);
|
|
|
+ String token = TokenGenerator.getInstance().getToken(email);
|
|
|
+ if (token == null || token.equals("")) {
|
|
|
+ responseResult.setMessage("重置失败,请检查数据格式");
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 密码重置成功,生成token给用户
|
|
|
+ dbUser.setToken(token);
|
|
|
+ userRepository.save(dbUser);
|
|
|
+
|
|
|
+ // 这种是不是会导致密码被修改为空。答案:不会
|
|
|
+ dbUser.setPassword("");
|
|
|
+ responseResult.setResultCode(ResultCode.SUCCESS);
|
|
|
+ responseResult.setMessage("密码重置成功");
|
|
|
+ responseResult.setResultObj(dbUser);
|
|
|
+
|
|
|
+ // 注册成功,从内存中移除相应的验证码,节省资源
|
|
|
+ mCodeMap.remove(email);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ responseResult.setMessage("重置失败,请检查数据格式");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/api/uploadBg", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String saveBgInfo(String email, String token, int mode, MultipartFile file) {
|
|
|
+ ResponseResult<String> responseResult = new ResponseResult<String>();
|
|
|
+ responseResult.setResultCode(ResultCode.FAILED_COMMON);
|
|
|
+ responseResult.setMessage("上传背景失败");
|
|
|
+
|
|
|
+ //String bgDir = request.getSession().getServletContext().getRealPath("bg") + File.separator;
|
|
|
+ String currentDir = request.getSession().getServletContext().getRealPath("/");
|
|
|
+ File bgDirFile = new File(new File(currentDir).getParentFile().getAbsolutePath(), "res/wuji/bg/");
|
|
|
+ if (!bgDirFile.exists()) {
|
|
|
+ bgDirFile.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (file != null) {
|
|
|
+ //String fileName = file.getOriginalFilename();
|
|
|
+ //String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ String fileSuffix = ".jpg";
|
|
|
+
|
|
|
+ try {
|
|
|
+ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(bgDirFile, email + fileSuffix)));
|
|
|
+ //System.out.println(file.getName());
|
|
|
+ out.write(file.getBytes());
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ responseResult.setResultCode(ResultCode.SUCCESS);
|
|
|
+ responseResult.setMessage("上传背景成功");
|
|
|
+
|
|
|
+ // 更新用户的theme mode(4为自定义)
|
|
|
+ userRepository.updateUser(mode, email);
|
|
|
+ } catch (Exception e) {
|
|
|
+ responseResult.setMessage("上传背景失败");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/api/downloadBg", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ public ResponseEntity<byte[]> download(String email, String token) throws IOException {
|
|
|
+ //String bgPath = request.getSession().getServletContext().getRealPath("bg") + File.separator + email + ".jpg";
|
|
|
+ String currentDir = request.getSession().getServletContext().getRealPath("/");
|
|
|
+ File file = new File(new File(currentDir).getParentFile().getAbsolutePath(), "res/wuji/bg/" + email + ".jpg");
|
|
|
+ if (!file.exists()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ byte[] body = null;
|
|
|
+ InputStream is = new FileInputStream(file);
|
|
|
+ body = new byte[is.available()];
|
|
|
+ is.read(body);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("Content-Disposition", "attchement;filename=" + file.getName());
|
|
|
+ HttpStatus statusCode = HttpStatus.OK;
|
|
|
+ ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新主题模式
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/updateTheme", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String updateTheme(String email, String token, int mode) {
|
|
|
+ ResponseResult responseResult = new ResponseResult();
|
|
|
+ responseResult.setMessage("用户模式更新失败");
|
|
|
+
|
|
|
+ try {
|
|
|
+ userRepository.updateUser(mode, email);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ responseResult.setMessage("用户模式更新成功");
|
|
|
+ responseResult.setResultCode(ResultCode.SUCCESS);
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+}
|