{"id":19329130,"url":"https://github.com/hello-java-maker/sms-verification","last_synced_at":"2025-04-22T21:31:27.380Z","repository":{"id":109696571,"uuid":"138142913","full_name":"hello-java-maker/SMS-verification","owner":"hello-java-maker","description":"手机短信验证，功能包括：短语验证、语音通话验证等功能","archived":false,"fork":false,"pushed_at":"2018-06-21T08:43:54.000Z","size":6840,"stargazers_count":29,"open_issues_count":0,"forks_count":27,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T04:23:02.226Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hello-java-maker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-21T08:35:51.000Z","updated_at":"2025-03-12T07:53:40.000Z","dependencies_parsed_at":"2023-04-20T20:08:36.538Z","dependency_job_id":null,"html_url":"https://github.com/hello-java-maker/SMS-verification","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hello-java-maker%2FSMS-verification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hello-java-maker%2FSMS-verification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hello-java-maker%2FSMS-verification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hello-java-maker%2FSMS-verification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hello-java-maker","download_url":"https://codeload.github.com/hello-java-maker/SMS-verification/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250328384,"owners_count":21412613,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T02:26:47.189Z","updated_at":"2025-04-22T21:31:27.374Z","avatar_url":"https://github.com/hello-java-maker.png","language":"Java","readme":"# SMS-verification\n\n手机短信验证现在在各种系统可以说都是用的非常普遍的，这个可能是方便和安全性的考虑，所以才广泛的使用，这篇文章就以一个短信接口的实例，来讲解一下怎么使用短信接口。\n\n### 一、前期工作\n首先，我们需要选定一家短信接口的公司，然后去注册和获取一系列的ID等，然后就可以正式的创建我们的短信业务了。下面以某个短信接口为例讲解。\n\n## 1.1、注册\nhttp://www.miaodiyun.com/index.html（对于用哪个平台的看个人，这个只是实例）\n\n## 1.2、获取到ACCOUNT SID和AUTH TOKEN\n![这里写图片描述](https://img-blog.csdn.net/20180611171754422?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpaGFpMTIzNDU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)\n\n## 1.3、创建短信模板\n![这里写图片描述](https://img-blog.csdn.net/20180611171933379?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpaGFpMTIzNDU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)\n\n如上图，点击**配置管理**，然后进入到**短信模板**，再点击**新建模板**，创建好你的**短信模板**。\n\n下面给出我的模板作为参考。\n![这里写图片描述](https://img-blog.csdn.net/20180611172119468?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpaGFpMTIzNDU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)\n\n**注意：**上面创建的**短信模板**的信息，需要在代码中用到，并且一定需要保持一致，否则，会出现异常。\n\n例如，上面的**短信模板**的信息应为：“【欧阳科技】登录验证码：{1}，如非本人操作，请忽略此短信。”，`{1}`为占位符，是你的短信验证码。\n\n好了，有了这些准备之后，就可以开始发短信了。\n\n### 二、具体代码\n**config.java:**\n这个类主要是一些常亮参数的配置信息。\n\n这里我们需要修改我们注册时获取到的`ACCOUNT SID`和`AUTH TOKEN`。\n```\n\n/**\n * 系统常量\n */\npublic class Config\n{\n\t/**\n\t * url前半部分\n\t */\n\tpublic static final String BASE_URL = \"https://api.miaodiyun.com/20150822\";\n\n\t/**\n\t * 开发者注册后系统自动生成的账号，可在官网登录后查看\n\t */\n\tpublic static final String ACCOUNT_SID = \"aac6e373c7534007bf47648ba34ba2f1\";\n\n\t/**\n\t * 开发者注册后系统自动生成的TOKEN，可在官网登录后查看\n\t */\n\tpublic static final String AUTH_TOKEN = \"47605360a97a4f81bcd576e8e0645edf\";\n\n\t/**\n\t * 响应数据类型, JSON或XML\n\t */\n\tpublic static final String RESP_DATA_TYPE = \"json\";\n}\n```\n\n**HttpUtil.java（http请求工具）：**\n\n```\n\nimport java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.io.OutputStreamWriter;\nimport java.net.URL;\nimport java.net.URLConnection;\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\n\nimport org.apache.commons.codec.digest.DigestUtils;\n\n/**\n * http请求工具\n */\npublic class HttpUtil\n{\n\t/**\n\t * 构造通用参数timestamp、sig和respDataType\n\t * \n\t * @return\n\t */\n\tpublic static String createCommonParam()\n\t{\n\t\t// 时间戳\n\t\tSimpleDateFormat sdf = new SimpleDateFormat(\"yyyyMMddHHmmss\");\n\t\tString timestamp = sdf.format(new Date());\n\n\t\t// 签名\n\t\tString sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp);\n\n\t\treturn \"\u0026timestamp=\" + timestamp + \"\u0026sig=\" + sig + \"\u0026respDataType=\" + Config.RESP_DATA_TYPE;\n\t}\n\n\t/**\n\t * post请求\n\t * \n\t * @param url\n\t *            功能和操作\n\t * @param body\n\t *            要post的数据\n\t * @return\n\t * @throws IOException\n\t */\n\tpublic static String post(String url, String body)\n\t{\n\t\tSystem.out.println(\"url:\" + System.lineSeparator() + url);\n\t\tSystem.out.println(\"body:\" + System.lineSeparator() + body);\n\n\t\tString result = \"\";\n\t\ttry\n\t\t{\n\t\t\tOutputStreamWriter out = null;\n\t\t\tBufferedReader in = null;\n\t\t\tURL realUrl = new URL(url);\n\t\t\tURLConnection conn = realUrl.openConnection();\n\n\t\t\t// 设置连接参数\n\t\t\tconn.setDoOutput(true);\n\t\t\tconn.setDoInput(true);\n\t\t\tconn.setConnectTimeout(5000);\n\t\t\tconn.setReadTimeout(20000);\n\t\t\tconn.setRequestProperty(\"Content-Type\", \"application/x-www-form-urlencoded\");\n\t\t\t// 提交数据\n\t\t\tout = new OutputStreamWriter(conn.getOutputStream(), \"UTF-8\");\n\t\t\tout.write(body);\n\t\t\tout.flush();\n\n\t\t\t// 读取返回数据\n\t\t\tin = new BufferedReader(new InputStreamReader(conn.getInputStream(), \"UTF-8\"));\n\t\t\tString line = \"\";\n\t\t\tboolean firstLine = true; // 读第一行不加换行符\n\t\t\twhile ((line = in.readLine()) != null)\n\t\t\t{\n\t\t\t\tif (firstLine)\n\t\t\t\t{\n\t\t\t\t\tfirstLine = false;\n\t\t\t\t} else\n\t\t\t\t{\n\t\t\t\t\tresult += System.lineSeparator();\n\t\t\t\t}\n\t\t\t\tresult += line;\n\t\t\t}\n\t\t} catch (Exception e)\n\t\t{\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * 回调测试工具方法\n\t * \n\t * @param url\n\t * @param reqStr\n\t * @return\n\t */\n\tpublic static String postHuiDiao(String url, String body)\n\t{\n\t\tString result = \"\";\n\t\ttry\n\t\t{\n\t\t\tOutputStreamWriter out = null;\n\t\t\tBufferedReader in = null;\n\t\t\tURL realUrl = new URL(url);\n\t\t\tURLConnection conn = realUrl.openConnection();\n\n\t\t\t// 设置连接参数\n\t\t\tconn.setDoOutput(true);\n\t\t\tconn.setDoInput(true);\n\t\t\tconn.setConnectTimeout(5000);\n\t\t\tconn.setReadTimeout(20000);\n\n\t\t\t// 提交数据\n\t\t\tout = new OutputStreamWriter(conn.getOutputStream(), \"UTF-8\");\n\t\t\tout.write(body);\n\t\t\tout.flush();\n\n\t\t\t// 读取返回数据\n\t\t\tin = new BufferedReader(new InputStreamReader(conn.getInputStream(), \"UTF-8\"));\n\t\t\tString line = \"\";\n\t\t\tboolean firstLine = true; // 读第一行不加换行符\n\t\t\twhile ((line = in.readLine()) != null)\n\t\t\t{\n\t\t\t\tif (firstLine)\n\t\t\t\t{\n\t\t\t\t\tfirstLine = false;\n\t\t\t\t} else\n\t\t\t\t{\n\t\t\t\t\tresult += System.lineSeparator();\n\t\t\t\t}\n\t\t\t\tresult += line;\n\t\t\t}\n\t\t} catch (Exception e)\n\t\t{\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn result;\n\t}\n}\n```\n\n**验证码通知短信接口：（最重要）**\n这里需要修改我们在注册时获取到的信息。\n\n- 修改smsContent \n把这个短信的内容修改为你创建的**短信模板**\n**注意：**一定要保持一致。 \n```\nimport java.net.URLEncoder;\n\nimport com.miaodiyun.httpApiDemo.common.Config;\nimport com.miaodiyun.httpApiDemo.common.HttpUtil;\n\n/**\n * 验证码通知短信接口\n * \n * @ClassName: IndustrySMS\n * @Description: 验证码通知短信接口\n *\n */\npublic class IndustrySMS\n{\n\tprivate static String operation = \"/industrySMS/sendSMS\";\n\n\tprivate static String accountSid = Config.ACCOUNT_SID;\n\tprivate static String to = \"13767441759\";\n\t\n\tprivate static String code = smsCode();\n//\t登录验证码：{1}，如非本人操作，请忽略此短信。\n\tprivate static String smsContent = \"【欧阳科技】登录验证码：\"+code+\"，如非本人操作，请忽略此短信。\";\n\n\t/**\n\t * 验证码通知短信\n\t */\n\tpublic static void execute()\n\t{\n\t\tString tmpSmsContent = null;\n\t    try{\n\t      tmpSmsContent = URLEncoder.encode(smsContent, \"UTF-8\");\n\t    }catch(Exception e){\n\t      \n\t    }\n\t    String url = Config.BASE_URL + operation;\n\t    String body = \"accountSid=\" + accountSid + \"\u0026to=\" + to + \"\u0026smsContent=\" + tmpSmsContent\n\t        + HttpUtil.createCommonParam();\n\n\t    // 提交请求\n\t    String result = HttpUtil.post(url, body);\n\t    System.out.println(\"result:\" + System.lineSeparator() + result);\n\t}\n\t\n\t//创建验证码\n\tpublic static String smsCode(){\n\t\tString random=(int)((Math.random()*9+1)*100000)+\"\";\t\n\t\tSystem.out.println(\"验证码：\"+random);\n\t\treturn random;\n\t}\n}\n\n```\n\n上面这些是主要的类，还有其他的类在文章末尾给出源代码。\n\n### 三、手机短信验证测试\n\n```\n\npublic class Test\n{\n\n\t/**\n\t * @param args\n\t */\n\tpublic static void main(String[] args)\n\t{\n\n\t\t// 验证码通知短信接口\n\t\t IndustrySMS.execute();\n\n\t}\n\t\n\t\n}\n\n```\n\n#####源代码下载\nhttps://download.csdn.net/download/sihai12345/10472391\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhello-java-maker%2Fsms-verification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhello-java-maker%2Fsms-verification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhello-java-maker%2Fsms-verification/lists"}