在springboot开发短信功能前,首先需要开启阿里云短信服务。
1. AccessKey管理登录阿里云,将短信服务添加到控制台,然后点击头像进入AccessKey管理。
点击子用户
创建用户
需要注意要勾选开发调用访问支持
然后创建用户组
添加组成员,选择刚才新建的用户
点击权限管理,新增授权
搜索Sms,点击添加就可以了
再回到用户,点击添加的用户,在最下面AccesKey可以创建AccessKey,要记住Access Id和AccessKey,整合代码时,需要使用。
进入短信服务控制台,点击右侧,国内消息,可以看见签名管理,模板管理等
添加签名和模板不多解释了,需要审核,一定要写仔细了,容易审核不通过。
2. 使用com.aliyun aliyun-java-sdk-core4.5.3
这一段代码就是官方提供的
@SpringBootTest class SpringbootApplicationTests { @Test void contextLoads() throws Exception { DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "Access Id", "Access Key");//这里放之前添加的Access Id,Access Key IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setSysMethod(MethodType.POST); request.setSysDomain("dysmsapi.aliyuncs.com"); request.setSysVersion("2017-05-25"); request.setSysAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); //这往上的不需要修改,下面需要传入参数,手机号,签名名称,模板code,以及验证码。 request.putQueryParameter("PhoneNumbers", "手机号"); request.putQueryParameter("SignName", "短信签名"); request.putQueryParameter("TemplateCode", "模板code"); Mapmap = new HashMap<>(); map.put("code","265987"); request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map)); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }
执行后,手机会收到验证码。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)