短信

先在阿里云注册,得到ID和key
获得代码,将jar报安装
在这里插入图片描述在这里插入图片描述
打开cmd命令,输入

mvn install -Dmaven.test.skip=true -Dgpg.skip=true

引入

<dependency>
	<groundId>org.springframework.boot</groundId>
	<artifactId>spring-boot-starter-amqp<artifactId>
</dependency>
<dependency>
	<groundId>com.aliyun</groundId>
	<artifactId>aliyun-java-sdk-core<artifactId>
</dependency>
<dependency>
	<groundId>com.aliyun</groundId>
	<artifactId>aliyun-java-sdk-dysmsapo<artifactId>
</dependency>
<dependency>
	<groundId>org.springframework.boot</groundId>
	<artifactId>spring-boot-starter-web<artifactId>
</dependency>
<dependency>
	<groundId>org.apache.commons</groundId>
	<artifactId>commons-lang3<artifactId>
</dependency>

配置文件
在这里插入图片描述
引导类

@SpringBootApplication
public class LeyouSmsApplication {
	public static void main(String[] args){
		SpringApplication.run(LeyouSmsApplication.class);
	}
}

读取配置信息

@ConfigurationProperties(prefix="leyou.sms")
public class SmsProperties{
	String accessKeyId;
	String accessKeySecret;
	String signName;
	String verifyCodeTemplate;
	// 写get、set方法
}

工具类

@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsUtils {
	@Autowired
	private SmsProperties prop;
	
	//产品名称:云通信短信API产品,开发者无需替换
	static fina1 String product = "Dysmsapi" ;
	//产品城名,开发者无需替换
	static final String domain = "dysmsapi.aliyuncs.com";
	static final Logger logger = LoggerFactory.getLogger(Smsutils.class);
	
	public SendSmsResponse sendSms (String phone,String code,String signName,String template) throws C1ientException { 
	//可自助调整超时时间
	System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
	System.setProperty("sun.net.client defaultReadTimeout", "10000");
	
	//初始化acsClient,暂不支持region化
	IclientProfile profile = DefaultProfile.getProfile("cn-hangzhou",prop.getAccessKeyIdO,prop. getAccessKeySecret());
	DefaultProfile.addEndpoint ("cn-hangzhou", "cn-hangzhou", product, domain);
	IAcsclient acsClient = new DefaultAcsclient(profile);
	
	//组装请求对象-具体描述见控制台-文档部分内容
	SendsmsRequest request = new SendSmsRequest();
	request.setMethod(MethodType.POST):
	//必填:待发送手机号
	request.setPhoneNumbers(phone);
	//必填:短值签名,可在短信控制台中找到
	request.setSignName(signName);
	//必填:短信模板-可在短信控制台中找到
	request.setTemplateCode(template);
	
	//可选:模板中的变量替换JSON串,如模板内容为"亲爱的$ {name}。您的验证码为${code}"时,此处的值为:
	request.setTemplateParam("{\"code\":\"" + code+ "\"}");
	
	//选填-.上行短信扩展码(无特殊需求用户请忽略此字段)
	//request.seSmsUpExtendcode("90997");
	
	//可选: outId为提供始业务方扩展字段,最终在短信回执消息中将此值带回始调用者
	request.setOutId("123456");
	
	//hint此处可能会抛出异常,注意catch
	SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
	
	logger.info("发送短信状态: {}", sendSmsResponse.getCode());
	logger.info("发送短信消息: {}", sendSmsResponse.getMessage());
	
	return sendSmsResponse;
}

生成短信验证码
在这里插入图片描述
Controller层

@PostMapping("code")
public ResponseEntity<Void> sendVerifyCode(@RequestParam("phone")String phone){
	this.userService.sendVerifyCode(phone);
	return ResponseEntity.status(HttpStatus.CREATED).build();
}
<dependency>
	<groundId>org.springframework.boot</groundId>
	<artifactId>spring-boot-starter-amqp<artifactId>
</dependency>
<dependency>
	<groundId>org.apache.commons</groundId>
	<artifactId>commons-lang3<artifactId>
</dependency>
<dependency>
	<groundId>com.leyou.common</groundId>
	<artifactId>leyou-common<artifactId>
</dependency>
<dependency>
	<groundId>org.slf4j</groundId>
	<artifactId>slf4j-api<artifactId>
</dependency>
<dependency>
	<groundId>org.springframework.boot</groundId>
	<artifactId>spring-boot-starter-web<artifactId>
</dependency>

在这里插入图片描述

@Component
public class SmsListener {
	@Autowired
	private SmsUtils smsUtils;
	@Autowired
	private SmsProperties smsProperties;
	@RabbitListener(bindings=@QueueBinding(
		value = @Queue(value="LEYOU.SMS.QUEUE",durable="true"),
		exchange = @Exchange(value="LEYOU.SMS.EXCHANGE",ignoreDeclarationExceptions="true",type=ExchangeTypes.TOPIC),
		key = {"verifycode_sms"}
	))
	pubic void sendSms (Map<String,String> msg) throws ClientException{
	if(CollectionUtils.isEmpty(msg){
		return;
	}
	String phone = msg.get("phone");
	String code = msg.get("code");
	if (StringUtils.isNoneBlank(phone) && StringUtils.isNoneBlank(code){
		this.smsUtils.sendSms(phone,code,this.smsProperties.getSignName(),this.smsProperties.getVerifyCodeTemplate());
	}
}
@Service
public class UserService{
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AmqpTemplate amqpTemplate;
	@Autowired
	private StringRedisTemplate redisTemplate;
	private static final String KEY_PREFIX="user:verify:";
	public void sendVerifyCode(String phone){
		if(StringUtils.isBlank(phone){
			return;
		}
		// 生成验证码
		String code = NumberUtils.generateCode(6);
		// 发送消息到rabbitmq
		Map<String,String> msg = new HashMap<>();
		msg.put("phone",phone);
		msg.put("code",code);
		this.amqpTemplate.convertAndSend("LEYUO.SMS.EXCHANGE","verifycode.sms",msg);
		// 把验证码保存的Redis中
		this.redisTemplate.opsForValue().set(KEY_PREFIX + phone,code,5,TimeUnit.MINUTES);
	}
}

在这里插入图片描述在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值