创建子模块mall-third-party 整合阿里云OSS等第三方模块

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.zsy</groupId>
  7. <artifactId>guli-mall</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <artifactId>mall-third-party</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <name>mall-third-party</name>
  13. <description>第三方服务整合</description>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.zsy</groupId>
  17. <artifactId>mall-common</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>com.alibaba.cloud</groupId>
  21. <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
  22. <version>2.2.0.RELEASE</version>
  23. </dependency>
  24. </dependencies>
  25. <build>
  26. <plugins>
  27. <plugin>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-maven-plugin</artifactId>
  30. </plugin>
  31. </plugins>
  32. </build>
  33. </project>

application.yaml

  1. spring:
  2. cloud:
  3. nacos:
  4. discovery:
  5. server-addr: 192.168.163.131:8848
  6. alicloud:
  7. access-key: LTAI4FwvfjSycd1APnuG9bjj
  8. secret-key: O6xaxyiWfSIitcOkSuK27ju4hXT5Hl
  9. oss:
  10. endpoint: oss-cn-beijing.aliyuncs.com
  11. bucket: gulimall-hello
  12. application:
  13. name: mall-third-party
  14. server:
  15. port: 30000

bootstrap.yaml

  1. spring:
  2. application:
  3. name: mall-third-party
  4. cloud:
  5. nacos:
  6. config:
  7. server-addr: 192.168.163.131:8848
  8. namespace: 90a6e497-bfb6-4784-8dd6-244c08b0708c
  9. file-extension: yaml
  10. extension-configs:
  11. - data-id: oss.yaml
  12. group: DEFAULT_GROUP
  13. refresh: true

服务端签名接口

OssController.java

  1. package com.zsy.third.party.controller;
  2. /**
  3. * @author: zhangshuaiyin
  4. * @date: 2021/3/7 19:29
  5. */
  6. @RestController
  7. public class OssController {
  8. @Autowired
  9. OSS ossClient;
  10. @Value("${spring.cloud.alicloud.oss.endpoint}")
  11. private String endpoint;
  12. @Value("${spring.cloud.alicloud.oss.bucket}")
  13. private String bucket;
  14. @Value("${spring.cloud.alicloud.access-key}")
  15. private String accessId;
  16. /**
  17. * Oss 获取服务端签名
  18. * @return
  19. */
  20. @RequestMapping("/oss/policy")
  21. public R policy() {
  22. // https://gulimall-hello.oss-cn-beijing.aliyuncs.com/hahaha.jpg host的格式为 bucketname.endpoint
  23. String host = "https://" + bucket + "." + endpoint;
  24. // callbackUrl为 上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。
  25. // String callbackUrl = "http://88.88.88.88:8888";
  26. String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  27. // 用户上传文件时指定的前缀。
  28. String dir = format + "/";
  29. Map<String, String> respMap = null;
  30. try {
  31. long expireTime = 30;
  32. long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
  33. Date expiration = new Date(expireEndTime);
  34. PolicyConditions policyConds = new PolicyConditions();
  35. policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
  36. policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
  37. String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
  38. byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
  39. String encodedPolicy = BinaryUtil.toBase64String(binaryData);
  40. String postSignature = ossClient.calculatePostSignature(postPolicy);
  41. respMap = new LinkedHashMap<String, String>();
  42. respMap.put("accessid", accessId);
  43. respMap.put("policy", encodedPolicy);
  44. respMap.put("signature", postSignature);
  45. respMap.put("dir", dir);
  46. respMap.put("host", host);
  47. respMap.put("expire", String.valueOf(expireEndTime / 1000));
  48. // respMap.put("expire", formatISO8601Date(expiration));
  49. } catch (Exception e) {
  50. // Assert.fail(e.getMessage());
  51. System.out.println(e.getMessage());
  52. }
  53. return R.ok().put("data", respMap);
  54. }
  55. }

使用网关服务统一接入

mall-gateway 服务 application.yaml
注意配置规则断言要讲更精确的放在前面

  1. spring:
  2. application:
  3. name: mall-gateway
  4. cloud:
  5. gateway:
  6. routes:
  7. - id: third_party_route
  8. uri: lb://mall-third-party
  9. predicates:
  10. - Path=/api/third-party/**
  11. filters:
  12. - RewritePath=/api/third-party/(?<segment>.*),/$\{segment}