交友项目自动装配模块封装阿里云发送短信&oss对象存储&百度云人脸识别(代码片段)

爱吃豆的土豆 爱吃豆的土豆     2023-04-01     284

关键词:

目录

自动装配模块:tanhua-autoconfig

1:SpringBoot封装短信

1.1:配置类

1.2:发送短信模板对象

1.3:自动装配类

1.4:自动装配配置

2:SpringBoot封装OSS对象

2.1:配置类

2.2:发送短信模板对象

2.3:自动装配类

2.4:自动装配配置

3:SpringBoot封装百度云人脸识别

3.1:配置类

3.2:发送短信模板对象

3.3:自动装配类

3.4:自动装配配置


自动装配模块:tanhua-autoconfig

properties用来进行读取yml文件的参数进行封装参数,

template模板用来封装短信,对象存储,人脸识别模板或者说是方法用来进行调用

 配置类:将其封装的模板注入Bean

@EnableConfigurationProperties(value = SmsProperties.class, OssProperties.class, FaceProperties.class)
public class TanhuaAutoConfiguration 
    @Bean
    private SmsTemplate smsTemplate(SmsProperties smsProperties)
        return new SmsTemplate(smsProperties);
    
    @Bean
    private OssTemplate ossTemplate(OssProperties ossProperties)
        return new OssTemplate(ossProperties);
    
    @Bean
    private FaceTemplate faceTemplate(FaceProperties faceProperties)
        return new FaceTemplate(faceProperties);
    

 

在resources目录下创建spring.factories配置文件,加载配置类

1:SpringBoot封装短信

1.1:配置类

tanhua-autoconfig模块创建配置信息类SmsProperties

@Data
@ConfigurationProperties(prefix = "tanhua.sms")
public class SmsProperties 
    private String signName;
    private String templateCode;
    private String accessKey;
    private String secret;

1.2:发送短信模板对象

tanhua-autoconfig模块创建模板对象发送信息

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 10:59
 */
@Data
public class SmsTemplate 
    private SmsProperties properties;

    public SmsTemplate(SmsProperties properties) 
        this.properties = properties;
    

    public void sendSms(String mobile, String username ,String code) 
        try 
            //配置阿里云
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(properties.getAccessKey())
                    // 您的AccessKey Secret
                    .setAccessKeySecret(properties.getSecret());
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";

            com.aliyun.dysmsapi20170525.Client client =  new com.aliyun.dysmsapi20170525.Client(config);

            String address = "默认地址";
            String phone = "13612345678";

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobile)
                    .setSignName(properties.getSignName())
                    .setTemplateCode(properties.getTemplateCode())
                    .setTemplateParam("\\"name\\":\\""+username+"\\",\\"code\\":\\""+code+"\\",\\"address\\":\\""+address+"\\",\\"phone\\":\\""+phone+"\\"");
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSms(sendSmsRequest);

            SendSmsResponseBody body = response.getBody();

            System.out.println(body.getMessage());

        catch (Exception e) 
            e.printStackTrace();
        
    

 

1.3:自动装配类

tanhua-autoconfig模块创建自动装配的配置类

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 11:03
 */
@EnableConfigurationProperties(value = SmsProperties.class, OssProperties.class, FaceProperties.class)
public class TanhuaAutoConfiguration 
    @Bean
    private SmsTemplate smsTemplate(SmsProperties smsProperties)
        return new SmsTemplate(smsProperties);
    
    @Bean
    private OssTemplate ossTemplate(OssProperties ossProperties)
        return new OssTemplate(ossProperties);
    
    @Bean
    private FaceTemplate faceTemplate(FaceProperties faceProperties)
        return new FaceTemplate(faceProperties);
    

 

1.4:自动装配配置

  • 根据自动装配原则,在tanhua-autoconfig模块创建/META-INF/spring.factories文件

 

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\
com.czxy.tanhua.autoconfig.TanhuaAutoConfiguration

2:SpringBoot封装OSS对象

2.1:配置类

tanhua-autoconfig模块创建配置信息类OssProperties

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/31 9:44
 */
@Data
@ConfigurationProperties(prefix = "tanhua.oss")
public class OssProperties 
    private String pathProtocol;    //路径协议

    private String endpoint;        //地域

    private String keyId;           //账号

    private String keySecret;       //密码

    private String dirName;         //上传目录

    private String bucketName;      //Bucket 名称

 

2.2:发送OSS对象模板

tanhua-autoconfig模块创建模板对象保存信息

OssTemplate对象模板

package com.czxy.tanhua.autoconfig.template;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.czxy.tanhua.autoconfig.properties.OssProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/31 9:49
 */
