一、项目搭建

  • 项目类型为SpringBoot项目,项目目录结构如下
    • common
      • 常用枚举类以及统一返回体
    • config
      • 配置类:跨域配置
    • controller
      • 项目接口保存目录
    • mapper
      • 数据库操作层
    • model
      • 实体类
    • service
      • 逻辑实现
    • util
      • 工具类,读取配置类的信息
  • pom关键依赖

    • dysmsapi:md5加密依赖

      1. <dependency>
      2. <groupId>com.aliyun</groupId>
      3. <artifactId>dysmsapi20170525</artifactId>
      4. <version>2.0.9</version>
      5. </dependency>
    • oss:阿里云文件上传

      1. <dependency>
      2. <groupId>com.aliyun</groupId>
      3. <artifactId>aliyun-java-sdk-core</artifactId>
      4. <version>4.5.18</version>
      5. </dependency>
      6. <dependency>
      7. <groupId>com.aliyun.oss</groupId>
      8. <artifactId>aliyun-sdk-oss</artifactId>
      9. <version>3.13.0</version>
      10. </dependency>
    • redis:数据缓存

      1. <dependency>
      2. <groupId>org.springframework.boot</groupId>
      3. <artifactId>spring-boot-starter-data-redis</artifactId>
      4. </dependency>
  • application文件配置

    • 配置数据库连接信息、json时间格式化类型、mybatis配置
      1. server:
      2. port: 8080
      3. spring:
      4. application:
      5. name: uni-article-api
      6. datasource:
      7. type: com.zaxxer.hikari.HikariDataSource
      8. driver-class-name: com.mysql.cj.jdbc.Driver
      9. url: jdbc:mysql://localhost:3306/uni_article?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
      10. username: root
      11. password: root
      12. hikari:
      13. connection-timeout: 30000
      14. minimum-idle: 5
      15. maximum-pool-size: 20
      16. auto-commit: true
      17. idle-timeout: 600000
      18. pool-name: DateSourceHikariCP
      19. max-lifetime: 1800000
      20. connection-test-query: SELECT 1
      21. jackson:
      22. date-format: yyyy-MM-dd HH:mm:ss
      23. time-zone: GMT+8
      24. mybatis:
      25. configuration:
      26. map-underscore-to-camel-case: true
  • mappper层使用mybatis对数据库操作

    二、重要功能实现

  • 短信验证码

    • 前置准备工作
    • 新建配置文件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())_;
      • 配置短信模板信息和随机生成的验证码
        1. Client client = new Client(config);
        2. SendSmsRequest sendSmsRequest = new SendSmsRequest()
        3. //客户端传的手机号
        4. .setPhoneNumbers(phone)
        5. .setSignName(aliyunResource.getSignName())
        6. .setTemplateCode(aliyunResource.getTemplateCode())
        7. //随机生成的验证码
        8. .setTemplateParam("{\"code\"" + code + "\"}");
        9. //发送验证码
        10. SendSmsResponse resp = client.sendSms(sendSmsRequest);
        11. // 获得返回结果JSON串
        12. String res = String.valueOf(resp.getBody());
        13. // 转成JSON对象
        14. JSONObject jsonObject = JSON.parseObject(res);
        15. // 返回发送成功与否的标记
        16. if ("OK".equals(jsonObject.get("Code"))) {return true;}
    • 随机生成六位验证码

      • 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++) {
        1. stringBuilder.append(random.nextInt(10));
        } return stringBuilder.toString(); } ```
    • 发送的验证码存到redis中,通过手机号和验证码两个值作为登录判断标准

  • 头像上传
    • 新建配置文件file.properties
      • file.host
      • file.endPoint
      • file.bucketName
      • file.objectName
      • file.ossHost
    • 读取配置文件信息
      • @Component 声明该类可以以依赖注入的方式引入
      • @PropertySource(“classpath:file.properties”) 需要读取的文件
      • @ConfigurationProperties(_prefix = “file”) _ 配置类的前缀
    • 文件上传功能实现
      • 传值类型为MultipartFile文件类型,读取需要用字节流
        1. @Override
        2. public String uploadFile(MultipartFile file) {
        3. // 读入配置文件
        4. String endpoint = fileResource.getEndPoint();
        5. String accessKeyId = aliyunResource.getAccessKeyId();
        6. String accessKeySecret = aliyunResource.getAccessKeySecret();
        7. // 创建OSSClient实例
        8. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        9. String fileName = file.getOriginalFilename();
        10. // 分割文件名,获得文件后缀名
        11. assert fileName != null;
        12. String[] fileNameArr = fileName.split("\\.");
        13. String suffix = fileNameArr[fileNameArr.length - 1];
        14. // 拼接得到新的上传文件名
        15. String uploadFileName = fileResource.getObjectName() + UUID.randomUUID() + "." + suffix;
        16. // 上传网络需要用的字节流
        17. InputStream inputStream = null;
        18. try {
        19. inputStream = file.getInputStream();
        20. } catch (IOException e) {
        21. System.err.println("上传文件出现异常");
        22. }
        23. // 执行阿里云上传文件操作
        24. ossClient.putObject(fileResource.getBucketName(), uploadFileName, inputStream);
        25. // 关闭OSSClient
        26. ossClient.shutdown();
        27. return uploadFileName;
        28. }