A simple SMS registration Demo

**

Users log in to any software need to register an account, input the phone number to send text messages, the phone has to obtain permission to get the phone number for registration, as well as the landing and then scan code associated with the phone number, but also associated with a third party such as QQ, Sina microblogging case, pay Bora login, either way is to follow your cellphone up to avoid a phone number multiple accounts

**

Today, casually write some simple registration login code,

Registration, then phone up no more than two buttons, one to send text messages, a registered

Registration process:
the user clicks to send text messages, receive backstage passes over the front desk phone number, and then send a text message to the phone number, and now a third party to send text messages to cell phone numbers a lot, such as greater than Ali, IFLYTEK, just find the first tripartite can, are the charges, but the test will be free of a few, to be greater than today Na Ali examples

Ali register an account, login application keys, then download the SDK under modification, probably the code below

First add dependencies, packages can also be downloaded directly jar, companies are basically using maven, you can rely on the introduction of direct

<dependency>
		<groupId>com.aliyun</groupId>
		<artifactId>aliyun-java-sdk-core</artifactId>
		<version>4.1.0</version>
</dependency>

Then write method to send text messages

/**
	 * 发送短信
	 * 
	 * @param phone
	 *            手机号
	 */
	public static String send(String phone, String rodom) {
		// 第二个参数为自己独有的accessKeyid,第三个参数为自己独有的accessKeySecret
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AAAAA", "BBBBBB");
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();// 组装请求对象
		// request.setProtocol(ProtocolType.HTTPS);
		request.setMethod(MethodType.POST);// 设置post提交
		request.setDomain("dysmsapi.aliyuncs.com");// 短信API产品域名(接口地址固定,无需修改)
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", "就是短信方括号里面的那个名字");// 短信签名
		request.putQueryParameter("TemplateCode", "SMS_162731312");
		request.putQueryParameter("TemplateParam", "{code:" + rodom + "}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			return response.getData();
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return null;
	}

Then write a random number generation method 6

// 生成6位随机数
	public static String createRodom() {
		Random random = new Random();
		String result = "";
		for (int i = 0; i < 6; i++) {
			result += random.nextInt(10);
		}
		return result;
	}

Both interfaces should be enough background

/**
	 * 发送短信接口
	 * 
	 * @param request
	 * @param number
	 * @return
	 */
	@RequestMapping("/sendSms")
	@ResponseBody
	public ReturnT<String> sendSms(HttpServletRequest request, String number) {
		try {
			// 生成6位验证码
			String rodom = Sms.createRodom();
			// 发送短信
			// TODO
			// Sms.send(number, rodom);
			// 将认证码存入SESSION
			request.getSession().setAttribute("verifyCode", rodom);
			request.getSession().setAttribute("createTime", System.currentTimeMillis());
			return ReturnT.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
/**
	 * 验证验证码是否正确
	 * 
	 * @param request
	 * @param verifyCode
	 * @return
	 */
	@RequestMapping("/cheakSms")
	@ResponseBody
	public ReturnT<String> cheakSms(HttpServletRequest request, String phone, String password, String verifyCode) {
		String code = (String) request.getSession().getAttribute("verifyCode");
		long time = (long) request.getSession().getAttribute("createTime");
		// 验证码不为空时,到后台进行比较,返回响应码,
		// 为0,提示成功
		// 为1,提示请先获得验证码
		// 为2,提示验证码错误
		// 为3,验证码超时
		// 为4,该手机号已注册
		if (verifyCode == null) {
			return new ReturnT<String>(1, "请先获得验证码");
		} else if (verifyCode.equals(code)) {
			SysUser user = new SysUser();
			user.setLoginName(phone);
			ReturnT<SysUser> returnT = sysUserService.selectSysUser(user);
			if (returnT == null && "".equals(returnT)) {
				if (System.currentTimeMillis() - time > 600000) {
					return new ReturnT<String>(3, "验证码超时");
				} else {
					// 注册成功,存储用户中心,salt默认为123456
					String hex = DigestUtils.md5Hex(phone + password + "123456");
					SysUser sysUser = new SysUser();
					sysUser.setLoginName(phone);
					sysUser.setPassword(hex);
					sysUser.setSalt("123456");
					sysUserService.save(sysUser);
					return new ReturnT<String>(0, "注册成功");
				}
			} else {
				return new ReturnT<String>(2, "验证码错误");
			}

		} else {
			return new ReturnT<String>(4, "该手机号已注册");
		}

	}

Registration put a short message interface, a front desk check SMS verification code entered, nothing more than a few cases, one by one under judgment like, or verification code is empty or not correct, either registered or has timed out, the code so much

Then start testing it, because the text messages sent over to look at the manual verification code

Published 12 original articles · won praise 1 · views 8139

Guess you like

Origin blog.csdn.net/Jaeger_Java/article/details/104755391
Recommended