Commit 3d65d66d authored by muhong's avatar muhong

修改 小程序缓存

parent 69ffc0c9
...@@ -7,7 +7,19 @@ public class CacheCodeConstants { ...@@ -7,7 +7,19 @@ public class CacheCodeConstants {
/** /**
* 微信接口调用凭证access_token * 微信接口调用凭证access_token
*/ */
public static final String WX_ACCESS_TOKEN = "quanku:wx:access_token"; public static final String WX_ACCESS_TOKEN = "cjxc:wx:access_token";
/**
* 缓存用户登录验证码
* <p>{0}:手机号</p>
*/
public static final String USER_LOGIN_SMS_KEY = "cjxc:wx:user:sms:login:{0}";
/**
* 缓存用户修改密码验证码
* <p>{0}:手机号</p>
*/
public static final String USER_UPDATE_SMS_KEY = "cjxc:wx:user:sms:update:{0}";
/** /**
* 短信验证码类型枚举 * 短信验证码类型枚举
......
package com.wwdz.ch.wx.impl; package com.wwdz.ch.wx.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.wwdz.ch.core.captcha.CaptchaCodeManager;
import com.wwdz.ch.core.consts.ResultCode; import com.wwdz.ch.core.consts.ResultCode;
import com.wwdz.ch.core.notify.AliSmsSender; import com.wwdz.ch.core.notify.AliSmsSender;
import com.wwdz.ch.core.notify.NotifyService; import com.wwdz.ch.core.notify.NotifyService;
...@@ -14,16 +13,17 @@ import com.wwdz.ch.core.util.UUID; ...@@ -14,16 +13,17 @@ import com.wwdz.ch.core.util.UUID;
import com.wwdz.ch.core.util.bcrypt.BCryptPasswordEncoder; import com.wwdz.ch.core.util.bcrypt.BCryptPasswordEncoder;
import com.wwdz.ch.db.dao.UserDao; import com.wwdz.ch.db.dao.UserDao;
import com.wwdz.ch.db.domain.User; import com.wwdz.ch.db.domain.User;
import com.wwdz.ch.db.dto.request.FavoritesRequestDto;
import com.wwdz.ch.wx.constant.CacheCodeConstants; import com.wwdz.ch.wx.constant.CacheCodeConstants;
import com.wwdz.ch.wx.dao.UserInfo; import com.wwdz.ch.wx.dao.UserInfo;
import com.wwdz.ch.wx.dao.UserToken; import com.wwdz.ch.wx.dao.UserToken;
import com.wwdz.ch.db.dto.request.FavoritesRequestDto;
import com.wwdz.ch.wx.entity.request.UserRequestDto; import com.wwdz.ch.wx.entity.request.UserRequestDto;
import com.wwdz.ch.wx.manager.UserTokenManager;
import com.wwdz.ch.wx.service.FavoritesService; import com.wwdz.ch.wx.service.FavoritesService;
import com.wwdz.ch.wx.service.UserService; import com.wwdz.ch.wx.service.UserService;
import com.wwdz.ch.wx.manager.UserTokenManager;
import com.wwdz.ch.wx.service.WxLoginService; import com.wwdz.ch.wx.service.WxLoginService;
import com.wwdz.ch.wx.util.IpUtil; import com.wwdz.ch.wx.util.IpUtil;
import com.wwdz.ch.wx.util.RedisUtil;
import com.wwdz.ch.wx.util.StringUtil; import com.wwdz.ch.wx.util.StringUtil;
import com.wwdz.ch.wx.util.WxResponseCode; import com.wwdz.ch.wx.util.WxResponseCode;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -37,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -37,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
...@@ -58,6 +59,8 @@ public class UserServiceImpl implements UserService { ...@@ -58,6 +59,8 @@ public class UserServiceImpl implements UserService {
private FavoritesService favoritesService; private FavoritesService favoritesService;
@Autowired @Autowired
private WxLoginService wxLoginService; private WxLoginService wxLoginService;
@Autowired
private RedisUtil redisUtil;
@Value("${avatar-url}") @Value("${avatar-url}")
private String avatarUrl; private String avatarUrl;
...@@ -181,6 +184,16 @@ public class UserServiceImpl implements UserService { ...@@ -181,6 +184,16 @@ public class UserServiceImpl implements UserService {
logger.error("请求验证码出错:{}", WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc()); logger.error("请求验证码出错:{}", WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc());
return Result.failed(WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc()); return Result.failed(WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc());
} }
String key = "";
if (dto.getSmsType().equals(CacheCodeConstants.SmsTypeEnum.LOGIN.getCode())) {
key = MessageFormat.format(CacheCodeConstants.USER_LOGIN_SMS_KEY, dto.getMobile());
} else {
key = MessageFormat.format(CacheCodeConstants.USER_UPDATE_SMS_KEY, dto.getMobile());
}
if (redisUtil.hasKey(key)) {
return Result.failed("验证码请求过于频繁");
}
String activeProfile = applicationContext.getEnvironment().getActiveProfiles()[0]; String activeProfile = applicationContext.getEnvironment().getActiveProfiles()[0];
String code = "123456"; String code = "123456";
if (!StringUtils.equals("dev", activeProfile)) { if (!StringUtils.equals("dev", activeProfile)) {
...@@ -190,16 +203,11 @@ public class UserServiceImpl implements UserService { ...@@ -190,16 +203,11 @@ public class UserServiceImpl implements UserService {
code = CharUtil.getRandomNum(6); code = CharUtil.getRandomNum(6);
SmsResult smsResult = aliSmsSender.sendWithTemplate(dto.getMobile(), code); SmsResult smsResult = aliSmsSender.sendWithTemplate(dto.getMobile(), code);
if (smsResult != null) { if (smsResult != null) {
logger.info("*****腾讯云短信发送->请求验证码,短信发送结果:{}", JSONObject.toJSONString(smsResult)); logger.info("*****阿里云短信发送->请求验证码,短信发送结果:{}", JSONObject.toJSONString(smsResult));
} }
} }
boolean successful = CaptchaCodeManager.addToCache(dto.getSmsType() + dto.getMobile(), code,1); redisUtil.set(key, code, 60);
if (!successful) {
logger.error("请求验证码出错:{}", WxResponseCode.AUTH_CAPTCHA_FREQUENCY.desc());
return Result.failed(WxResponseCode.AUTH_CAPTCHA_FREQUENCY.desc());
}
logger.info("【请求结束】请求验证码成功"); logger.info("【请求结束】请求验证码成功");
return Result.success(); return Result.success();
} catch (Exception e) { } catch (Exception e) {
...@@ -212,11 +220,12 @@ public class UserServiceImpl implements UserService { ...@@ -212,11 +220,12 @@ public class UserServiceImpl implements UserService {
@Override @Override
public Result loginByMobile(UserRequestDto dto, HttpServletRequest request) { public Result loginByMobile(UserRequestDto dto, HttpServletRequest request) {
try { try {
String mobileCode = CaptchaCodeManager.getCachedCaptcha(CacheCodeConstants.SmsTypeEnum.LOGIN.getCode() + dto.getMobile()); String key = MessageFormat.format(CacheCodeConstants.USER_LOGIN_SMS_KEY, dto.getMobile());
if (StringUtils.isBlank(mobileCode)) { Object object = redisUtil.get(key);
if (Objects.isNull(object)) {
return Result.failed("验证码已过期"); return Result.failed("验证码已过期");
} }
if (!StringUtils.equals(dto.getSmsCode(), mobileCode)) { if (!StringUtils.equals(dto.getSmsCode(), object.toString())) {
return Result.failed("验证码不正确"); return Result.failed("验证码不正确");
} }
...@@ -294,6 +303,8 @@ public class UserServiceImpl implements UserService { ...@@ -294,6 +303,8 @@ public class UserServiceImpl implements UserService {
result.put("userInfo", userInfo); result.put("userInfo", userInfo);
logger.info("【请求结束】手机号登录,响应结果:{}", JSONObject.toJSONString(result)); logger.info("【请求结束】手机号登录,响应结果:{}", JSONObject.toJSONString(result));
// 清除验证码缓存
redisUtil.del(key);
return Result.success(result); return Result.success(result);
} catch (Exception e) { } catch (Exception e) {
logger.error("手机号登录失败", e); logger.error("手机号登录失败", e);
...@@ -392,11 +403,12 @@ public class UserServiceImpl implements UserService { ...@@ -392,11 +403,12 @@ public class UserServiceImpl implements UserService {
if (!StringUtils.equals(user.getMobile(), dto.getBeforeMobile())) { if (!StringUtils.equals(user.getMobile(), dto.getBeforeMobile())) {
return Result.failed("原手机号码不正确"); return Result.failed("原手机号码不正确");
} }
String mobileCode = CaptchaCodeManager.getCachedCaptcha(CacheCodeConstants.SmsTypeEnum.UPDATE.getCode() + dto.getAfterMobile()); String key = MessageFormat.format(CacheCodeConstants.USER_UPDATE_SMS_KEY, dto.getAfterMobile());
if (StringUtils.isBlank(mobileCode)) { Object object = redisUtil.get(key);
if (Objects.isNull(object)) {
return Result.failed("验证码已过期"); return Result.failed("验证码已过期");
} }
if (!StringUtils.equals(dto.getSmsCode(), mobileCode)) { if (!StringUtils.equals(dto.getSmsCode(), object.toString())) {
return Result.failed("验证码不正确"); return Result.failed("验证码不正确");
} }
if (userDao.isExistedByMobile(dto.getBeforeMobile(), dto.getAfterMobile())) { if (userDao.isExistedByMobile(dto.getBeforeMobile(), dto.getAfterMobile())) {
...@@ -405,6 +417,8 @@ public class UserServiceImpl implements UserService { ...@@ -405,6 +417,8 @@ public class UserServiceImpl implements UserService {
user.setMobile(dto.getAfterMobile()); user.setMobile(dto.getAfterMobile());
userDao.updateById(user); userDao.updateById(user);
logger.info("【请求结束】用户修改手机号成功"); logger.info("【请求结束】用户修改手机号成功");
// 清除验证码缓存
redisUtil.del(key);
return Result.success(); return Result.success();
} catch (Exception e) { } catch (Exception e) {
logger.error("用户修改手机号失败", e); logger.error("用户修改手机号失败", e);
......
...@@ -24,9 +24,9 @@ public class UserTokenManager { ...@@ -24,9 +24,9 @@ public class UserTokenManager {
private Gson gson = new Gson(); private Gson gson = new Gson();
private static final String TOKEN_PREFIX = "token_prefix:"; private static final String TOKEN_PREFIX = "cjxc:wx:token:token_prefix:";
private static final String TOKEN_ID_PREFIX = "token_id_prefix:"; private static final String TOKEN_ID_PREFIX = "cjxc:wx:token:token_id_prefix:";
private static Map<String, UserToken> tokenMap = new HashMap<>(); private static Map<String, UserToken> tokenMap = new HashMap<>();
private static Map<Long, UserToken> idMap = new HashMap<>(); private static Map<Long, UserToken> idMap = new HashMap<>();
private static long SURVIVAL_TIME = 86400; private static long SURVIVAL_TIME = 86400;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment