引入passjava-common模块,并且排除mybatis-plus依赖
<dependency> <groupId>com.jackson0714.passjava</groupId> <artifactId>passjava-common</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </exclusion> </exclusions> </dependency>配置服务发现和端口
spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 application: name: passjava-thirdparty server: port: 14000配置配置中心
spring.application.name=passjava-thirdparty spring.cloud.nacos.config.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.namespace=passjava-thirdparty spring.cloud.nacos.config.extension-configs[0].data-id=oss.yml spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP spring.cloud.nacos.config.extension-configs[0].refresh=true配置Nacos命名空间和oss.yml
spring: cloud: alicloud: access-key: LTAI4G3KxBJ26EUbWsenmqhP secret-key: RHtADVlvlKJvVBQnFNNvnne9p4NwnA oss: endpoint: oss-cn-beijing.aliyuncs.com开启服务发现@EnableDiscoveryClient
@EnableDiscoveryClient @SpringBootApplication public class PassjavaThirdpartyApplication { public static void main(String[] args) { SpringApplication.run(PassjavaThirdpartyApplication.class, args); } } 4.2 获取签名类 @RestController @RequestMapping("/thirdparty/v1/admin/oss") public class OssController { @Autowired OSS ossClient; @Value("${spring.cloud.alicloud.access-key}") private String accessId; @Value("${spring.cloud.alicloud.secret-key}") private String accessKey; @Value("${spring.cloud.alicloud.oss.endpoint}") private String endpoint; @Value("${spring.cloud.alicloud.oss.bucket}") private String bucket; @RequestMapping("/getPolicy") public Map<String, String> getPolicy() { String host = "https://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint // callbackUrl为 上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。 // String callbackUrl = "http://88.88.88.88:8888"; String formatDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); String dir = formatDate + "http://www.likecs.com/"; // 用户上传文件时指定的前缀。 Map<String, String> respMap = new LinkedHashMap<String, String>(); try { long expireTime = 30; long expireEndTime = System.currentTimeMillis() + expireTime * 1000; Date expiration = new Date(expireEndTime); PolicyConditions policyConds = new PolicyConditions(); policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000); policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir); String postPolicy = ossClient.generatePostPolicy(expiration, policyConds); byte[] binaryData = postPolicy.getBytes("utf-8"); String encodedPolicy = BinaryUtil.toBase64String(binaryData); String postSignature = ossClient.calculatePostSignature(postPolicy); respMap.put("accessid", accessId); respMap.put("policy", encodedPolicy); respMap.put("signature", postSignature); respMap.put("dir", dir); respMap.put("host", host); respMap.put("expire", String.valueOf(expireEndTime / 1000)); } catch (Exception e) { // Assert.fail(e.getMessage()); System.out.println(e.getMessage()); } finally { ossClient.shutdown(); } return respMap; } }测试接口
:14000/api/thirdparty/v1/admin/oss/getPolicy { "accessid": "LTAI4G3KxBJ26EUbWsenmqhP", "policy": "eyJleHBpcmF0aW9uIjoiMjAyMC0wNC0yOFQwMjozMzowNy42NzNaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIwLTA0LTI4LyJdXX0=", "signature": "pfn4cggFTMMNqTs+qUnDN5c+k5M=", "dir": "2020-04-28/", "host": "https://passjava.oss-cn-beijing.aliyuncs.com", "expire": "1588041187" } 4.3 配置网关路由