完善验证码和登录的接口

This commit is contained in:
jishenghua
2024-05-29 01:14:48 +08:00
parent 98e728cc55
commit d15b5312e6
6 changed files with 146 additions and 12 deletions

View File

@@ -4,6 +4,7 @@ import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
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.StringRedisSerializer;
import org.springframework.stereotype.Component;
@@ -62,6 +63,19 @@ public class RedisService {
}
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
* description:
@@ -77,6 +91,29 @@ public class RedisService {
redisTemplate.opsForHash().put(token, key, obj.toString());
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
* description:

View File

@@ -285,19 +285,42 @@ public class UserService {
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
* @return
* @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<>();
String msgTip = "";
User user=null;
String loginName = userParam.getLoginName().trim();
String password = userParam.getPassword().trim();
User user = null;
//判断用户是否已经登录过,登录过不再处理
Object userId = redisService.getObjectFromSessionByKey(request,"userId");
if (userId != null) {