BadIntentionOpServiceImpl.java 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.cyksj.service.user.impl;
  2. import cn.hutool.extra.servlet.ServletUtil;
  3. import cn.hutool.http.HttpUtil;
  4. import cn.hutool.json.JSONObject;
  5. import com.cyksj.common.exception.BusinessRuntimeException;
  6. import com.cyksj.common.util.StringUtil;
  7. import com.cyksj.redis.RedisService;
  8. import com.cyksj.service.user.BadIntentionOpService;
  9. import lombok.RequiredArgsConstructor;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import javax.servlet.http.HttpServletRequest;
  15. @Service
  16. @RequiredArgsConstructor
  17. @Slf4j
  18. public class BadIntentionOpServiceImpl implements BadIntentionOpService {
  19. @Value("${smsVerify.apiKey}")
  20. String apiKey;
  21. private final RedisService redisService;
  22. private static final int PHONE_CODE_GET_NUM_DAY_LIMIT = 30;
  23. private static final int PHONE_CODE_GET_NUM_DAY_MAX_BLACK = 50;
  24. private static final int PHONE_LOGIN_CODE_RETRY = 10;
  25. private static final String VERIFY_URL = "https://captcha.luosimao.com/api/site_verify";
  26. @Override
  27. public void checkHandleBadUserOp(String phone, String verifyCode) {
  28. if ("13662979985".equals(phone)) {
  29. throw BusinessRuntimeException.getInstance("异常手机号");
  30. }
  31. String dayKey = RedisService.key.PHONE_CODE_USER_NUM_KEY_DAY.getName() + phone;
  32. if (redisService.hasKey(RedisService.key.PHONE_CODE_BALCK_KEY.getName() + phone)) {
  33. throw BusinessRuntimeException.getInstance("异常手机号");
  34. }
  35. if (redisService.hasKey(dayKey)) {
  36. Long incr = redisService.incr(dayKey, 1l);
  37. if (incr.intValue() >= PHONE_CODE_GET_NUM_DAY_MAX_BLACK) {
  38. log.error("近一天内手机号:{}获取验证码超过50次,移入黑名单", phone);
  39. redisService.set(RedisService.key.PHONE_CODE_BALCK_KEY.getName() + phone, phone);
  40. }
  41. if (incr.intValue() > PHONE_CODE_GET_NUM_DAY_LIMIT) {
  42. log.error("手机号:{}频繁获取短信验证码", phone);
  43. throw BusinessRuntimeException.getInstance("请勿频繁获取短信验证码");
  44. }
  45. if (StringUtils.isBlank(verifyCode)) {
  46. throw BusinessRuntimeException.getInstance("人机校验不通过");
  47. }else {
  48. JSONObject parmas = new JSONObject();
  49. parmas.putOpt("api_key", apiKey);
  50. parmas.putOpt("response", verifyCode);
  51. String post = HttpUtil.post(VERIFY_URL, parmas.toString());
  52. JSONObject res = new JSONObject(post);
  53. if (!res.getStr("res").equals("success")) {
  54. log.error("手机号:{}验证码校验不通过 res:{}", phone, res);
  55. throw BusinessRuntimeException.getInstance("人机校验不通过");
  56. }
  57. }
  58. return;
  59. }
  60. redisService.set(dayKey, 1, RedisService.key.PHONE_CODE_USER_NUM_KEY_DAY.getTimeout());
  61. }
  62. @Override
  63. public void isBadOpLoginPhone(HttpServletRequest request, String phone) {
  64. String ip = StringUtil.getInternalAddressByIP(ServletUtil.getClientIP(request));
  65. String blackIpKey = RedisService.key.BALCK_USER_IP_KEY + ip;
  66. if (redisService.hasKey(blackIpKey)) {
  67. throw BusinessRuntimeException.getInstance("检测到您操作异常,请联系客服");
  68. }
  69. String RetryTimeKey = RedisService.key.PHONE_CODE_RETRY_TIME_KEY.getName() + phone;
  70. if (redisService.hasKey(RetryTimeKey)) {
  71. int i = Integer.parseInt(redisService.get(RetryTimeKey).toString());
  72. if (i >= PHONE_LOGIN_CODE_RETRY) {
  73. //锁住他的ip
  74. redisService.setNx(blackIpKey, ip, RedisService.key.BALCK_USER_IP_KEY.getTimeout());
  75. throw BusinessRuntimeException.getInstance("检测到您操作异常,请联系客服");
  76. } else {
  77. redisService.incr(RetryTimeKey, 1l);
  78. }
  79. } else {
  80. redisService.setNx(RetryTimeKey, 1, RedisService.key.PHONE_CODE_RETRY_TIME_KEY.getTimeout());
  81. }
  82. }
  83. }