2024年Java实现短信验证码,最新Java开发进阶

最后

我想问下大家当初选择做程序员的初衷是什么?有思考过这个问题吗?高薪?热爱?

既然入了这行就应该知道,这个行业是靠本事吃饭的,你想要拿高薪没有问题,请好好磨练自己的技术,不要抱怨。有的人通过培训可以让自己成长,有些人可以通过自律强大的自学能力成长,如果你两者都不占,还怎么拿高薪?

架构师是很多程序员的职业目标,一个好的架构师是不愁所谓的35岁高龄门槛的,到了那个时候,照样大把的企业挖他。为什么很多人想进阿里巴巴,无非不是福利待遇好以及优质的人脉资源,这对个人职业发展是有非常大帮助的。

如果你也想成为一名好的架构师,那或许这份Java核心架构笔记你需要阅读阅读,希望能够对你的职业发展有所帮助。

中高级开发必知必会:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

 <artifactId>spring-context</artifactId>

 <version>5.1.6.RELEASE</version>
 <groupId>org.springframework</groupId>

 <artifactId>spring-jdbc</artifactId>

 <version>5.1.6.RELEASE</version>
 <groupId>com.aliyun</groupId>

 <artifactId>dysmsapi20170525</artifactId>

 <version>2.0.9</version>
 <groupId>junit</groupId>

 <artifactId>junit</artifactId>

 <version>4.12</version>

 <scope>test</scope>
 <groupId>org.springframework</groupId>

 <artifactId>spring-test</artifactId>

 <version>5.1.6.RELEASE</version>
 <groupId>com.aliyun.oss</groupId>

 <artifactId>aliyun-sdk-oss</artifactId>

 <version>3.10.2</version>



### []( )创建config.properties配置文件



> 在原代码中,我们用到的AccessKey ID、AccessKey Secret、域名。短信签名以及短信模板都是固定不变的,因此我们可以将它们拿出来放到一个配置文件中。



#aliyun短信配置参数

aliyun.accessKeyId=LTAI5tLdwwPpCrJbzMdTdQ7w

aliyun.accessKeySecret=jnP9no9KhtsE4kVbqbV40JKCksCqy2

aliyun.endpoint=dysmsapi.aliyuncs.com

aliyun.signName=阿里云短信测试

aliyun.templateCode=SMS_154950909




### []( )创建applicationContext.xml配置文件



> 在配置文件中配置注解自动扫描,以及加载外部配置文件



<beans xmlns=“http://www.springframework.org/schema/beans”

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans.xsd

  http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

<!--  注解扫描  -->

<context:component-scan base-package="com.it"/>

<!--加载外部配置-->

<context:property-placeholder location="classpath:config.properties"/>



### []( )封装工具类



package com.it.sms;

import com.aliyun.dysmsapi20170525.Client;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;

import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;

import com.aliyun.teaopenapi.models.Config;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component //创建当前类的对象

public class MessageTemplate {

@Value("${aliyun.accessKeyId}")

private String accessKeyId;

@Value("${aliyun.accessKeySecret}")

private String accessKeySecret;

@Value("${aliyun.endpoint}")

private String endpoint;

@Value("${aliyun.signName}")

private String signName;

@Value("${aliyun.templateCode}")

private String templateCode;



public void sendMessage(String phone,String code) throws Exception {

    Config config = new Config()

            // 您的AccessKey ID

            .setAccessKeyId(accessKeyId)

            // 您的AccessKey Secret

            .setAccessKeySecret(accessKeySecret);

    // 访问的域名

    config.endpoint = endpoint;

    Client client = new Client(config);

    SendSmsRequest sendSmsRequest = new SendSmsRequest()

            .setSignName(signName)

            .setTemplateCode(templateCode)

            .setPhoneNumbers(phone)

            .setTemplateParam("{\"code\":\""+code+"\"}");

    // 复制代码运行请自行打印 API 的返回值

    SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);

    SendSmsResponseBody body = sendSmsResponse.getBody();

    System.out.println("短信发送结果:"+body.toString());//打印结果

}

}




### []( )测试类



package com.it.sms;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;

//spring整合单元测试

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(“classpath:applicationContext.xml”)

public class MessageTemplateTest {

@Autowired

private MessageTemplate messageTemplate;

最后

小编精心为大家准备了一手资料

以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术

【附】架构书籍

  1. BAT面试的20道高频数据库问题解析
  2. Java面试宝典
  3. Netty实战
  4. 算法

BATJ面试要点及Java架构师进阶资料

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

库问题解析
2. Java面试宝典
3. Netty实战
4. 算法

[外链图片转存中…(img-DkCuhFLV-1714844768122)]

BATJ面试要点及Java架构师进阶资料

[外链图片转存中…(img-36ww8GHH-1714844768123)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值