BadIntentionOpServiceImpl.java 5.1 KB

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