(java)调用阿里大于接口做手机验证

调用阿里大于做手机验证
运用技术 : 阿里大于,java, js ,jsp

第一步: https://dysms.console.aliyun.com/dysms.htm 申请模板和签名 不要有敏感词汇 一般都会通过。
在这里插入图片描述

创建应用,并记录下应用的KeyId 及 KeySecret 此处很重要。
在这里插入图片描述
第二步: 准备完毕,直接上代码


import java.util.Random;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
@Controller
@RequestMapping(value="/shortMessage")
public class CodeController {

	
	@RequestMapping(value="/shortmessage")
	@ResponseBody
	public static String shortmessage(String phone){
		
		DefaultProfile profile = DefaultProfile.getProfile("default", "此处填写key", "此处填写 Secret");       ***** 此处需修改
        IAcsClient client = new DefaultAcsClient(profile);
        
        //组装请求对象
        CommonRequest request = new CommonRequest();
        //使用post提交
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", phone);         *****   phone: 此处为获取动态手机号 
        request.putQueryParameter("SignName", "固件系统");
        request.putQueryParameter("TemplateCode", "SMS_1739");       *****   此处为模版CODE
        
        String msgCode = getMsgCode();
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + msgCode + "\"}");   
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
		return "ok";
    }
	
	/**
     * 生成随机的6位数,短信验证码
     * @return
     */
    private static String getMsgCode() {
        int n = 6;
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        return code.toString();

	}
}

第三步 : 简单jsp页面
在这里插入图片描述一个点击事件 样式可忽略

第四步 : js
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值