|
@@ -62,10 +62,6 @@ import static com.ydtech.constants.RedisConstant.PHONE_VERIFICATION_CODE_KEY_TIM
|
|
|
public class SysLoginController {
|
|
public class SysLoginController {
|
|
|
|
|
|
|
|
// https://wenku.baidu.com/view/d7b845cf49fe04a1b0717fd5360cba1aa9118c49.html
|
|
// https://wenku.baidu.com/view/d7b845cf49fe04a1b0717fd5360cba1aa9118c49.html
|
|
|
- private static final ExpiringMap<String, String> redis = ExpiringMap.builder()
|
|
|
|
|
- .maxSize(100)
|
|
|
|
|
- .expiration(20, TimeUnit.SECONDS)
|
|
|
|
|
- .variableExpiration().expirationPolicy(ExpirationPolicy.CREATED).build();
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private Producer producer;
|
|
private Producer producer;
|
|
@@ -77,7 +73,7 @@ public class SysLoginController {
|
|
|
private SysMsgLogService sysMsgLogService;
|
|
private SysMsgLogService sysMsgLogService;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
|
+ private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private EsmUserInternalService esmUserInternalService;
|
|
private EsmUserInternalService esmUserInternalService;
|
|
@@ -90,16 +86,13 @@ public class SysLoginController {
|
|
|
public void captcha(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
|
|
public void captcha(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
|
|
|
response.setHeader("Cache-Control", "no-store, no-cache");
|
|
response.setHeader("Cache-Control", "no-store, no-cache");
|
|
|
response.setContentType("image/jpeg");
|
|
response.setContentType("image/jpeg");
|
|
|
-
|
|
|
|
|
// 生成文字验证码
|
|
// 生成文字验证码
|
|
|
String text = producer.createText();
|
|
String text = producer.createText();
|
|
|
// 生成图片验证码
|
|
// 生成图片验证码
|
|
|
BufferedImage image = producer.createImage(text);
|
|
BufferedImage image = producer.createImage(text);
|
|
|
// 保存到验证码到 session
|
|
// 保存到验证码到 session
|
|
|
- request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, text);
|
|
|
|
|
- redis.put(IPUtil.getIpAddr(request), text, 120, TimeUnit.SECONDS);
|
|
|
|
|
- System.out.println("存入session验证码为:" + text);
|
|
|
|
|
- log.info("存入session验证码为:" + text);
|
|
|
|
|
|
|
+ redisTemplate.opsForValue().set(IPUtil.getIpAddr(request), text, 120, TimeUnit.SECONDS);
|
|
|
|
|
+ log.info("存入redis验证码为:" + text);
|
|
|
ServletOutputStream out = response.getOutputStream();
|
|
ServletOutputStream out = response.getOutputStream();
|
|
|
ImageIO.write(image, "jpg", out);
|
|
ImageIO.write(image, "jpg", out);
|
|
|
IOUtils.closeQuietly(out);
|
|
IOUtils.closeQuietly(out);
|
|
@@ -114,20 +107,9 @@ public class SysLoginController {
|
|
|
String username = loginBean.getAccount();
|
|
String username = loginBean.getAccount();
|
|
|
String password = loginBean.getPassword();
|
|
String password = loginBean.getPassword();
|
|
|
String captcha = loginBean.getCaptcha();
|
|
String captcha = loginBean.getCaptcha();
|
|
|
-
|
|
|
|
|
- // 从session中获取之前保存的验证码跟前台传来的验证码进行匹配
|
|
|
|
|
- Object kaptcha = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
|
|
|
|
|
- System.out.println("页面验证码:" + captcha + "===" + "session验证码:" + kaptcha);
|
|
|
|
|
- log.info("页面验证码:" + captcha + "===" + "session验证码:" + kaptcha);
|
|
|
|
|
-
|
|
|
|
|
- if (kaptcha == null) {
|
|
|
|
|
- // 从redis中获取
|
|
|
|
|
- kaptcha = redis.get(IPUtil.getIpAddr(request));
|
|
|
|
|
- log.info("session获取失败,改为redis中获取!页面验证码:" + captcha + "===" + "redis验证码:" + kaptcha);
|
|
|
|
|
- if (kaptcha == null) {
|
|
|
|
|
- return HttpResult.error("验证码已失效");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 从redis中获取
|
|
|
|
|
+ String kaptcha = redisTemplate.opsForValue().get(IPUtil.getIpAddr(request));
|
|
|
|
|
+ log.info("页面验证码:" + captcha + "===" + "redis验证码:" + kaptcha);
|
|
|
if (!captcha.equals(kaptcha)) {
|
|
if (!captcha.equals(kaptcha)) {
|
|
|
return HttpResult.error("验证码不正确");
|
|
return HttpResult.error("验证码不正确");
|
|
|
}
|
|
}
|
|
@@ -194,7 +176,7 @@ public class SysLoginController {
|
|
|
// ArrayList<Object> params = new ArrayList<Object>();
|
|
// ArrayList<Object> params = new ArrayList<Object>();
|
|
|
// params.add(phone);
|
|
// params.add(phone);
|
|
|
// List<SysMsgLog> msgList = sysMsgLogService.selectList(whereStr, params.toArray());
|
|
// List<SysMsgLog> msgList = sysMsgLogService.selectList(whereStr, params.toArray());
|
|
|
- String msg = stringRedisTemplate.opsForValue().get(PHONE_VERIFICATION_CODE_KEY + phone);
|
|
|
|
|
|
|
+ String msg = redisTemplate.opsForValue().get(PHONE_VERIFICATION_CODE_KEY + phone);
|
|
|
|
|
|
|
|
if (msg == null || msg.isEmpty()) {
|
|
if (msg == null || msg.isEmpty()) {
|
|
|
return HttpResult.error("验证码已失效");
|
|
return HttpResult.error("验证码已失效");
|
|
@@ -239,7 +221,7 @@ public class SysLoginController {
|
|
|
// request.getSession().setAttribute("PHONE_SESSION_KEY", String.valueOf(randNumber));
|
|
// request.getSession().setAttribute("PHONE_SESSION_KEY", String.valueOf(randNumber));
|
|
|
// 保存验证码到数据库
|
|
// 保存验证码到数据库
|
|
|
// 保存验证码到redis
|
|
// 保存验证码到redis
|
|
|
- stringRedisTemplate.opsForValue().set(PHONE_VERIFICATION_CODE_KEY + phone, String.valueOf(randNumber), PHONE_VERIFICATION_CODE_KEY_TIME, TimeUnit.MINUTES);
|
|
|
|
|
|
|
+ redisTemplate.opsForValue().set(PHONE_VERIFICATION_CODE_KEY + phone, String.valueOf(randNumber), PHONE_VERIFICATION_CODE_KEY_TIME, TimeUnit.MINUTES);
|
|
|
|
|
|
|
|
// SysMsgLog sysMsgLog = new SysMsgLog();
|
|
// SysMsgLog sysMsgLog = new SysMsgLog();
|
|
|
// Date date = new Date();
|
|
// Date date = new Date();
|