javaweb如何使用华为云短信通知公共类调用
作者:L-seeker-Q
这篇文章主要介绍了javaweb使用华为云短信通知公共类调用的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
javaweb华为云短信通知公共类调用
情景:公司业务需求,短信从阿里云切换到华为云,参照华为云短信调用的相关文档遇到不少坑,在此记录一下。
开发环境:JDK1.8 系统环境:SpringBoot
1、华为云短信配置信息在application.yml中配置
sms: huawei: url: https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1 appKey: ****** appSecret: ******
2、创建短信公共类:HwSmsSender
package com.seeker.common.utils.hwsms; import com.alibaba.fastjson.JSONObject; import com.seeker.common.utils.Constants; import com.seeker.common.utils.LoggerUtils; import com.seeker.common.utils.SmsConstants; import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.*; @Component public class HwSmsSender { protected Logger logger = LoggerFactory.getLogger(getClass()); /** * 无需修改,用于格式化鉴权头域,给"X-WSSE"参数赋值 */ 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\""; @Value("${sms.huawei.url}") private String url; @Value("${sms.huawei.appKey}") private String appKey; @Value("${sms.huawei.appSecret}") private String appSecret; public String sendNotifyMsg(String mobile, String templateId, String templateParas) throws Exception { // 默认通知类 return sendMsg(mobile, SmsConstants.ResultMessage.SIGN_NOTIFY_ID, templateId, templateParas); } public String sendMsg(String mobile, String sender, String templateId, String templateParas) throws Exception { //条件必填,国内短信关注,当templateId指定的模板类型为通用模板时生效且必填,必须是已审核通过的,与模板类型一致的签名名称 //国际/港澳台短信不用关注该参数 //签名名称 String signature = "美团外卖"; // String sender = "10690400999304584"; String receiver = "+86" + mobile; //选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告 String statusCallBack = ""; /** * 选填,使用无变量模板时请赋空值 String templateParas = ""; * 单变量模板示例:模板内容为"您的验证码是${1}"时,templateParas可填写为"[\"369751\"]" * 双变量模板示例:模板内容为"您有${1}件快递请到${2}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]" * 模板中的每个变量都必须赋值,且取值不能为空 * 查看更多模板和变量规范:产品介绍>模板和变量规范 */ //模板变量,此处以单变量验证码短信为例,请客户自行生成6位验证码,并定义为字符串类型,以杜绝首位0丢失的问题(例如:002569变成了2569)。 //templateParas = "[\"369751\"]"; //请求Body,不携带签名名称时,signature请填null String body = buildRequestBody(sender, receiver, templateId, templateParas, statusCallBack, signature); if (null == body || body.isEmpty()) { LoggerUtils.info(logger, "body is null."); return "1"; } //请求Headers中的X-WSSE参数值 String wsseHeader = buildWsseHeader(appKey, appSecret); if (null == wsseHeader || wsseHeader.isEmpty()) { LoggerUtils.info(logger, "wsse header is null."); return "1"; } //如果JDK版本低于1.8,可使用如下代码 //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 //CloseableHttpClient client = HttpClients.custom() // .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { // @Override // public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { // return true; // } // }).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build(); //如果JDK版本是1.8,可使用如下代码 //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 CloseableHttpClient client = HttpClients.custom() .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, (x509CertChain, authType) -> true).build()) .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .build(); //请求方法POST HttpResponse response = client.execute(RequestBuilder.create("POST") .setUri(url) .addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded") .addHeader(HttpHeaders.AUTHORIZATION, AUTH_HEADER_VALUE) .addHeader("X-WSSE", wsseHeader) .setEntity(new StringEntity(body)).build()); //打印响应头域信息 LoggerUtils.info(logger, (response.toString())); //打印响应消息实体 String entity = EntityUtils.toString(response.getEntity()); LoggerUtils.info(logger, "消息实体" + entity); HwSmsRoot hwSmsRoot = JSONObject.parseObject(entity, HwSmsRoot.class); return "0"; } /** * 构造请求Body体 * * @param sender * @param receiver * @param templateId * @param templateParas * @param statusCallbackUrl * @param signature | 签名名称,使用国内短信通用模板时填写 * @return */ static 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")); } /** * 构造X-WSSE参数值 * * @param appKey * @param appSecret * @return */ static 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'"); //Created String time = sdf.format(new Date()); //Nonce String nonce = UUID.randomUUID().toString().replace("-", ""); byte[] passwordDigest = DigestUtils.sha256(nonce + time + appSecret); String hexDigest = Hex.encodeHexString(passwordDigest); //如果JDK版本是1.8,请加载原生Base64类,并使用如下代码 //PasswordDigest String passwordDigestBase64Str = Base64.getEncoder().encodeToString(hexDigest.getBytes()); //如果JDK版本低于1.8,请加载三方库提供Base64类,并使用如下代码 //PasswordDigest //String passwordDigestBase64Str = Base64.encodeBase64String(hexDigest.getBytes(Charset.forName("utf-8"))); //若passwordDigestBase64Str中包含换行符,请执行如下代码进行修正 //passwordDigestBase64Str = passwordDigestBase64Str.replaceAll("[\\s*\t\n\r]", ""); return String.format(WSSE_HEADER_FORMAT, appKey, passwordDigestBase64Str, nonce, time); } //本地调试方法 public static void main(String[] args) throws Exception { //必填,请参考"开发准备"获取如下数据,替换为实际值 //APP接入地址+接口访问URI String url = "https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1"; //APP_Key 输入自己的 String appKey = "******"; //APP_Secret 输入自己的 String appSecret = "******"; //国内短信签名通道号或国际/港澳台短信通道号 String sender = "8820032023657"; //模板ID String templateId = "d1e8f2c7ab964c6998bda5638238bb7d"; //条件必填,国内短信关注,当templateId指定的模板类型为通用模板时生效且必填,必须是已审核通过的,与模板类型一致的签名名称 //国际/港澳台短信不用关注该参数 //签名名称 String signature = "美团外卖"; //必填,全局号码格式(包含国家码),示例:+8615123**6789,多个号码之间用英文逗号分隔 //String receiver = "+8615123**6789,+8615234**7890"; //短信接收人号码 String receiver = "+8617520**2687"; //选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告 String statusCallBack = ""; /** * 选填,使用无变量模板时请赋空值 String templateParas = ""; * 单变量模板示例:模板内容为"您的验证码是${1}"时,templateParas可填写为"[\"369751\"]" * 双变量模板示例:模板内容为"您有${1}件快递请到${2}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]" * 模板中的每个变量都必须赋值,且取值不能为空 * 查看更多模板和变量规范:产品介绍>模板和变量规范 */ //模板变量,此处以单变量验证码短信为例,请客户自行生成6位验证码,并定义为字符串类型,以杜绝首位0丢失的问题(例如:002569变成了2569)。 String templateParas = JSONObject.toJSONString(new String[]{"598745", "1"}); //请求Body,不携带签名名称时,signature请填null String body = buildRequestBody(sender, receiver, templateId, templateParas, statusCallBack, signature); if (null == body || body.isEmpty()) { System.out.println("body is null."); return; } //请求Headers中的X-WSSE参数值 String wsseHeader = buildWsseHeader(appKey, appSecret); if (null == wsseHeader || wsseHeader.isEmpty()) { System.out.println("wsse header is null."); return; } //如果JDK版本低于1.8,可使用如下代码 //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 //CloseableHttpClient client = HttpClients.custom() // .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { // @Override // public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { // return true; // } // }).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build(); //如果JDK版本是1.8,可使用如下代码 //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 CloseableHttpClient client = HttpClients.custom() .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, (x509CertChain, authType) -> true).build()) .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .build(); //请求方法POST HttpResponse response = client.execute(RequestBuilder.create("POST") .setUri(url) .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()); //打印响应消息实体 String entity = EntityUtils.toString(response.getEntity()); System.out.println(entity); HwSmsRoot hwSmsRoot = JSONObject.parseObject(entity, HwSmsRoot.class); System.out.println(hwSmsRoot.getCode()); } }
3、上面公共类还用到以下类:
HwSmsResult
import lombok.Data; @Data public class HwSmsResult { private String originTo; private String createTime; private String from; private String smsMsgId; private String status; }
HwSmsRoot
@Data public class HwSmsRoot { private List<HwSmsResult> result; private String code; private String description; }
SmsConstants
public class SmsConstants { public static class ResultMessage { /** * 通道 美团外卖 验证码类 */ public static String SIGN_NOTIFY_ID = "8821041535507"; public static String SMS_SEND_ID = "8fed7b85f85d4a248fe983a321"; } }
LoggerUtils
import org.slf4j.Logger; public class LoggerUtils { public static void debug(Logger logger, Object message) { if (logger.isDebugEnabled()) { logger.debug("Debug日志信息{}", message); } } public static void info(Logger logger, Object message) { if (logger.isInfoEnabled()) { logger.info("Info日志信息{}", message); } } public static void error(Logger logger, Object message) { if (logger.isErrorEnabled()) { logger.error("Error日志信息{}", message); } } }
4、实战调用如下:
/** * 获取验证码接口 */ @PostMapping("/sendMessageWxUser") public AjaxResult sendMessageWxUser(@RequestBody WxUser user, HttpServletRequest request, HttpServletResponse response) throws Exception { String ipAddr = IPUtils.getIpAddr(request); AjaxResult ajaxResult = new AjaxResult(0, "短信发送成功!请等待查收!"); if (StringUtils.isEmpty(user.getLoginName())) { return AjaxResult.error(500, "手机号不能为空"); } //生成6位随机数,有效期1分钟 String randNum = VerificatieCodeUtils.getRandNum(6); String[] templateParas = new String[]{randNum, "1"}; String templateId = SmsConstants.ResultMessage.SMS_SEND_ID; String code = smsSendUtils.sendRegisterSMS(user.getLoginName(), templateId, JSONObject.toJSONString(templateParas)); logger.info("--打印验证码:" + randNum); if ("0".equals(code)) { //缓存按ip+varcode 作为key保存 redisCache.setCacheObject(ipAddr + "varcode", randNum, 60, TimeUnit.SECONDS); return ajaxResult; } else { return new AjaxResult(500, "短信发送失败!"); } }
VerificatieCodeUtils :验证码随机数生成
import java.util.Random; public class VerificatieCodeUtils { public static String getRandNum(int charCount) { String charValue = ""; for (int i = 0; i < charCount; i++) { char c = (char) (randomInt(0, 10) + '0'); charValue += String.valueOf(c); } return charValue; } public static int randomInt(int from, int to) { Random r = new Random(); return from + r.nextInt(to - from); } }
最后:需要注意的是,根据自己项目开发环境版本调整一些相关方法,以及华为云一些密钥什么需要填写正确。
java调用华为云上的文字识别(OCR)接口
首先要满足的条件,已注册华为云账户,并订阅华为云上的文字识别接口,以下以订阅的身份证识别接口为例
package OCRDemo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; /** * @author abang * @date 2020/7/3 0003 20:59 */ public class GetOCR { public static void main(String[] args) throws IOException { //请求的region String region = "cn-north-4"; //用户名 String username = "xxxxxx"; //用户密码 String password = "xxxxxx"; //账户名 String userName = "xxxxxx"; //请求的uri接口 String uri = "/v1.0/ocr/id-card"; //传入image或uri的json字符串(图片为公网的地址,可查询) String param = "{\"url\":\"http://photocdn.sohu.com/20101021/Img276166786.jpg\"}"; //获取用token String token = getToken(region,username,password,userName); //返回请求的图片文字信息 System.out.println(getIdCard(region,token,param,uri)); } //获取请求链接中用户的token信息 public static String getToken(String region,String username,String password,String userName) throws IOException { String iam = "https://iam."+region+".myhuaweicloud.com/v3/auth/tokens"; String param = "{\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"name\":\"" + username + "\",\"password\":\"" + password + "\",\"domain\":{\"name\":\"" + userName + "\"}}}},\"scope\":{\"project\":{\"name\":\"cn-north-4\"}}}}"; PrintWriter out; BufferedReader in = null; String token = ""; String response = ""; try { //需要请求的url URL url = new URL(iam); //打开和URL之间的连接 URLConnection connection = url.openConnection(); //设置通用的请求属性,请求头部分 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0"); // 发送POST请求必须设置如下两行 connection.setDoInput(true); connection.setDoOutput(true); // 建立实际的连接 connection.connect(); ///获取URLConnection对象对应的输出流 out = new PrintWriter(connection.getOutputStream()); //发送请求参数 out.write(param); //flush输出流的缓冲 out.flush(); //获取相应头中的token信息 token = connection.getHeaderField("X-Subject-Token"); //定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { //换行打印获取结果 response += "\n" + line; } // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { //打印出相应头中的信息 //System.out.println(key + "--->" + map.get(key)); } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } //返回所要用到的token信息 return token; } //请求云上接口,调用接口服务 public static String getIdCard(String region,String token,String param,String uri) throws IOException { String ocr = "https://ocr."+region+".myhuaweicloud.com"+uri; String response = ""; BufferedReader in = null; try { URL url = new URL(ocr); URLConnection connection = url.openConnection(); //容易载跟头,表明请求体的部分为json形式 connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("X-Auth-Token", token); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); PrintWriter out = new PrintWriter(connection.getOutputStream()); out.write(param); out.flush(); in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { response += "\n" + line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } //返回相应体中的结果,打印出来 return response; } }
返回成功
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。