@Component
public class OssTemplate 
    private OssProperties ossProperties;

    public OssTemplate(OssProperties properties) 
        this.ossProperties = properties;
    

    public String upload(File file) 
        if(file == null) 
            throw new RuntimeException("上传文件为空");
        
        try 
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getKeyId(), ossProperties.getKeySecret());
            String path = ossProperties.getDirName() + "/" + System.currentTimeMillis() + ".png";
            // 数据流
            FileInputStream fileInputStream = new FileInputStream(file);
            // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
            ossClient.putObject(ossProperties.getBucketName(), path, fileInputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            String url = ossProperties.getPathProtocol() + "://"+ossProperties.getBucketName()+"."+ossProperties.getEndpoint()+"/" + path;
            return url;
         catch (IOException e) 
            throw new RuntimeException("文件上传有误");
        

    

2.3:自动装配类

 tanhua-autoconfig模块创建自动装配的配置类

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 11:03
 */
@EnableConfigurationProperties(value = SmsProperties.class, OssProperties.class, FaceProperties.class)
public class TanhuaAutoConfiguration 
    @Bean
    private SmsTemplate smsTemplate(SmsProperties smsProperties)
        return new SmsTemplate(smsProperties);
    
    @Bean
    private OssTemplate ossTemplate(OssProperties ossProperties)
        return new OssTemplate(ossProperties);
    
    @Bean
    private FaceTemplate faceTemplate(FaceProperties faceProperties)
        return new FaceTemplate(faceProperties);
    

 

2.4:自动装配配置

  •  根据自动装配原则,在tanhua-autoconfig模块创建/META-INF/spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\
com.czxy.tanhua.autoconfig.TanhuaAutoConfiguration

3:SpringBoot封装百度云人脸识别

 

3.1:配置类

tanhua-autoconfig模块创建配置信息类FaceProperties

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/31 10:24
 */
@Data
@ConfigurationProperties(prefix = "tanhua.aip")
public class FaceProperties 
    private String appId;
    private String apiKey;
    private String secretKey;
    @Bean
    public AipFace aipFace()
        AipFace client = new AipFace(appId, apiKey, secretKey);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        return client;
    

3.2:发送百度人脸图像识别模板

tanhua-autoconfig模块创建配置人脸识别类:检测识别结果

package com.czxy.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import com.czxy.tanhua.autoconfig.properties.FaceProperties;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/31 10:27
 */
@Component
public class FaceTemplate 
    @Resource
    private AipFace aipFace;
    private FaceProperties faceProperties;
    public FaceTemplate(FaceProperties faceProperties)
        this.faceProperties = faceProperties;
    

    public boolean FaceUtils(String imageurl)
        
        String imageType = "URL";


            // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
//        options.put("liveness_control", "LOW");

        // 人脸检测
        JSONObject res = aipFace.detect(imageurl, imageType, options);
        Integer error_code = (Integer) res.get("error_code");
        return error_code == 0;
    

3.3:自动装配类

 tanhua-autoconfig模块创建自动装配的配置类

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 11:03
 */
@EnableConfigurationProperties(value = SmsProperties.class, OssProperties.class, FaceProperties.class)
public class TanhuaAutoConfiguration 
    @Bean
    private SmsTemplate smsTemplate(SmsProperties smsProperties)
        return new SmsTemplate(smsProperties);
    
    @Bean
    private OssTemplate ossTemplate(OssProperties ossProperties)
        return new OssTemplate(ossProperties);
    
    @Bean
    private FaceTemplate faceTemplate(FaceProperties faceProperties)
        return new FaceTemplate(faceProperties);
    

3.4:自动装配配置

  •   根据自动装配原则,在tanhua-autoconfig模块创建/META-INF/spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\
com.czxy.tanhua.autoconfig.TanhuaAutoConfiguration

 

使用阿里云的短信服务发送短信(代码片段)

...处理方式也不太一样,之前用的一个厂商的,提供了一个封装类就很容易发送短息,因此都是基于HTTP协议做的一个数据发送而已,接触阿里云的短信服务器后,发现阿里云还增加了非常多的参数,其中包括一些秘钥和签名的内... 查看详情

阿里云短信验证_基于阿里云openapi实现(代码片段)

...信验证以及短信通知,目前已经应用的非常广泛,最近因项目需要,需要将原来的短信接口换成阿里云的的短信服务,原项目集成的短信服务能够实现短信的发送以及短信的验证整个过程,简单的来说,原来的短息服务,只需应... 查看详情

springboot入门到精通-基于阿里云短信服务-定义starter封装通用组件(代码片段)

前言在实际项目开发中,有大量的公用功能,SpringBoot提供了便利的方式支持我们对公共业务功能进行抽取封装,本篇文章是基于SpringBoot定义starter,以短信为例,定义通用的starter组件实现短信的发送。开启阿... 查看详情

springboot入门到精通-基于阿里云短信服务-定义starter封装通用组件(代码片段)

前言在实际项目开发中,有大量的公用功能,SpringBoot提供了便利的方式支持我们对公共业务功能进行抽取封装,本篇文章是基于SpringBoot定义starter,以短信为例,定义通用的starter组件实现短信的发送。开启阿... 查看详情

2021-07-24.net高级班118-直播项目专题(阿里云实现短信验证码发送)(代码片段)

一:发送短信验证码///<summary>///发送短信验证码///</summary>///<paramname="mobile"></param>///<returns></returns>[Route("SendVerifyCode")][HttpPost</ 查看详情

c#使用阿里云发送短信

最近有个项目,短信服务使用的是阿里云的,想要使用阿里云平台的短信服务,首先要注册一个阿里云账号,由于发送短信消息需要用到短信签名、短信模板ID(已添加并通过审核)1、注册阿里云账号࿰... 查看详情

个人项目需要发送验证码?无法对接运营商,试试第三方工具阿里云吧(代码片段)

验证码1、发送验证码2、短信服务(推荐)2.1、注册购买2.2、代码测试1、发送验证码短信发送是电信运营商提供的服务,需要访问对应的接口,不同运营商提供的接口地址肯定不一样,如果直接访问这些接口... 查看详情

发送短信(阿里云短信发送方式)

/**发送短信(阿里云短信发送方式)*/functionsms($code,$mobile,$string)$iClientProfile=DefaultProfile::getProfile("cn-hangzhou","","");$client=newDefaultAcsClient($iClientProfile);$request=newSms\\SingleSendSmsRequest();$request->setSignN... 查看详情

阿里云实现发送短信(java实例教程)(代码片段)

...推荐)🐳注册购买🏓代码测试🖐Java组件封装🥝发送实例🥝pom依赖🌈Spring.factories(略)🍎麻烦给博主点个关注+收藏+点赞!🍋发送验证码短信发送是电信运营商提供的服务,需要... 查看详情

