SmsController.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package com.yihu.base.security.sms.controller;
  2. import com.yihu.base.security.properties.SecurityProperties;
  3. import com.yihu.base.security.sms.mobile.MobileCheck;
  4. import com.yihu.base.security.sms.process.SmsValidateCodeProcessor;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import org.springframework.web.context.request.ServletWebRequest;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.OutputStream;
  13. import java.io.PrintWriter;
  14. /**
  15. * Created by chenweida on 2017/12/5.
  16. */
  17. @RestController
  18. public class SmsController {
  19. @Autowired
  20. private SmsValidateCodeProcessor smsValidateCodeProcessor;
  21. @Autowired
  22. private MobileCheck mobileCheck;
  23. /**
  24. * 创建验证码
  25. *
  26. * @param request
  27. * @param response
  28. * @throws Exception
  29. */
  30. @GetMapping(SecurityProperties.mobileSendSms)
  31. public void createCode(
  32. HttpServletRequest request,
  33. HttpServletResponse response)
  34. throws Exception {
  35. //获取手机号
  36. String mobile = request.getParameter(SecurityProperties.mobileLoginAccountKey);
  37. //验证手机号是否正确
  38. if (!mobileCheck.checkMobile(mobile)) {
  39. //通过设置响应头控制浏览器以UTF-8的编码显示数据,如果不加这句话,那么浏览器显示的将是乱码
  40. response.setHeader("content-type", "text/html;charset=UTF-8");
  41. response.setStatus(HttpStatus.NOT_IMPLEMENTED.value());//参数错误
  42. PrintWriter pw = response.getWriter();
  43. pw.write(new String("电话号码格式错误"));
  44. pw.flush();
  45. } else {
  46. //发送短信验证码并且保存到redis中
  47. smsValidateCodeProcessor.create(new ServletWebRequest(request, response));
  48. }
  49. }
  50. }