springboot接入华为云短信
#记一次接入华为云短信
几个月前,也就是公司网站第一个版本上线的时候,当时有业务需要短信验证,于是购买了华为云的短信业务.在此简单的记录一下华为云短信api的接入,比较简单,拿来就可以用这里我首先定义了一个实体类,方便参数注入
public class SmsConfig {//短信发送apiURIprivate String url;//APP_Keyprivate String appKey; //APP_Secretprivate String appSecret ; //国内短信签名通道号或国际/港澳台短信通道号private String sender ; //模板IDprivate String templateId ; //签名名称private String signature;//返回信息回调URI//选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告private String statusCallBack;}
这是首先要定义的两个方法,从华为云短信官方文档中找到,主要作用建立短信发送的请求头和请求体
直接从文档中拿取
private static final String WSSE_HEADER_FORMAT = "UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"";//无需修改,用于格式化鉴权头域,给"Authorization"参数赋值
private static final String AUTH_HEADER_VALUE = "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\"";public String buildRequestBody(String sender,String receiver,String templateId,String templateParas,String statusCallbackUrl,String signature) {if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty()|| templateId.isEmpty()) {System.out.println("buildRequestBody(): sender, receiver or templateId is null.");return null;}List<NameValuePair> keyValues = new ArrayList<NameValuePair>();keyValues.add(new BasicNameValuePair("from", sender));keyValues.add(new BasicNameValuePair("to", receiver));keyValues.add(new BasicNameValuePair("templateId", templateId));if (null != templateParas && !templateParas.isEmpty()) {keyValues.add(new BasicNameValuePair("templateParas", templateParas));}if (null != statusCallbackUrl && !statusCallbackUrl.isEmpty()) {keyValues.add(new BasicNameValuePair("statusCallback", statusCallbackUrl));}if (null != signature && !signature.isEmpty()) {keyValues.add(new BasicNameValuePair("signature", signature));}return URLEncodedUtils.format(keyValues, Charset.forName("UTF-8"));}public String buildWsseHeader(String appKey, String appSecret) {if (null == appKey || null == appSecret || appKey.isEmpty() || appSecret.isEmpty()) {System.out.println("buildWsseHeader(): appKey or appSecret is null.");return null;}SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");String time = sdf.format(new Date()); //CreatedString nonce = UUID.randomUUID().toString().replace("-", ""); //Noncebyte[] passwordDigest = DigestUtils.sha256(nonce + time + appSecret);String hexDigest = Hex.encodeHexString(passwordDigest);String passwordDigestBase64Str = Base64.getEncoder().encodeToString(hexDigest.getBytes()); //PasswordDigestreturn String.format(WSSE_HEADER_FORMAT, appKey, passwordDigestBase64Str, nonce, time);}
最后这个就是发送功能的主要代码
@Autowired
private SmsConfig smsConfig;public void sendMsg(String receiver,String templateParas) {String body = this.buildRequestBody(smsConfig.getSender(), receiver, smsConfig.getTemplateId(), templateParas, smsConfig.getStatusCallBack(), smsConfig.getSignature());if (null == body || body.isEmpty()) {System.out.println("body is null.");return;}String wsseHeader = this.buildWsseHeader(smsConfig.getAppKey(), smsConfig.getAppSecret());if (null == wsseHeader || wsseHeader.isEmpty()) {System.out.println("wsse header is null.");return;}try {CloseableHttpClient client = HttpClients.custom().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null,(x509CertChain, authType) -> true).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();HttpResponse response = client.execute(RequestBuilder.create("POST")//请求方法POST.setUri(smsConfig.getUrl()).addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").addHeader(HttpHeaders.AUTHORIZATION, AUTH_HEADER_VALUE).addHeader("X-WSSE", wsseHeader).setEntity(new StringEntity(body)).build());System.out.println(response.toString()); //打印响应头域信息System.out.println(EntityUtils.toString(response.getEntity()));//打印响应消息实体}catch(Exception e) {log.error("[发送消息接口调用错误");}}
springboot接入华为云短信相关推荐
- Springboot接入华为云短信平台
最近公司的项目需要用到短信验证码,之前没有做过短信的业务,就去华为云官网研究了一下,发现官网给出的教程相当完善,这边记录一下,希望大家能帮到大家... 一.华为云官网申请短信相关信息 1.1申请短信应 ...
- SpringBoot下实现华为云短信验证功能(含代码)
准备工作 登入华为云控制台,找到"消息&短信"模块,找到国内短信(本次开发只针对国内用户),下面分别有 应用管理–签名管理–模板管理 三块 自上而下依次添加应用,首先是 应 ...
- 关于华为云短信接口对接问题
华为云---消息&短信 对接API 问题 对接API 华为云基础示例 https://support.huaweicloud.com/devg-msgsms/sms_04_0002.html ...
- SpringBoot整合阿里云短信服务详细过程(保证初学者也能实现)
前言 网上关于实操性的文章普遍大部分都记录不全,要么只记录重点部分,对于刚学习的小伙伴来说看起来是比较困难的 所以,基于这一点. 该文章会详细介绍使用SpringBoot整合阿里云短信服务的每一步过程 ...
- SpringBoot腾讯云短信实现验证码
SpringBoot腾讯云短信实现验证码 在学习过程中偶然遇见需要实现验证码功能的需求,于是寻思着将功能抽取出来用于分享学习 业务功能:实现验证码60s,且要求防止用户高频刷验证码(即1min一次不多 ...
- SpringBoot集成阿里云短信服务
SpringBoot集成阿里云短信服务 1.准备工作 2.项目集成 2.1 添加依赖 2.2 配置文件 2.3 业务逻辑实现 在实际项目中经常有发送短信的功能,今天进说一下对接阿里云短信服务实现短信发 ...
- springboot集成阿里云短信服务,实现发送短信功能
springboot集成阿里云短信服务,实现发送短信功能 准备工作: 1.登陆阿里云->进入控制台->开通短信服务(进入后根据提示开通) 2.充值(借人家平台发短信你以为不要钱的?我充了3 ...
- Springboot+redis+榛子云短信验证服务进行登录
Springboot+redis+榛子云短信验证服务进行登录 前言 最近在做尚医通的医院预约挂号管理系统时,做到了使用阿里云的短信验证服务,但是现在阿里云的短信验证服务审核听说非常严格,主要是我没有专 ...
- java spring-boot-starter-mail邮件和阿里云华为云短信发送全套
短信API都是需要对接平台的,平台会提供参考代码比较简单,但是邮件是免费的,除了springboot,还能用javaxmail(我记得是,大家可以查查资料) 文章目录 前言 一.spring-boot ...
最新文章
- CentOS7.5常用命令整理
- jeecg3.5.2中上传下载文件的示例中的的一个bug
- python stm32-STM32F4系列使用MicroPython开发
- 对互联网用户分类的思考
- 【HDU 1735】字数统计(贪心,有缺陷的一道题)
- Guava Cache探索及spring项目整合GuavaCache实例
- 58到家为2000万家庭提供上门生活服务的背后是云计算的高效支撑
- python偏函数和高阶函数_【Python入门】8.高阶函数之 匿名函数和偏函数
- 写给新入IT的新人们(转载)
- 会议OA项目之我的审批(查询会议签字)
- 广联达报错access_广联达土建算量软件错误提示大全及解决方法(持续更新至20170418)...
- 自定义 View 之抖音时钟罗盘仪效果
- Python爬取堆糖网的表情包(再也不用担心斗图失败了)
- 我的2011--衣带渐宽终不悔,为伊消得人憔悴
- 淘宝数据库,主键如何设计以及自增ID的问题
- 魔兽对战平台修改服务器数据,《魔兽争霸3》1.29补丁上线官方对战平台:平衡性大做改动...
- 哨兵2号Sentinel-2已经完成大气校正的L2A级遥感影像产品的下载方法
- html在输入框添加叉叉,【部分解决】bootstrap 4中如何给输入框右边加上清除叉叉的按钮...
- windows xp 算号原理
- java视频学习网(java教程视频网)