阿里云短信发送

一、准备工作:1、下载SDK工具包https://help.aliyun.com/document_detail/55359.html?spm=5176.doc55451.2.6.BgY0732、申请ID和Secret3、申请签名https://dysms.console.aliyun.com/dysms.htm?spm=5176.8195934.499931.1.64sayk#/4、选择短信模板 查看详情

flask实战第41天:发送短信验证码

本项目使用的短信运营商是阿里云。使用淘宝账号登录阿里云控制台。在“产品与服务”中搜索“短信”进入短信服务获取AccessKey输入子账户用户名权限选择管理短信服务  签名管理:申请签名模板管理:设... 查看详情

django之集成阿里云通信(发送手机短信验证码)(代码片段)

python3+django2.0集成“阿里云通信”服务:(SDK文档地址:https://help.aliyun.com/document_detail/55491.html?spm=5176.10629532.106.3.2fe01cbeAp0iFO)步骤1:  在阿里云“短信服务”中创建一个签名步骤2:  在阿里云“短信服务”中创建一个短信... 查看详情

阿里云批量发送短信功能测试

packagecom.yongjie.ZhiJianSbpt.sms;importjava.text.SimpleDateFormat;importjava.util.Date;importcom.aliyuncs.DefaultAcsClient;importcom.aliyuncs.IAcsClient;importcom.aliyuncs.dysmsapi.model.v20170525.Q 查看详情

阿里云发送短信验证码失败

报错:把require_once ‘/api_sdk/vendor/autoload.php‘;改为require_once __DIR__.‘/api_sdk/vendor/autoload.php‘;文件目录一定要写完整  查看详情

《java开发学习大纲文档》v8.0

...入的部分有云开发(阿里云OSS存储、(github)gitlab+docker网站自动化部署、码云自动化部署、阿里云自带自动化部署、短信模块(七牛短信、阿里云短信、消息队列处理高并发问题)、(图形、Ecxce、Word文档)报表汇报模块) 查看详情

阿里云短信验证码

  开通云通信-短信服务  点击短信发送API(SendSms)---JAVA  仔细阅读短信发送API(SendSms)---JAVA,按照图片中的步骤进行操作(AccessKeyId,AccessKeySecret, 短信签名, 短信模板)  技术对接步骤   &n 查看详情

交友项目后端环境搭建(代码片段)

目录1:环境搭建1.1:MYSQL数据库1.1.1:导入相应的sql1.2:Linux中的docker-compose方法集中部署1.2.1:介绍1.3:IDEA设置1.3.1:基本要求1.3.2:设置项目编码格式1.3.3:设置Maven仓库1.4:Maven模块分析1.4... 查看详情

使用阿里云短信服务发送短信验证码(代码片段)

阿里云短发服务使用流程:  1.在阿里云上完成短信服务的购买。  2.导入相关的jar包。<!--阿里云短信服务--><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.1.0</vers... 查看详情