/** * 用户注册发送短信 */ @PostMapping("/sendMassage") @ApiOperationSupport(order = 12) @ApiOperation(value = "用户注册发送短信", notes = "传入electricMassage") //@PreAuth(RoleConstant.HAS_ROLE_ADMIN) public R sendMassage(String phone) throws IOException { Boolean flag = false; HttpClient client = new HttpClient(); PostMethod post = new PostMethod("http://xxxxxxxx:8080/xxxx.do"); post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码 String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000); NameValuePair[] data = { new NameValuePair("corp_id", "xxx"), new NameValuePair("corp_pwd", "xxx"), new NameValuePair("corp_service", "1069106967491"), new NameValuePair("mobile", phone), new NameValuePair("msg_content", "尊敬的用户,您好,您的验证码为:"+ verifyCode + ",验证码失效时间为5分钟,若非本人操作,请忽略此短信。"), new NameValuePair("corp_msg_id", ""), new NameValuePair("ext", "") }; post.setRequestBody(data); client.executeMethod(post); Header[] headers = post.getResponseHeaders(); for (Header h : headers) { System.out.println(h.toString()); } int statusCode = post.getStatusCode(); if (statusCode == 200) { flag = saveCaptcha(verifyCode, phone); if (flag) { String result = new String(post.getResponseBodyAsString()); System.out.println(result); post.releaseConnection(); } } return flag ? R.success(SEND_SUCCESS) : R.fail(SEND_FAIL); } /** * 用户注册接口 * * @param user 用户信息 * @return */ @PostMapping("/regist") @ApiOperationSupport(order = 12) @ApiOperation(value = "用户注册接口", notes = "传入code") //@PreAuth(RoleConstant.HAS_ROLE_ADMIN) public R regist(@Valid @RequestBody User user) { boolean validate = checkCaptcha(user.getCaptcha(), user.getPhone()); Boolean index = false; if (validate) { CacheUtil.clear(USER_CACHE); index = userService.submit(user); } return R.status(index); } //讲注册用的验证码存入到redis public Boolean saveCaptcha(String captcha, String phone) { try { //将验证码存储在redis中,并且设置过期时间5分钟 redisTemplate.boundValueOps(phone).set(captcha, 300, TimeUnit.SECONDS); return true; } catch (Exception ex) { throw new RuntimeException(ex); } } //验证短信验证码 public Boolean checkCaptcha(String captcha, String phone) { try { if (captcha.equals(redisTemplate.boundValueOps(phone).get())) { return true; } else { return false; } } catch (Exception ex) { throw new RuntimeException(ex); } }