Springboot项目调用阿里云号码隐私保护服务【真实有用】

1、首先去阿里云申请号码隐私保护服务,申请号码池以及专属号码,获取有效的阿里云AK。

在这里插入图片描述

2、导入以下两个依赖
<dependency>
     <groupId>com.aliyun</groupId>
     <artifactId>aliyun-java-sdk-dyplsapi</artifactId>
     <version>1.2.0</version>
 </dependency>
 
 <dependency>
     <groupId>com.aliyun</groupId>
     <artifactId>aliyun-java-sdk-core</artifactId>
     <version>4.1.0</version>
 </dependency>
3、在yml文件中写入有效的AK
oss:
  product: Dyplsapi
  domain: dyplsapi.aliyuncs.com
  accessKeyId: XXXXXXXXXXXXXXXXXXXXXX
  accessKeySecret: XXXXXXXXXXXXXXXX
4、调用代码
/**
 * 阿里云号码隐私拨号保护API
 *
 * @author shiwen
 * @date 2020/9/15
 */
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyplsapi.model.v20170525.*;
import com.aliyuncs.exceptions.ClientException;
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.context.annotation.Configuration;

@Configuration
public class PhoneEncryption {

    @Autowired
    private RedisUtils redisUtils;

    @Value("${oss.product}")
    private String product;
    @Value("${oss.domain}")
    private String domain;
    @Value("${oss.accessKeyId}")
    private String accessKeyId;
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    /**
     * AXB绑定示例
     *
     * @return
     * @throws ClientException
     */
    public BindAxbResponse bindAxb(String phoneA, String phoneB) throws ClientException {
        //设置超时时间-可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化ascClient,暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //AXB绑定请求结构体-参数说明详见参数说明
        BindAxbRequest request = new BindAxbRequest();
        //必填:号池Key-详见概览页面的号池管理功能
        request.setPoolKey("XXXXXXXXXXXXXX");
        //必填:AXB关系中的A号码
        request.setPhoneNoA(phoneA);
        //必选:AXB中的B号码
        request.setPhoneNoB(phoneB);
        //可选:指定X号码选号
        request.setPhoneNoX("XXXXXXXXXXXXXX");
        //可选:指定需要分配归属城市的X号码
        //request.setExpectCity("北京');
        //必填:绑定关系对应的失效时间-不能早于当前系统时间(100秒)
        request.setExpiration(DateTimeHelper.timestampToLocalDateTimeStr((System.currentTimeMillis() + 100000L)));
        //可选:是否需要录制音频-默认是false
        request.setIsRecordingEnabled(false);
        //hint 此处可能会抛出异常,注意catch
        BindAxbResponse response = acsClient.getAcsResponse(request);
        if (response.getCode() != null && response.getCode().equals("OK")) {
            //请求成功
            return response;
        }
        return null;
    }

    /**
     * 订购关系解绑示例(解绑接口不区分AXB、AXN)
     *
     * @return
     * @throws ClientException
     */
    public UnbindSubscriptionResponse unbind(String subsId, String secretNo) throws ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        UnbindSubscriptionRequest request = new UnbindSubscriptionRequest();
        //必填:对应的号池Key
        request.setPoolKey("XXXXXXXXXXXXXXXXXXXX");
        //必填-分配的X小号-对应到绑定接口中返回的secretNo;
        request.setSecretNo(secretNo);
        //可选-绑定关系对应的ID-对应到绑定接口中返回的subsId;
        request.setSubsId(subsId);
        UnbindSubscriptionResponse response = acsClient.getAcsResponse(request);
        return response;
    }

    public Boolean phoneEncryption(String phoneA, String phoneB) {
        // 这一步需要先拿到最新的redis保存的subsId, 解绑我们的隐私号码, 然后才能进行电话的拨打操作
        String cacheSubsId = redisUtils.get(CACHE_KEY);
        // 如果数据库中已经存在subsId的话, 就直接让它失效
        if (GeneralUtil.isNotNullAndEmpty(cacheSubsId)) {
            try {
                // 解绑之前的手机号
                unbind(cacheSubsId, "XXXXXXXXXXXXXXXXXXXX");
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
        // 如果subsId不为空的话,就直接绑定AXB
        BindAxbResponse axbResponse = null;
        try {
            axbResponse = bindAxb(phoneA, phoneB);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        String axbSubsId = axbResponse.getSecretBindDTO() == null ? null : axbResponse.getSecretBindDTO().getSubsId();
        if (GeneralUtil.isObjNotNull(axbResponse) && GeneralUtil.isNotNullAndEmpty(axbSubsId)) {
            redisUtils.set(CACHE_KEY, axbSubsId);
            LogUtil.info("Processing phoneEncryption success!Code = %s , RequestId = %s , subsId = %s"
                    , axbResponse.getCode(), axbResponse.getRequestId(), axbSubsId);
            return true;
        }
        return false;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值