|
@@ -1,5 +1,8 @@
|
|
|
package com.judong.chuanyiserver.service.impl;
|
|
|
|
|
|
+import cn.hutool.captcha.CaptchaUtil;
|
|
|
+import cn.hutool.captcha.ShearCaptcha;
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.judong.chuanyiserver.dao.UserDao;
|
|
|
import com.judong.chuanyiserver.entity.Permission;
|
|
@@ -9,14 +12,26 @@ import com.judong.chuanyiserver.enums.ResultEnum;
|
|
|
import com.judong.chuanyiserver.exception.CustomException;
|
|
|
import com.judong.chuanyiserver.service.UserService;
|
|
|
import com.judong.chuanyiserver.util.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.Cookie;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
@Transactional
|
|
|
public class UserServiceImpl implements UserService {
|
|
@@ -30,35 +45,71 @@ public class UserServiceImpl implements UserService {
|
|
|
@Autowired
|
|
|
private UserUtil userUtil;
|
|
|
|
|
|
+ @Override
|
|
|
+ public Result getCodeImage() {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ // 定义图形验证码的长、宽、验证码字符数、干扰线宽度
|
|
|
+ ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(126, 40, 4, 4);
|
|
|
+ // 图形验证码写出,可以写出到文件,也可以写出到流
|
|
|
+ String captchaCode = captcha.getCode();
|
|
|
+ HttpServletRequest request = userUtil.getRequest();
|
|
|
+ String sessionId = request.getRequestedSessionId();
|
|
|
+ if (Blank.isEmpty(sessionId)) {
|
|
|
+ sessionId = request.getSession().getId();
|
|
|
+ }
|
|
|
+ // 设置验证码有效时间
|
|
|
+ redisUtil.set(sessionId + "-captcha", captchaCode, ConstantStr.ONE_MINUTE);
|
|
|
+ jsonObject.put("uid", sessionId);
|
|
|
+ jsonObject.put("verifyCode", captcha.getImageBase64());
|
|
|
+ return Result.ok(jsonObject);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 前端用户登录
|
|
|
*
|
|
|
- * @param userName
|
|
|
- * @param password
|
|
|
+ * @param user
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public Result frontUserLogin(String userName, String password) {
|
|
|
+ public Result frontUserLogin(User user) {
|
|
|
try {
|
|
|
- password = RSAUtil.decrypt(password, "UTF-8");
|
|
|
+ user.setPassword(RSAUtil.decrypt(user.getPassword(), "UTF-8"));
|
|
|
} catch (Exception e) {
|
|
|
- throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
|
|
|
+ throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
|
|
|
}
|
|
|
- User user = userDao.getUserByNamePass(userName, EncryptUtils.StrToMD5(password));
|
|
|
- if (Blank.isEmpty(user)) {
|
|
|
+ User isExistUser = userDao.getUserByNamePass(user.getUserName(), EncryptUtils.StrToMD5(user.getPassword()));
|
|
|
+ if (Blank.isEmpty(isExistUser)) {
|
|
|
return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "用户名或密码错误");
|
|
|
}
|
|
|
+ // 校验验证码
|
|
|
+ String storeCode = Convert.toStr(redisUtil.get(user.getUid() + "-captcha"));
|
|
|
+ if (Blank.isEmpty(storeCode)) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "验证码失效,请重新获取验证码");
|
|
|
+ }
|
|
|
+ if (!user.getVerifyCode().equals(storeCode)) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "输入的验证码不正确");
|
|
|
+ }
|
|
|
+ // 验证码通过则删除验证码
|
|
|
+ redisUtil.del(user.getUid() + "-captcha");
|
|
|
JSONObject json = new JSONObject();
|
|
|
- List<Integer> roleIdList = userDao.getRoleIdListByUserId(user.getUserId());
|
|
|
+ isExistUser.setPassword("");
|
|
|
+ json.put("user", isExistUser);
|
|
|
+ List<Integer> roleIdList = userDao.getRoleIdListByUserId(isExistUser.getUserId());
|
|
|
+ if (Blank.isEmpty(roleIdList)) {
|
|
|
+ return Result.no(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), "没有角色信息");
|
|
|
+ }
|
|
|
json.put("roleIdList", roleIdList);
|
|
|
List<Permission> permissionList = userDao.getPermissionByRoleList(roleIdList);
|
|
|
+ if (Blank.isEmpty(permissionList)) {
|
|
|
+ return Result.no(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), "没有权限信息");
|
|
|
+ }
|
|
|
json.put("permissionList", permissionList);
|
|
|
//生成token
|
|
|
- String token = TokenUtil.token(userName, ConstantStr.HALF_HOUR);
|
|
|
+ String token = TokenUtil.token(isExistUser.getUserName(), ConstantStr.HALF_HOUR);
|
|
|
json.put("token", token);
|
|
|
- redisUtil.set(token, user.getUserId(), ConstantStr.HALF_HOUR);
|
|
|
+ redisUtil.set(token, isExistUser.getUserId(), ConstantStr.HALF_HOUR);
|
|
|
//更新用户登录状态
|
|
|
- if (userDao.updateLoginState(user.getUserId()) <= 0) {
|
|
|
+ if (userDao.updateLoginState(isExistUser.getUserId()) <= 0) {
|
|
|
return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "更新登录状态失败");
|
|
|
}
|
|
|
return Result.ok(json);
|
|
@@ -67,35 +118,49 @@ public class UserServiceImpl implements UserService {
|
|
|
/**
|
|
|
* 后台管理系统登录
|
|
|
*
|
|
|
- * @param userName
|
|
|
- * @param password
|
|
|
+ * @param user
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public Result backUserLogin(String userName, String password) {
|
|
|
+ public Result backUserLogin(User user) {
|
|
|
try {
|
|
|
- password = RSAUtil.decrypt(password, "UTF-8");
|
|
|
+ user.setPassword(RSAUtil.decrypt(user.getPassword(), "UTF-8"));
|
|
|
} catch (Exception e) {
|
|
|
throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
|
|
|
}
|
|
|
- User user = userDao.getUserByNamePass(userName, EncryptUtils.StrToMD5(password));
|
|
|
- if (Blank.isEmpty(user)) {
|
|
|
+ User isExistUser = userDao.getUserByNamePass(user.getUserName(), EncryptUtils.StrToMD5(user.getPassword()));
|
|
|
+ if (Blank.isEmpty(isExistUser)) {
|
|
|
return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "用户名或密码错误");
|
|
|
}
|
|
|
- List<Integer> roleIdList = userDao.getRoleIdListByUserId(user.getUserId());
|
|
|
+ // 校验验证码
|
|
|
+ String storeCode = Convert.toStr(redisUtil.get(user.getUid() + "-captcha"));
|
|
|
+ if (Blank.isEmpty(storeCode)) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "验证码失效,请重新获取验证码");
|
|
|
+ }
|
|
|
+ if (!user.getVerifyCode().equals(storeCode)) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "输入的验证码不正确");
|
|
|
+ }
|
|
|
+ // 验证码通过则删除验证码
|
|
|
+ redisUtil.del(user.getUid() + "-captcha");
|
|
|
+ List<Integer> roleIdList = userDao.getRoleIdListByUserId(isExistUser.getUserId());
|
|
|
if (Blank.isEmpty(roleIdList)) {
|
|
|
return Result.no(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), ResultEnum.NO_OPERATION_AUTHORITY.getRespMsg());
|
|
|
}
|
|
|
for (Integer roleId : roleIdList) {
|
|
|
if (roleId == ConstantStr.ROLE_ADMIN) {
|
|
|
JSONObject json = new JSONObject();
|
|
|
+ isExistUser.setPassword("");
|
|
|
+ json.put("user", isExistUser);
|
|
|
json.put("roleIdList", roleIdList);
|
|
|
List<Permission> permissionList = userDao.getPermissionByRoleList(roleIdList);
|
|
|
+ if (Blank.isEmpty(permissionList)) {
|
|
|
+ return Result.no(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), "没有权限信息");
|
|
|
+ }
|
|
|
json.put("permissionList", permissionList);
|
|
|
//生成token
|
|
|
- String token = TokenUtil.token(userName, ConstantStr.HALF_HOUR);
|
|
|
+ String token = TokenUtil.token(isExistUser.getUserName(), ConstantStr.HALF_HOUR);
|
|
|
json.put("token", token);
|
|
|
- redisUtil.set(token, user.getUserId(), ConstantStr.HALF_HOUR);
|
|
|
+ redisUtil.set(token, isExistUser.getUserId(), ConstantStr.HALF_HOUR);
|
|
|
return Result.ok(json);
|
|
|
}
|
|
|
}
|