完善验证码和登录的接口
This commit is contained in:
@@ -218,4 +218,13 @@ public class BusinessConstants {
|
|||||||
* */
|
* */
|
||||||
//session的生命周期,秒
|
//session的生命周期,秒
|
||||||
public static final Long MAX_SESSION_IN_SECONDS=60*60*24*3L;
|
public static final Long MAX_SESSION_IN_SECONDS=60*60*24*3L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码 redis key
|
||||||
|
*/
|
||||||
|
public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
|
||||||
|
/**
|
||||||
|
* 验证码有效期(分钟)
|
||||||
|
*/
|
||||||
|
public static final Integer CAPTCHA_EXPIRATION = 2;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,12 @@ public class ExceptionConstants {
|
|||||||
//当前机构已经存在经理
|
//当前机构已经存在经理
|
||||||
public static final int USER_LEADER_IS_EXIST_CODE = 500009;
|
public static final int USER_LEADER_IS_EXIST_CODE = 500009;
|
||||||
public static final String USER_LEADER_IS_EXIST_MSG = "抱歉,当前机构已经存在经理";
|
public static final String USER_LEADER_IS_EXIST_MSG = "抱歉,当前机构已经存在经理";
|
||||||
|
//验证码错误
|
||||||
|
public static final int USER_JCAPTCHA_ERROR_CODE = 500010;
|
||||||
|
public static final String USER_JCAPTCHA_ERROR_MSG = "验证码错误";
|
||||||
|
//验证码已失效
|
||||||
|
public static final int USER_JCAPTCHA_EXPIRE_CODE = 500011;
|
||||||
|
public static final String USER_JCAPTCHA_EXPIRE_MSG = "验证码已失效";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色信息
|
* 角色信息
|
||||||
|
|||||||
@@ -3,13 +3,14 @@ package com.jsh.erp.controller;
|
|||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.jsh.erp.constants.BusinessConstants;
|
||||||
import com.jsh.erp.constants.ExceptionConstants;
|
import com.jsh.erp.constants.ExceptionConstants;
|
||||||
import com.jsh.erp.datasource.entities.Tenant;
|
import com.jsh.erp.datasource.entities.Tenant;
|
||||||
import com.jsh.erp.datasource.entities.User;
|
import com.jsh.erp.datasource.entities.User;
|
||||||
import com.jsh.erp.datasource.entities.UserEx;
|
import com.jsh.erp.datasource.entities.UserEx;
|
||||||
import com.jsh.erp.datasource.vo.TreeNodeEx;
|
import com.jsh.erp.datasource.vo.TreeNodeEx;
|
||||||
import com.jsh.erp.exception.BusinessParamCheckingException;
|
import com.jsh.erp.exception.BusinessParamCheckingException;
|
||||||
import com.jsh.erp.service.log.LogService;
|
import com.jsh.erp.exception.BusinessRunTimeException;
|
||||||
import com.jsh.erp.service.redis.RedisService;
|
import com.jsh.erp.service.redis.RedisService;
|
||||||
import com.jsh.erp.service.role.RoleService;
|
import com.jsh.erp.service.role.RoleService;
|
||||||
import com.jsh.erp.service.tenant.TenantService;
|
import com.jsh.erp.service.tenant.TenantService;
|
||||||
@@ -31,6 +32,7 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;
|
import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;
|
||||||
|
|
||||||
@@ -63,13 +65,15 @@ public class UserController {
|
|||||||
|
|
||||||
@PostMapping(value = "/login")
|
@PostMapping(value = "/login")
|
||||||
@ApiOperation(value = "登录")
|
@ApiOperation(value = "登录")
|
||||||
public BaseResponseInfo login(@RequestBody User userParam,
|
public BaseResponseInfo login(@RequestBody UserEx userParam, HttpServletRequest request)throws Exception {
|
||||||
HttpServletRequest request)throws Exception {
|
|
||||||
BaseResponseInfo res = new BaseResponseInfo();
|
BaseResponseInfo res = new BaseResponseInfo();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> data = userService.login(userParam, request);
|
userService.validateCaptcha(userParam.getCode(), userParam.getUuid());
|
||||||
|
Map<String, Object> data = userService.login(userParam.getLoginName().trim(), userParam.getPassword().trim(), request);
|
||||||
res.code = 200;
|
res.code = 200;
|
||||||
res.data = data;
|
res.data = data;
|
||||||
|
} catch (BusinessRunTimeException e) {
|
||||||
|
throw new BusinessRunTimeException(e.getCode(), e.getMessage());
|
||||||
} catch(Exception e){
|
} catch(Exception e){
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
res.code = 500;
|
res.code = 500;
|
||||||
@@ -91,7 +95,7 @@ public class UserController {
|
|||||||
res.data = "微信未绑定";
|
res.data = "微信未绑定";
|
||||||
} else {
|
} else {
|
||||||
logger.info("微信登录:" + user.getLoginName());
|
logger.info("微信登录:" + user.getLoginName());
|
||||||
Map<String, Object> data = userService.login(user, request);
|
Map<String, Object> data = userService.login(user.getLoginName().trim(), user.getPassword().trim(), request);
|
||||||
res.code = 200;
|
res.code = 200;
|
||||||
res.data = data;
|
res.data = data;
|
||||||
}
|
}
|
||||||
@@ -313,6 +317,7 @@ public class UserController {
|
|||||||
HttpServletRequest request)throws Exception{
|
HttpServletRequest request)throws Exception{
|
||||||
JSONObject result = ExceptionConstants.standardSuccess();
|
JSONObject result = ExceptionConstants.standardSuccess();
|
||||||
ue.setUsername(ue.getLoginName());
|
ue.setUsername(ue.getLoginName());
|
||||||
|
userService.validateCaptcha(ue.getCode(), ue.getUuid());
|
||||||
userService.checkLoginName(ue); //检查登录名
|
userService.checkLoginName(ue); //检查登录名
|
||||||
ue = userService.registerUser(ue,manageRoleId,request);
|
ue = userService.registerUser(ue,manageRoleId,request);
|
||||||
return result;
|
return result;
|
||||||
@@ -408,13 +413,13 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取随机校验码
|
* 获取随机校验码【后续会废弃】
|
||||||
* @param response
|
* @param response
|
||||||
* @param key
|
* @param key
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/randomImage/{key}")
|
@GetMapping(value = "/randomImage/{key}")
|
||||||
@ApiOperation(value = "获取随机校验码")
|
@ApiOperation(value = "获取随机校验码【后续会废弃】")
|
||||||
public BaseResponseInfo randomImage(HttpServletResponse response,@PathVariable String key){
|
public BaseResponseInfo randomImage(HttpServletResponse response,@PathVariable String key){
|
||||||
BaseResponseInfo res = new BaseResponseInfo();
|
BaseResponseInfo res = new BaseResponseInfo();
|
||||||
try {
|
try {
|
||||||
@@ -433,6 +438,34 @@ public class UserController {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取随机校验码
|
||||||
|
* @param response
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping(value = "/randomImage")
|
||||||
|
@ApiOperation(value = "获取随机校验码")
|
||||||
|
public BaseResponseInfo randomImage(HttpServletResponse response){
|
||||||
|
BaseResponseInfo res = new BaseResponseInfo();
|
||||||
|
try {
|
||||||
|
Map<String, Object> data = new HashMap<>();
|
||||||
|
String uuid = UUID.randomUUID().toString().replaceAll("-", "") + "";
|
||||||
|
String verifyKey = BusinessConstants.CAPTCHA_CODE_KEY + uuid;
|
||||||
|
String codeNum = Tools.getCharAndNum(4);
|
||||||
|
redisService.storageCaptchaObject(verifyKey, codeNum);
|
||||||
|
String base64 = RandImageUtil.generate(codeNum);
|
||||||
|
data.put("uuid", uuid);
|
||||||
|
data.put("base64", base64);
|
||||||
|
res.code = 200;
|
||||||
|
res.data = data;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
res.code = 500;
|
||||||
|
res.data = "获取失败";
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量设置状态-启用或者禁用
|
* 批量设置状态-启用或者禁用
|
||||||
* @param jsonObject
|
* @param jsonObject
|
||||||
|
|||||||
@@ -30,6 +30,16 @@ public class UserEx extends User{
|
|||||||
|
|
||||||
private String leaderFlagStr;
|
private String leaderFlagStr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码
|
||||||
|
*/
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 唯一标识
|
||||||
|
*/
|
||||||
|
private String uuid;
|
||||||
|
|
||||||
public String getOrgAbr() {
|
public String getOrgAbr() {
|
||||||
return orgAbr;
|
return orgAbr;
|
||||||
}
|
}
|
||||||
@@ -109,4 +119,20 @@ public class UserEx extends User{
|
|||||||
public void setLeaderFlagStr(String leaderFlagStr) {
|
public void setLeaderFlagStr(String leaderFlagStr) {
|
||||||
this.leaderFlagStr = leaderFlagStr;
|
this.leaderFlagStr = leaderFlagStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCode(String code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUuid() {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUuid(String uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.jsh.erp.constants.BusinessConstants;
|
|||||||
import com.jsh.erp.utils.StringUtil;
|
import com.jsh.erp.utils.StringUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.ValueOperations;
|
||||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -62,6 +63,19 @@ public class RedisService {
|
|||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得缓存的基本对象。
|
||||||
|
*
|
||||||
|
* @param key 缓存键值
|
||||||
|
* @return 缓存键值对应的数据
|
||||||
|
*/
|
||||||
|
public <T> T getCacheObject(final String key)
|
||||||
|
{
|
||||||
|
ValueOperations<String, T> operation = redisTemplate.opsForValue();
|
||||||
|
return operation.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author jisheng hua
|
* @author jisheng hua
|
||||||
* description:
|
* description:
|
||||||
@@ -77,6 +91,29 @@ public class RedisService {
|
|||||||
redisTemplate.opsForHash().put(token, key, obj.toString());
|
redisTemplate.opsForHash().put(token, key, obj.toString());
|
||||||
redisTemplate.expire(token, BusinessConstants.MAX_SESSION_IN_SECONDS, TimeUnit.SECONDS);
|
redisTemplate.expire(token, BusinessConstants.MAX_SESSION_IN_SECONDS, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author jisheng hua
|
||||||
|
* description:
|
||||||
|
* 将信息放入session或者redis中
|
||||||
|
* @date: 2024/5/28 20:10
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public void storageCaptchaObject(String verifyKey, String codeNum) {
|
||||||
|
//把验证码放到redis中
|
||||||
|
redisTemplate.opsForValue().set(verifyKey, codeNum, BusinessConstants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除单个对象
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
public boolean deleteObject(final String key)
|
||||||
|
{
|
||||||
|
return redisTemplate.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author jisheng hua
|
* @author jisheng hua
|
||||||
* description:
|
* description:
|
||||||
|
|||||||
@@ -285,19 +285,42 @@ public class UserService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验验证码
|
||||||
|
* @param code 验证码
|
||||||
|
* @param uuid 唯一标识
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public void validateCaptcha(String code, String uuid) {
|
||||||
|
if(StringUtil.isNotEmpty(code) && StringUtil.isNotEmpty(uuid)) {
|
||||||
|
code = code.trim();
|
||||||
|
uuid = uuid.trim();
|
||||||
|
String verifyKey = BusinessConstants.CAPTCHA_CODE_KEY + uuid;
|
||||||
|
String captcha = redisService.getCacheObject(verifyKey);
|
||||||
|
redisService.deleteObject(verifyKey);
|
||||||
|
if (captcha == null) {
|
||||||
|
logger.error("异常码[{}],异常提示[{}]", ExceptionConstants.USER_JCAPTCHA_EXPIRE_CODE, ExceptionConstants.USER_JCAPTCHA_EXPIRE_MSG);
|
||||||
|
throw new BusinessRunTimeException(ExceptionConstants.USER_JCAPTCHA_EXPIRE_CODE, ExceptionConstants.USER_JCAPTCHA_EXPIRE_MSG);
|
||||||
|
}
|
||||||
|
if (!code.equalsIgnoreCase(captcha)) {
|
||||||
|
logger.error("异常码[{}],异常提示[{}]", ExceptionConstants.USER_JCAPTCHA_ERROR_CODE, ExceptionConstants.USER_JCAPTCHA_ERROR_MSG);
|
||||||
|
throw new BusinessRunTimeException(ExceptionConstants.USER_JCAPTCHA_ERROR_CODE, ExceptionConstants.USER_JCAPTCHA_ERROR_MSG);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录
|
* 用户登录
|
||||||
* @param userParam
|
* @param loginName
|
||||||
|
* @param password
|
||||||
* @param request
|
* @param request
|
||||||
* @return
|
* @return
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public Map<String, Object> login(User userParam, HttpServletRequest request) throws Exception {
|
public Map<String, Object> login(String loginName, String password, HttpServletRequest request) throws Exception {
|
||||||
Map<String, Object> data = new HashMap<>();
|
Map<String, Object> data = new HashMap<>();
|
||||||
String msgTip = "";
|
String msgTip = "";
|
||||||
User user=null;
|
User user = null;
|
||||||
String loginName = userParam.getLoginName().trim();
|
|
||||||
String password = userParam.getPassword().trim();
|
|
||||||
//判断用户是否已经登录过,登录过不再处理
|
//判断用户是否已经登录过,登录过不再处理
|
||||||
Object userId = redisService.getObjectFromSessionByKey(request,"userId");
|
Object userId = redisService.getObjectFromSessionByKey(request,"userId");
|
||||||
if (userId != null) {
|
if (userId != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user