In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge of this article "how to send SMS with SpringBoot", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "how to send SMS with SpringBoot" article.
Application.properties:
Server.port=8002#server.servlet.context-path=/spring.datasource.url=jdbc:mysql://localhost:3306/ssm_message?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghaispring.datasource.username=rootspring.datasource.password=19961117Lhhspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# open hump named mybatis.configuration.map-underscore-to-camel-case=true# setting timeout-you can adjust the sms.default.connect.timeout=sun.net.client.defaultConnectTimeoutsms.default.read.timeout=sun.net.client. Several parameters required by defaultReadTimeoutsms.timeout=10000# to initialize ascClient # SMS API product name (SMS product name is fixed No need to modify) sms.product=Dysmsapi# SMS API product domain name (API address is fixed, no need to modify) sms.domain=dysmsapi.aliyuncs.com# is replaced with your AK (product secret) # your accessKeyId, fill in your own above configuration sms.access.key.id=xxxx# your accessKeyId, fill in your own above configuration self-configuration sms.access.key.secret=xxxx# Ali Cloud configure your own SMS template fill in sms.template.code=SMS_238470888
MessageController
Package com.example.demo.controller;import com.alibaba.fastjson.JSON;import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;import com.example.demo.service.MessageService;import com.example.demo.utils.MessageUtils;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController Import java.util.HashMap;import java.util.Map;@Api (description = "SMS interface") @ RequestMapping ("/ smsLogin") @ RestControllerpublic class MessageController {@ Autowired public MessageService messageService; @ Autowired public MessageUtils messageUtils; @ ApiOperation (value = "API for obtaining SMS verification code", notes = "API for obtaining SMS verification code") @ GetMapping ("/ sendMessage") public Map getSMSMessage (String phone) {Map map = new HashMap () If (phone = = null | | phone = = "") {map.put ("code", "FAIL"); map.put ("msg", "mobile number is empty"); return map;} Map smsMap = messageUtils.getPhoneMsg (phone); if ("OK" .equals (smsMap.get ("status") {Map data = messageService.selectSMSDataByPhone (phone) Map.put ("phone", phone); map.put ("smsCode", smsMap.get ("msg")); / / you can also consider using redis to store the CAPTCHA in the database. Here we use the database as an example, if (data! = null) {messageService.updateSMSDataByPhone (map). } else {messageService.insert (map);} smsMap.put ("msg", "success");} return smsMap } @ ApiOperation (value = "SMS verification login interface", notes = "SMS verification login interface") @ GetMapping ("/ login") public Map login (String phone, String smsCode) {Map map = new HashMap (); if (StringUtils.isEmpty (phone) | | StringUtils.isEmpty (smsCode)) {map.put ("code", "FAIL") Map.put ("msg", "Please check data"); return map;} / / take out the corresponding CAPTCHA and compare it with Map smsMap = messageService.selectSMSDataByPhone (phone); if (smsMap = = null) {map.put ("code", "FAIL") Map.put ("msg", "this mobile number does not send CAPTCHA"); return map;} String code = (String) smsMap.get ("sms_code"); if (! smsCode.equals (code)) {map.put ("code", "FAIL"); map.put ("msg", "CAPTCHA incorrect") Return map;} map.put ("code", "OK"); map.put ("msg", "success"); return map;}}
MessageUtils
Package com.example.demo.utils;import com.aliyuncs.DefaultAcsClient;import com.aliyuncs.IAcsClient;import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;import com.aliyuncs.exceptions.ClientException;import com.aliyuncs.http.MethodType;import com.aliyuncs.profile.DefaultProfile;import com.aliyuncs.profile.IClientProfile;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component Import org.springframework.web.client.RestTemplate;import java.util.HashMap;import java.util.Map;@Componentpublic class MessageUtils {@ Autowired RestTemplate restTemplate; @ Value ("${sms.default.connect.timeout}") private String DEFAULT_CONNECT_TIMEOUT; @ Value ("${sms.default.read.timeout}") private String DEFAULT_READ_TIMEOUT; @ Value ("${sms.timeout}") private String SMS_TIMEOUT @ Value ("${sms.product}") private String SMS_PRODUCT; @ Value ("${sms.domain}") private String SMS_DOMAIN; @ Value ("${sms.access.key.id}") private String SMS_ACCESSKEYID; @ Value ("${sms.access.key.secret}") private String SMS_ACCESSKEYSECRET; @ Value ("${sms.template.code}") private String TEMPLATE_CODE; private static String code / / code corresponds to the parameter public Map getPhoneMsg (String phone) {if (phone = = null | | phone = = "") {System.out.println ("phone number is empty") in your SMS destination; return null;} / / set the timeout period-you can adjust System.setProperty (DEFAULT_CONNECT_TIMEOUT, SMS_TIMEOUT) System.setProperty (DEFAULT_READ_TIMEOUT, SMS_TIMEOUT); / / several parameters needed to initialize ascClient final String product = SMS_PRODUCT; final String domain = SMS_DOMAIN; / / replace with your AK final String accessKeyId = SMS_ACCESSKEYID; final String accessKeySecret = SMS_ACCESSKEYSECRET / / initialize ascClient, currently does not support multiple region IClientProfile profile = DefaultProfile.getProfile ("cn-hangzhou", accessKeyId, accessKeySecret); Map map = new HashMap (); try {DefaultProfile.addEndpoint ("cn-hangzhou", "cn-hangzhou", product, domain); / / get the verification code code = vcode () IAcsClient acsClient = new DefaultAcsClient (profile); / / Assembly request object SendSmsRequest request = new SendSmsRequest (); / / submit request.setMethod (MethodType.POST) using post; / / required: mobile phone number to be sent. Batch calls are supported in comma-separated form, with a maximum of 1000 mobile phone numbers. Batch calls are slightly delayed relative to the timeliness of a single call. Request.setPhoneNumbers (phone) is recommended for SMS messages with CAPTCHA type; / / required: SMS signature-request.setSignName can be found in the SMS console ("java Learning"). / / required: SMS template-you can find request.setTemplateCode (TEMPLATE_CODE) in the SMS console. / / optional: replace the JSON string with variables in the template. For example, when the template content is "Dear ${name}, your CAPTCHA is $[code]", the value here is / / friendly hint: if a newline character is needed in JSON, please refer to the newline character requirements of the standard JSON protocol. For example, if the text message content contains\ r\ n, it needs to be represented as\\ r\\ n in JSON. Otherwise, JSON will fail to resolve request.setTemplateParam ("{\" code\ ":\"+ code +"\ "}") on the server side. / / optional-Uplink SMS extension code (ignore this field for users with no special needs) / / request.setSmsUpExtendCode ("90997"); / / optional: outId is an extended field provided to the business side, and this value is finally brought back to the caller request.setOutId ("yourOutId") in the SMS receipt message. / / if the request fails, a ClientException exception will be thrown SendSmsResponse sendSmsResponse = acsClient.getAcsResponse (request); map.put ("status", sendSmsResponse.getCode ()) If (sendSmsResponse.getCode ()! = null & & sendSmsResponse.getCode () .equals ("OK")) {/ / request succeeded map.put ("msg", code) } else {/ / if there is an error in the CAPTCHA code, it will output an error code telling you the specific reason map.put ("msg", sendSmsResponse.getMessage ());}} catch (Exception e) {e.printStackTrace (); map.put ("status", "FAIL") Map.put ("msg", "failed to get SMS verification code");} return map;} / * * generate 6-digit random number verification code * * @ return * / public static String vcode () {String vcode = ""; for (int I = 0; I < 6) ITunes +) {vcode = vcode + (int) (Math.random () * 9);} return vcode;}} the above is about "how to send SMS messages by SpringBoot". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.