QQ+Phone之JAVA实现验证码发送=>注册登录使用

实现起来都是比较简单的!麻烦在配置开通等等

1. 流程

(可能有成本)

我们平常发手机短信,首先我们得有自己的手机号,然后输入目标手机号,然后进行发送短信

那其实我们首先登录注册时想要获取验证码,就类似于我们用一个手机发送短信给目标手机号一样;我们后面就是要去腾讯云那边开通一个短信服务,就相当于有一个手机可以发短信,我们只需要调用sdk方法让他去发短信!

(零成本)

我们平常发送qq邮箱,也是要有自己的邮箱,然后输入目标邮箱,然后发送邮箱信息

同理我们需要在我们的qq邮箱去开启服务(主要就是来链接qq那边的接口,然后发送短信服务)

2. 腾讯云短信验证码注册

腾讯云短信服务实现 Java 发送手机验证码(SpringBoot+Redis 实现)-腾讯云开发者社区-腾讯云

2.1. 腾讯云短信服务

首先我们想要使用给腾讯云的短信服务需要上传很多资料的,阿里云的短信服务也是如此!

腾讯云搜索

2.2. 实名资质管理:

2.3. 签名管理:

可以去弄一个简单的公众号!

2.4. 正文模板管理:

主要是去设置你的短信内容发送的模板样式

2.5. 审核完毕所有内容后:

2.6. 复制好你之前的签名模板等id(有用)

比如:

2.7. 同时需要密钥

登录 - 腾讯云

所以需要注意创建时保留好SecretId和 SecretKey

2.8. SDKAppID

https://console.cloud.tencent.com/smsv2/app-manage

2.9. SDK实现——maven

2.9.1. 引入依赖包

<!-- 腾讯云手机号验证码依赖包 -->
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
    <!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
    <version>3.1.1000</version>
</dependency>

如果下载的慢的话:

2.9.2. 代码复制

只需要填写相关信息就可以使用了

简化版:

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
 
public class TencentSmsSender {
 
    public void sendSms(String phoneNumber, String verifyCode) throws TencentCloudSDKException {
        String secretId = "你的腾讯云API密钥-SecretId";
        String secretKey = "你的腾讯云API密钥-SecretKey";
        String sdkAppId = "你的腾讯云短信App ID"; // 例如:1400006666
        String signName = "你的短信签名内容"; // 例如:"腾讯云短信测试"
        String templateId = "你的短信模板ID"; // 例如:"1176554"
 
        Credential cred = new Credential(secretId, secretKey);
        SmsClient client = new SmsClient(cred, "ap-guangzhou"); // 选择广州地区
 
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppid(sdkAppId);
        req.setSign(signName);
        req.setTemplateID(templateId);
        req.setPhoneNumberSet(phoneNumber);
        req.setTemplateParamSet(verifyCode);
 
        SendSmsResponse resp = client.SendSms(req);
 
        // 输出返回的结果
        System.out.println(SendSmsResponse.toJsonString(resp));
    }
 
    public static void main(String[] args) {
        try {
            TencentSmsSender sender = new TencentSmsSender();
            sender.sendSms("收信人手机号", "验证码内容");
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
    }
}

完整版

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

//导入可选配置类
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

// 导入对应SMS模块的client
import com.tencentcloudapi.sms.v20210111.SmsClient;

// 导入要请求接口对应的request response类
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

/**
 * Tencent Cloud Sms Sendsms
 *
 */
public class SendSms
{
    public static void main(String[] args)
    {
        try {
            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId,SecretKey。
            // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中,请参考凭证管理 https://github.com/TencentCloud/tencentcloud-sdk-java?tab=readme-ov-file#%E5%87%AD%E8%AF%81%E7%AE%A1%E7%90%86。
            // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
            // SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi
            // Credential cred = new Credential("SecretId", "SecretKey");
            Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));
            
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            // 从3.0.96版本开始, 单独设置 HTTP 代理(无需要直接忽略)
            // httpProfile.setProxyHost("真实代理ip");
            // httpProfile.setProxyPort(真实代理端口);
很抱歉,我作为AI语言模型无法直接执行Java程序,但是我可以提供以下Java代码示例参考。 首先,你需要使用QQAPI发送验证码。你可以在QQ的开放平台上申请API密钥。然后,使用Java的HTTP请求发送POST请求来调用API。 下面是使用Java发送POST请求的示例代码: ```java import java.net.HttpURLConnection; import java.net.URL; import java.io.OutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; public class SendCode { public static void main(String[] args) { try { // 构造POST请求URL和参数 URL url = new URL("https://api.qq.com/send_code"); String params = "phone=123456789&code=1234"; // 配置HTTP请求 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 发送请求 OutputStream out = conn.getOutputStream(); out.write(params.getBytes()); out.flush(); out.close(); // 读取响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; StringBuffer response = new StringBuffer(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 输出响应 System.out.println(response.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意:上述代码中的URL和参数仅供参考,实际使用时需要修改为你自己的API地址和参数。 另外,为了保证验证码的安全性,建议使用HTTPS协议发送请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值