学习笔记
BaseController 添加
public static final String CONTENT_TYPE_FORMED = "application/x-www-form-urlencoded";
UserController 添加
需添加 @CrossOrigin 注解,解决跨域问题
@Autowired private HttpServletRequest httpServletRequest; @RequestMapping(value = "/getotp", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED}) @ResponseBody public CommonReturnType getOtp(@RequestParam(name="telphone") String telphone){ // 生成 otp 验证码 Random random = new Random(); int randomInt = random.nextInt(99999); randomInt += 10000; String otpCode = String.valueOf(randomInt); // 将 otp 验证码同对应的用户关联 (暂时使用 httpsession 的方式绑定 otp 与手机号) httpServletRequest.getSession().setAttribute(telphone, otpCode); // 将 otp 验证码通过短信通道发送给用户 (省略,使用控制台输出代替) System.out.println(String.format("telphone = %s & otpCode = %s", telphone, otpCode)); return CommonReturnType.create(null); }
新增 getotp 页面
获取 otp 信息
获取 otp 短信
源码: