|
|
@@ -58,120 +58,6 @@ public class UserController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 添加@ResponseBody注解就可以返回json了
|
|
|
- /**
|
|
|
- * 获取验证码,返回aes给客户端
|
|
|
- */
|
|
|
- @RequestMapping(value = "/api/code", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
|
|
|
- @ResponseBody
|
|
|
- public String getAuthCode(String email) {
|
|
|
- ResponseResult<Boolean> responseResult = new ResponseResult<Boolean>();
|
|
|
- responseResult.setContent(false);
|
|
|
- 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.setCode(ResultCode.SUCCESS);
|
|
|
- responseResult.setMessage("获取验证码成功");
|
|
|
- responseResult.setContent(true);
|
|
|
- responseResult.setExtra(SecureManager.KEY_FOR_CLIENT + "=" + SecureManager.IV_FOR_CLIENT);
|
|
|
- } else {
|
|
|
- responseResult.setCode(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(@RequestBody Map<String, Object> registerMap) {
|
|
|
- ResponseResult<User> responseResult = new ResponseResult<User>();
|
|
|
- responseResult.setCode(ResultCode.FAILED_COMMON);
|
|
|
- responseResult.setMessage("注册失败");
|
|
|
-
|
|
|
- if (registerMap == null) {
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- String email = String.valueOf(registerMap.get("email"));
|
|
|
- String code = String.valueOf(registerMap.get("code"));
|
|
|
- String password = String.valueOf(registerMap.get("password"));
|
|
|
- String nickname = String.valueOf(registerMap.get("nickname"));
|
|
|
- String cashAccount = String.valueOf(registerMap.get("cashAccount"));
|
|
|
-
|
|
|
- // 从数据库查询,如果没有这个email并且不在黑名单里,才可以继续注册
|
|
|
- User dbUser = userRepository.findUserByEmail(email);
|
|
|
- if (dbUser != null) {
|
|
|
- responseResult.setCode(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);
|
|
|
- user.setCashAccount(cashAccount);
|
|
|
- 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.setCode(ResultCode.SUCCESS);
|
|
|
- responseResult.setMessage("注册成功");
|
|
|
- responseResult.setContent(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.setCode(ResultCode.SUCCESS);
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 登录:email+加密的密码
|
|
|
*/
|
|
|
@@ -235,12 +121,37 @@ public class UserController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 登录:email+token
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/login/token", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String loginToken(@RequestHeader HttpHeaders header) {
|
|
|
+ String token = header.getFirst("token");
|
|
|
+ String email = header.getFirst("email");
|
|
|
+
|
|
|
+ ResponseResult<User> responseResult = new ResponseResult<User>();
|
|
|
+ responseResult.setMessage("请重新登录");
|
|
|
+ User user = userRepository.findUserByEmail(email);
|
|
|
+ if (user == null || !TextUtils.equals(token, user.getToken())) {
|
|
|
+ // 拿到token就可以拿到token的有效时间,以后可以做过期校验
|
|
|
+ responseResult.setMessage("请重新登录");
|
|
|
+ responseResult.setCode(ResultCode.FAILED_NEED_RE_LOGIN);
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ responseResult.setContent(user);
|
|
|
+ responseResult.setMessage("登录成功");
|
|
|
+ responseResult.setCode(ResultCode.SUCCESS);
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设置为VIP
|
|
|
*/
|
|
|
@RequestMapping(value = "/api/setVip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
@ResponseBody
|
|
|
- public String setApiConfig(@RequestHeader HttpHeaders header, String email) {
|
|
|
+ public String setVip(@RequestHeader HttpHeaders header, String email) {
|
|
|
ResponseResult<String> responseResult = new ResponseResult<String>();
|
|
|
responseResult.setMessage("设置失败");
|
|
|
if (!RequestTool.isRequestAdminValid(header, userRepository)) {
|
|
|
@@ -270,28 +181,89 @@ public class UserController {
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
- * 登录:email+token
|
|
|
+ * 注册
|
|
|
*/
|
|
|
- @RequestMapping(value = "/api/login/token", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
+ @RequestMapping(value = "/api/register", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
@ResponseBody
|
|
|
- public String loginToken(@RequestHeader HttpHeaders header) {
|
|
|
- String token = header.getFirst("token");
|
|
|
- String email = header.getFirst("email");
|
|
|
-
|
|
|
+ public String register(@RequestBody Map<String, Object> registerMap) {
|
|
|
ResponseResult<User> responseResult = new ResponseResult<User>();
|
|
|
- responseResult.setMessage("请重新登录");
|
|
|
- User user = userRepository.findUserByEmail(email);
|
|
|
- if (user == null || !TextUtils.equals(token, user.getToken())) {
|
|
|
- // 拿到token就可以拿到token的有效时间,以后可以做过期校验
|
|
|
- responseResult.setMessage("请重新登录");
|
|
|
- responseResult.setCode(ResultCode.FAILED_NEED_RE_LOGIN);
|
|
|
+ responseResult.setCode(ResultCode.FAILED_COMMON);
|
|
|
+ responseResult.setMessage("注册失败");
|
|
|
+
|
|
|
+ if (registerMap == null) {
|
|
|
return JSON.toJSONString(responseResult);
|
|
|
}
|
|
|
|
|
|
- responseResult.setContent(user);
|
|
|
- responseResult.setMessage("登录成功");
|
|
|
- responseResult.setCode(ResultCode.SUCCESS);
|
|
|
+ String email = String.valueOf(registerMap.get("email"));
|
|
|
+ String code = String.valueOf(registerMap.get("code"));
|
|
|
+ String password = String.valueOf(registerMap.get("password"));
|
|
|
+ String nickname = String.valueOf(registerMap.get("nickname"));
|
|
|
+ String cashAccount = String.valueOf(registerMap.get("cashAccount"));
|
|
|
+
|
|
|
+ // 从数据库查询,如果没有这个email并且不在黑名单里,才可以继续注册
|
|
|
+ User dbUser = userRepository.findUserByEmail(email);
|
|
|
+ if (dbUser != null) {
|
|
|
+ responseResult.setCode(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);
|
|
|
+ user.setCashAccount(cashAccount);
|
|
|
+ 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.setCode(ResultCode.SUCCESS);
|
|
|
+ responseResult.setMessage("注册成功");
|
|
|
+ responseResult.setContent(user);
|
|
|
+
|
|
|
+ // 注册成功,从内存中移除相应的验证码,节省资源
|
|
|
+ mCodeMap.remove(email);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ responseResult.setMessage("注册失败,请检查数据格式");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
return JSON.toJSONString(responseResult);
|
|
|
}
|
|
|
|
|
|
@@ -445,4 +417,41 @@ public class UserController {
|
|
|
responseResult.setCode(ResultCode.SUCCESS);
|
|
|
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.setCode(ResultCode.SUCCESS);
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加@ResponseBody注解就可以返回json了
|
|
|
+ /**
|
|
|
+ * 获取验证码,返回aes给客户端
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/api/code", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
|
|
|
+ @ResponseBody
|
|
|
+ public String getAuthCode(String email) {
|
|
|
+ ResponseResult<Boolean> responseResult = new ResponseResult<Boolean>();
|
|
|
+ responseResult.setContent(false);
|
|
|
+ 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.setCode(ResultCode.SUCCESS);
|
|
|
+ responseResult.setMessage("获取验证码成功");
|
|
|
+ responseResult.setContent(true);
|
|
|
+ responseResult.setExtra(SecureManager.KEY_FOR_CLIENT + "=" + SecureManager.IV_FOR_CLIENT);
|
|
|
+ } else {
|
|
|
+ responseResult.setCode(ResultCode.FAILED_COMMON);
|
|
|
+ responseResult.setMessage("获取验证码失败");
|
|
|
+ }
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
+ }
|
|
|
}
|