一、项目搭建
- 项目类型为SpringBoot项目,项目目录结构如下
- common
- 常用枚举类以及统一返回体
- config
- 配置类:跨域配置
- controller
- 项目接口保存目录
- mapper
- 数据库操作层
- model
- 实体类
- service
- 逻辑实现
- util
- 工具类,读取配置类的信息
- common
pom关键依赖
dysmsapi:md5加密依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.9</version>
</dependency>
oss:阿里云文件上传
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.18</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.13.0</version>
</dependency>
redis:数据缓存
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application文件配置
- 配置数据库连接信息、json时间格式化类型、mybatis配置
server:
port: 8080
spring:
application:
name: uni-article-api
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/uni_article?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
username: root
password: root
hikari:
connection-timeout: 30000
minimum-idle: 5
maximum-pool-size: 20
auto-commit: true
idle-timeout: 600000
pool-name: DateSourceHikariCP
max-lifetime: 1800000
connection-test-query: SELECT 1
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
mybatis:
configuration:
map-underscore-to-camel-case: true
- 配置数据库连接信息、json时间格式化类型、mybatis配置
-
二、重要功能实现
短信验证码
- 前置准备工作
- 下载redis 地址:https://github.com/MicrosoftArchive/redis/releases
- set是设置键值对,keys是列出所有键,get是获取指定键的值,del是删除指定的键值。
- 开通阿里云短信服务,并申请模板
- 通过API地址:https://help.aliyun.com/document_detail/101414.html?accessToken=eyJhbGciOiJIUzI1NiIsImtpZCI6ImRlZmF1bHQiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiJhY2Nlc3NfcmVzb3VyY2UiLCJleHAiOjE2NDgzNjk5NjUsImciOiJwbWt4UWdMd1JFQzRNMEFOIiwiaWF0IjoxNjQ4MzY5NjY1LCJ1c2VySWQiOjIzODUyNjg3fQ.qhF5qbFDmjKOVvWp6wBhEHJq3Qw8S9nq7ZKwGvnB2-8
- 下载redis 地址:https://github.com/MicrosoftArchive/redis/releases
- 新建配置文件aliyun.properties
- aliyun.accessKeyId
- aliyun.accessSecret
- aliyun.templateCode:短信验证模板编码
- aliyun.signName:模板名称
- 读取配置文件信息
- @Component 声明该类可以以依赖注入的方式引入
- @PropertySource(“classpath:aliyun.properties”) 需要读取的文件
- @ConfigurationProperties(_prefix = “aliyun”) _ 配置类的前缀
发送短信需要用到的方法
- 先配置key和secrect
- Config config = new Config().setAccessKeyId(_aliyunResource.getAccessKeyId()).setAccessKeySecret(aliyunResource.getAccessKeySecret())_;
- 配置短信模板信息和随机生成的验证码
Client client = new Client(config);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
//客户端传的手机号
.setPhoneNumbers(phone)
.setSignName(aliyunResource.getSignName())
.setTemplateCode(aliyunResource.getTemplateCode())
//随机生成的验证码
.setTemplateParam("{\"code\"" + code + "\"}");
//发送验证码
SendSmsResponse resp = client.sendSms(sendSmsRequest);
// 获得返回结果JSON串
String res = String.valueOf(resp.getBody());
// 转成JSON对象
JSONObject jsonObject = JSON.parseObject(res);
// 返回发送成功与否的标记
if ("OK".equals(jsonObject.get("Code"))) {return true;}
- 先配置key和secrect
随机生成六位验证码
- random.nextInt(10)随机生成数字的范围
- StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象且线程安全 ```java private final static int LENGTH = 6;
/**
- 获取六位随机数据短信验证码 *
- @return
*/
public static String getVerifyCode() {
Random random = new Random();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < LENGTH; i++) {
} return stringBuilder.toString(); } ```stringBuilder.append(random.nextInt(10));
发送的验证码存到redis中,通过手机号和验证码两个值作为登录判断标准
- 前置准备工作
- 头像上传
- 新建配置文件file.properties
- file.host
- file.endPoint
- file.bucketName
- file.objectName
- file.ossHost
- 读取配置文件信息
- @Component 声明该类可以以依赖注入的方式引入
- @PropertySource(“classpath:file.properties”) 需要读取的文件
- @ConfigurationProperties(_prefix = “file”) _ 配置类的前缀
- 文件上传功能实现
- 传值类型为MultipartFile文件类型,读取需要用字节流
@Override
public String uploadFile(MultipartFile file) {
// 读入配置文件
String endpoint = fileResource.getEndPoint();
String accessKeyId = aliyunResource.getAccessKeyId();
String accessKeySecret = aliyunResource.getAccessKeySecret();
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String fileName = file.getOriginalFilename();
// 分割文件名,获得文件后缀名
assert fileName != null;
String[] fileNameArr = fileName.split("\\.");
String suffix = fileNameArr[fileNameArr.length - 1];
// 拼接得到新的上传文件名
String uploadFileName = fileResource.getObjectName() + UUID.randomUUID() + "." + suffix;
// 上传网络需要用的字节流
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
} catch (IOException e) {
System.err.println("上传文件出现异常");
}
// 执行阿里云上传文件操作
ossClient.putObject(fileResource.getBucketName(), uploadFileName, inputStream);
// 关闭OSSClient
ossClient.shutdown();
return uploadFileName;
}
- 传值类型为MultipartFile文件类型,读取需要用字节流
- 新建配置文件file.properties