1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
public class CheckSendMailHelper {
private static final Logger logger = LoggerFactory.getLogger("phoneCode");
private static final int PHONE_REQUEST_TIME = 60 * 1000;
private static final int CAPTCHA_REQUEST_TIME = 60 * 1000;
private static final int PHONE_REQUEST_WAIT_TIME = 60 * 1000 * 24 * 60;
private static final int PHONE_REQUEST_INIT_SIZE = 1;
private static final String CONTEXT_MAP = "CONTEXT_MAP";
private static final String PHONE_MOUDULE_FORMAT = "%s-%s";
private static final String PHONE_MOUDULE_TIMER_FORMAT = "%s-%s-timer";
private static final String SMS_MODULE_TEMPLATE = "sms.modules.%s";
private static final String CAPTCHA_MOUDULE_FORMAT = "%s-%s-captcha";
private static final String CAPTCHA_MOUDULE_TIMER_FORMAT = "%s-%s-captcha-timer";
private static final String PHONE_MOUDULE_FLAG = "phoneModule";
private static final String CAPCHACODE = "capchaCode";
private static final boolean CAPTCHA_ENABLE = Setter.getBoolean("captcha.enable");
public static void addCapcha(String phoneCode, String code, HttpServletRequest request) { if (!CAPTCHA_ENABLE) { logger.info("请开启图形验证码校验之后,再配合本工具类方法使用!"); return; } ServletContext servletContext = request.getServletContext(); Map<String, Map<String, Object>> attribute = initServletContextMap(servletContext); Date now = new Date(); String phoneMouduleFlag = checkExistsAndGetModule(request); if (StringUtils.isBlank(phoneMouduleFlag)) { return; } String modulePhone = String.format(PHONE_MOUDULE_FORMAT, phoneCode, phoneMouduleFlag); String capchaModule = String.format(CAPTCHA_MOUDULE_FORMAT, phoneCode, phoneMouduleFlag); String capchaModuleTimer = String.format(CAPTCHA_MOUDULE_TIMER_FORMAT, phoneCode, phoneMouduleFlag); if (!attribute.containsKey(modulePhone)) { HashMap<String, Object> stringObjectHashMap = new HashMap<>(); stringObjectHashMap.put(capchaModule, code); stringObjectHashMap.put(capchaModuleTimer, now); attribute.put(modulePhone, stringObjectHashMap); } else { Map<String, Object> stringObjectMap = attribute.get(modulePhone); stringObjectMap.put(capchaModule, code); stringObjectMap.put(capchaModuleTimer, now); } }
public static boolean checkContextMap(Map<String, Object> result, HttpServletRequest request, String phoneCode) { ServletContext servletContext = request.getServletContext(); Map<String, Map<String, Object>> attribute = initServletContextMap(servletContext); Date now = new Date(); String phoneMouduleFlag = checkExistsAndGetModule(request); if (phoneMouduleFlag == null) { result.put("result", SmsRequestStatusEnum.RESULT_STATUS_9.getCode()); result.put("msg", SmsRequestStatusEnum.RESULT_STATUS_9.getName()); return false; } String modulePhone = String.format(PHONE_MOUDULE_FORMAT, phoneCode, phoneMouduleFlag); String modulePhoneTimer = String.format(PHONE_MOUDULE_TIMER_FORMAT, phoneCode, phoneMouduleFlag); int moduleCount = Setter.getInt(String.format(SMS_MODULE_TEMPLATE, phoneMouduleFlag)); if (!attribute.containsKey(modulePhone)) { HashMap<String, Object> stringObjectHashMap = new HashMap<>(); stringObjectHashMap.put(modulePhone, PHONE_REQUEST_INIT_SIZE); stringObjectHashMap.put(modulePhoneTimer, now); attribute.put(modulePhone, stringObjectHashMap); return true; } else { Map<String, Object> objectMap = attribute.get(modulePhone); if (CAPTCHA_ENABLE) { if (!checkCatpchaCode(result, request, phoneCode, now, phoneMouduleFlag, objectMap)) { return true; } } Object count = objectMap.get(modulePhone); Object timer = objectMap.get(modulePhoneTimer); if (Objects.isNull(count) || Objects.isNull(timer)) { objectMap.put(modulePhone, PHONE_REQUEST_INIT_SIZE); objectMap.put(modulePhoneTimer, now); return true; } Integer integer = Integer.valueOf(objectMap.get(modulePhone).toString()); Date time = (Date) timer; if(!checkLastGetTime(result, now, time)){ return false; } if ((now.getTime() - time.getTime()) > PHONE_REQUEST_WAIT_TIME) { objectMap.put(modulePhone, PHONE_REQUEST_INIT_SIZE); objectMap.put(modulePhoneTimer, now); return true; } if (integer > moduleCount) { if (now.getTime() - time.getTime() > PHONE_REQUEST_WAIT_TIME) { objectMap.put(modulePhone, PHONE_REQUEST_INIT_SIZE); objectMap.put(modulePhoneTimer, now); return true; } result.put("result", SmsRequestStatusEnum.RESULT_STATUS_10.getCode()); result.put("msg", SmsRequestStatusEnum.RESULT_STATUS_10.getName()); return false; } objectMap.put(modulePhone, integer + PHONE_REQUEST_INIT_SIZE); objectMap.put(modulePhoneTimer, now); } return true; }
private static boolean checkCatpchaCode(Map<String, Object> result, HttpServletRequest request, String phoneCode, Date now, String phoneMouduleFlag, Map<String, Object> objectMap) { String capchaModule = String.format(CAPTCHA_MOUDULE_FORMAT, phoneCode, phoneMouduleFlag); String capchaModuleTimer = String.format(CAPTCHA_MOUDULE_TIMER_FORMAT, phoneCode, phoneMouduleFlag); Date captchaCodeValidPeriod = (Date) objectMap.get(capchaModuleTimer); String requestCaptchaCode = RequestHelper.getString(CAPCHACODE, request); Object requestCode = objectMap.get(capchaModule); if (StringUtils.isBlank(requestCaptchaCode) || Objects.isNull(requestCode)) { result.put("result", SmsRequestStatusEnum.RESULT_STATUS_8.getCode()); result.put("msg", SmsRequestStatusEnum.RESULT_STATUS_8.getName()); return false; } if (!Objects.equals(requestCaptchaCode, requestCode.toString()) || (now.getTime() - captchaCodeValidPeriod.getTime() > (CAPTCHA_REQUEST_TIME))) { result.put("result", SmsRequestStatusEnum.RESULT_STATUS_8.getCode()); result.put("msg", SmsRequestStatusEnum.RESULT_STATUS_8.getName()); return false; } else { objectMap.put(capchaModule, null); } return true; }
private static boolean checkLastGetTime(Map<String, Object> result, Date now, Date lastSend) { if ((now.getTime() - lastSend.getTime()) <= PHONE_REQUEST_TIME) { result.put("result", SmsRequestStatusEnum.RESULT_STATUS_7.getCode()); result.put("msg", SmsRequestStatusEnum.RESULT_STATUS_7.getName()); return false; } return true; }
private static Map<String, Map<String, Object>> initServletContextMap(ServletContext servletContext) { Map<String, Map<String, Object>> attribute = (Map<String, Map<String, Object>>) servletContext.getAttribute(CONTEXT_MAP); if (Objects.isNull(attribute)) { attribute = new HashMap<>(); servletContext.setAttribute(CONTEXT_MAP, attribute); } return attribute; }
static String checkExistsAndGetModule(HttpServletRequest request) { String phoneMouduleFlag = RequestHelper.getString(PHONE_MOUDULE_FLAG, request); String moduleNo = Setter.getString(String.format(SMS_MODULE_TEMPLATE, phoneMouduleFlag)); if (StringUtils.isBlank(moduleNo)) { logger.info("未找到对应的短信模块,请在xml配置短信模块名称,并在请求参数中加入 phoneModule: 对应模块名称之后再进行请求"); return null; } return phoneMouduleFlag; }
}
|