diff --git a/README.md b/README.md index c216797..a76d622 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,8 @@ parent:版本封装
base:功能封装 #### 最新版本 ->yexuejc.springboot.version=2.0.0
->yexuejc.base.version=1.1.9 +>yexuejc.springboot.version=2.0.2
+>yexuejc.base.version=1.2.0 pom.xml ``` @@ -104,5 +104,4 @@ pom.xml [更新记录](UPDATE.md)
-[1.0.6新增 针对API请求安全解决方案](PARAMS_RSA_DECRYPT_ENCRYPT.md)
-[1.0.6新增 加密功能](PARAMS_RSA_DECRYPT_ENCRYPT.md) +[相关功能使用指南](doc/MENU.md) diff --git a/doc/MENU.md b/doc/MENU.md new file mode 100644 index 0000000..3e8eb37 --- /dev/null +++ b/doc/MENU.md @@ -0,0 +1,13 @@ +功能目录 +------------------------ + +### 第三方集成 +# +* [redis 集成](REDIS.md) +* [Aliyun MNS 消息队列](MNS.md) +* [Aliyun OSS 对象存储](OSS.md) + +### 内部集成 +# +* [1.0.6新增 针对API请求安全解决方案](PARAMS_RSA_DECRYPT_ENCRYPT.md)
+* [1.0.6新增 加密功能](PARAMS_RSA_DECRYPT_ENCRYPT.md) diff --git a/doc/MNS.md b/doc/MNS.md new file mode 100644 index 0000000..0e22465 --- /dev/null +++ b/doc/MNS.md @@ -0,0 +1,34 @@ +aliyun MNS 消息队列 使用指南 +------------- +* 本项目依赖不向下传递 +* 开通请参考[阿里云-消息服务-MNS](https://help.aliyun.com/product/27412.html?spm=5176.7944397.207973.oss4.4d1ab2418oQPP0) + +> 引入依赖 pom.xml + +```mxml + + + + com.aliyun + aliyun-java-sdk-core + + + + com.aliyun.mns + aliyun-sdk-mns + jar-with-dependencies + + +``` +> 配置 application.properties +``` +#mns +yexuejc.alibaba.mns.access-key-id=阿里MNS提供的AccessKey +yexuejc.alibaba.mns.access-key-secret=阿里MNS提供的的SecretKey +yexuejc.alibaba.mns.endpoint=MNS接入endpoint,公网,私网,VPC各不相同,需要分环境配置 +yexuejc.alibaba.mns.queue-name-prefix=队列名称前缀 +``` + +> 使用example + +参考[MnsTest.java](../yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/MnsTest.java) diff --git a/doc/OSS.md b/doc/OSS.md new file mode 100644 index 0000000..7e9719b --- /dev/null +++ b/doc/OSS.md @@ -0,0 +1,36 @@ +aliyun OSS 对象存储 使用指南 +------------- +* 本项目依赖不向下传递 +* 开通请参考[阿里云对象存储-OSS](https://help.aliyun.com/document_detail/32008.html?spm=5176.87240.400427.45.1dd74614DiKpxR) + +_本工程暂提供上传功能_ + +> 引入依赖 pom.xml + +```mxml + + + + com.aliyun + aliyun-java-sdk-core + + + + com.aliyun.oss + aliyun-sdk-oss + + +``` +> 配置 +``` application.properties +#OSS +yexuejc.alibaba.oss.endpoint=阿里OSS提供的endpoint +yexuejc.alibaba.oss.access-key-secret=阿里OSS提供的SecretKey +yexuejc.alibaba.oss.access-key-id=阿里OSS提供的AccessKey +yexuejc.alibaba.oss.bucket=阿里OSS提供的bucket + +``` + +> 使用example + +参考[OssTest.java](../yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/OssTest.java) diff --git a/PARAMS_RSA_DECRYPT_ENCRYPT.md b/doc/PARAMS_RSA_DECRYPT_ENCRYPT.md similarity index 100% rename from PARAMS_RSA_DECRYPT_ENCRYPT.md rename to doc/PARAMS_RSA_DECRYPT_ENCRYPT.md diff --git a/doc/REDIS.md b/doc/REDIS.md new file mode 100644 index 0000000..31ecaa5 --- /dev/null +++ b/doc/REDIS.md @@ -0,0 +1,30 @@ +redis 使用指南 +------------- +* 本项目依赖不向下传递 + +> 引入依赖 pom.xml + +```mxml + + + + org.springframework.data + spring-data-redis + + + redis.clients + jedis + + +``` +> 配置 application.properties +``` +#reids +spring.redis.host=你的reids地址 +spring.redis.password=密码 +spring.redis.port=端口 +``` + +> 使用example + +参考[RedisTest.java](../yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/RedisTest.java) diff --git a/RSA.md b/doc/RSA.md similarity index 100% rename from RSA.md rename to doc/RSA.md diff --git a/pom.xml b/pom.xml index 6d0bcad..038160e 100644 --- a/pom.xml +++ b/pom.xml @@ -38,11 +38,11 @@ 1.2.49 1.11 - 4.0.8 + 4.1.1 3.0.0 3.1.0 1.1.0 - 1.1.8.6 + 1.1.8 3.3.49.ALL @@ -180,14 +180,12 @@ ${aliyun-sdk-mns.version} jar-with-dependencies - com.esotericsoftware.reflectasm reflectasm ${reflectasm.version} - com.alibaba.boot dubbo-spring-boot-starter diff --git a/yexuejc-springboot-base/pom.xml b/yexuejc-springboot-base/pom.xml index 31a8912..c853ec7 100644 --- a/yexuejc-springboot-base/pom.xml +++ b/yexuejc-springboot-base/pom.xml @@ -79,6 +79,13 @@ jedis true + + + com.aliyun.mns + aliyun-sdk-mns + jar-with-dependencies + true + org.springframework.boot spring-boot-starter-test diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsAutoConfiguration.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsAutoConfiguration.java new file mode 100644 index 0000000..71bd8de --- /dev/null +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsAutoConfiguration.java @@ -0,0 +1,37 @@ +package com.yexuejc.springboot.base.autoconfigure; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.aliyun.mns.client.MNSClient; + +/** + * 阿里云消息服务MNS相关配置 + * + * @author maxf + * @version 1.0 + * @ClassName MnsAutoConfiguration + * @Description + * @date 2018/11/1 10:04 + */ +@Configuration +@ConditionalOnClass(MNSClient.class) +@EnableConfigurationProperties(MnsProperties.class) +public class MnsAutoConfiguration { + private final MnsProperties properties; + + public MnsAutoConfiguration(MnsProperties properties) { + super(); + this.properties = properties; + } + + @Bean + @ConditionalOnMissingBean + public MnsFacade mnsFacade() { + return new MnsFacade(properties); + } + +} \ No newline at end of file diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsFacade.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsFacade.java new file mode 100644 index 0000000..d500d44 --- /dev/null +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsFacade.java @@ -0,0 +1,168 @@ + +package com.yexuejc.springboot.base.autoconfigure; + +import com.aliyun.mns.client.CloudAccount; +import com.aliyun.mns.client.CloudTopic; +import com.aliyun.mns.client.MNSClient; +import com.aliyun.mns.model.*; +import com.yexuejc.springboot.base.util.LogUtil; + +/** + * 阿里云消息服务MNS操作类 + * + * @author maxf + * @version 1.0 + * @ClassName MnsFacade + * @Description + * @date 2018/11/1 10:21 + */ +public class MnsFacade { + /** + * 默认长轮询等待秒数 + */ + private static int DEFAULT_WAIT_SECONDS = 15; + + /** + * 项目中需要用到的所有队列 + */ + private MNSClient client; + /** + * MNS相关配置 + */ + private MnsProperties properties; + + public MnsFacade(MnsProperties properties) { + CloudAccount account = new CloudAccount(properties.getAccessKeyId(), properties.getAccessKeySecret(), + properties.getEndpoint()); + this.client = account.getMNSClient(); + this.properties = properties; + } + + /** + * 发送消息 + * + * @param queueName 发送对象队列名 + * @param msg 发送的消息字符串,使用json格式 + * @param delaySeconds 发送成功的Message对象 + * @return + */ + public Message sendMsg(String queueName, String msg, int delaySeconds) { + Message message = new Message(); + message.setMessageBody(msg); + message.setDelaySeconds(delaySeconds); + LogUtil.bizLogger.debug("[alibaba-MNS]发送消息{}:{}", queueName, msg); + return client.getQueueRef(queueFullName(queueName)).putMessage(message); + } + + + /** + * 取出消息 + * + * @param queueName 接收对象对列名 + * @return 接收成功的Message对象 + */ + public Message popMsg(String queueName) { + return client.getQueueRef(queueFullName(queueName)).popMessage(DEFAULT_WAIT_SECONDS); + } + + /** + * 取出消息 长轮询 + * + * @param queueName 接收对象对列名 + * @param waitSeconds 接收成功的Message对象 + * @return + */ + public Message popMsg(String queueName, int waitSeconds) { + return client.getQueueRef(queueFullName(queueName)).popMessage(waitSeconds); + } + + + /** + * 删除消息 + * + * @param queueName 删除对象对列名 + * @param msg 需要删除的Message对象 + */ + public void deleteMsg(String queueName, Message msg) { + LogUtil.bizLogger.info("删除对象--:queueName{},msg{}", queueName, msg); + client.getQueueRef(queueFullName(queueName)).deleteMessage(msg.getReceiptHandle()); + + } + + /** + * 返回队列全称 + * + * @return + */ + public String queueFullName(String queueName) { + return properties.getQueueNamePrefix() + "-" + queueName; + } + + /** + * 创建主题 + */ + public CloudTopic crtTopic() { + TopicMeta topicMeta = new TopicMeta(); + topicMeta.setTopicName(properties.getTopicName()); + //是否启用日志 + topicMeta.setLoggingEnabled(true); + return client.createTopic(topicMeta); + } + + /** + * 删除主题 + */ + public void delTopic() { + getTopic().delete(); + } + + /** + * 获取主题 + * + * @return + */ + public CloudTopic getTopic() { + return client.getTopicRef(properties.getTopicName()); + } + + /** + * 创建订阅 + */ + public void subscribe(String subscriptionName, String queueName) { + CloudTopic topic = getTopic(); + SubscriptionMeta subMeta = new SubscriptionMeta(); + subMeta.setSubscriptionName(subscriptionName); + subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.SIMPLIFIED); + subMeta.setEndpoint(topic.generateQueueEndpoint(queueFullName(queueName))); + topic.subscribe(subMeta); + } + + /** + * 发送消息 + * + * @param message + * @return + */ + public TopicMessage publishMessage(String message) { + CloudTopic topic = getTopic(); + //可以使用TopicMessage结构,选择不进行Base64加密 + TopicMessage msg = new Base64TopicMessage(); + msg.setMessageBody(message); + msg = topic.publishMessage(msg); + return msg; + } + + + /** + * 取出消息 + * + * @param queueName + * @return + */ + public Message popTopicMessage(String queueName) { + QueueMeta queueMeta = new QueueMeta(); + queueMeta.setQueueName(queueFullName(queueName)); + return client.createQueue(queueMeta).popMessage(DEFAULT_WAIT_SECONDS); + } + +} diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsProperties.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsProperties.java new file mode 100644 index 0000000..47218f2 --- /dev/null +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/MnsProperties.java @@ -0,0 +1,74 @@ +package com.yexuejc.springboot.base.autoconfigure; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * 阿里云消息服务MNS相关配置 + * + * @author maxf + * @version 1.0 + * @ClassName MnsProperties + * @Description + * @date 2018/11/1 10:27 + */ +@ConfigurationProperties(prefix = "yexuejc.alibaba.mns") +public class MnsProperties { + /** + * 在RAM创建的AccessKey + */ + private String accessKeyId="xxxxxxxxxx"; + /** + * 在RAM创建的SecretKey + */ + private String accessKeySecret="xxxxxxxxx"; + /** + * MNS接入endpoint,公网,私网,VPC各不相同,需要分环境配置 + */ + private String endpoint="http://00000000.mns.cn-hangzhou.aliyuncs.com"; + + /** + * 队列名称前缀(消费端为CONSUMER,测试环境为IVT-CONSUMER) + */ + private String queueNamePrefix; + private String topicName = "IVT"; + + public String getAccessKeyId() { + return accessKeyId; + } + + public void setAccessKeyId(String accessKeyId) { + this.accessKeyId = accessKeyId; + } + + public String getAccessKeySecret() { + return accessKeySecret; + } + + public void setAccessKeySecret(String accessKeySecret) { + this.accessKeySecret = accessKeySecret; + } + + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + public String getQueueNamePrefix() { + return queueNamePrefix; + } + + public void setQueueNamePrefix(String queueNamePrefix) { + this.queueNamePrefix = queueNamePrefix; + } + + public String getTopicName() { + return topicName; + } + + public void setTopicName(String topicName) { + this.topicName = topicName; + } +} diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssAutoConfiguration.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssAutoConfiguration.java index 8cb0135..e323204 100644 --- a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssAutoConfiguration.java +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssAutoConfiguration.java @@ -9,10 +9,16 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** - * OSS相关配置 + * 阿里OSS相关配置 + * + * @author maxf + * @version 1.0 + * @ClassName OssAutoConfiguration + * @Description + * @date 2018/11/1 10:30 */ @Configuration -@ConditionalOnClass({ OSSClient.class }) +@ConditionalOnClass({OSSClient.class}) @EnableConfigurationProperties(OssProperties.class) public class OssAutoConfiguration { diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssFacade.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssFacade.java index da79d72..df15828 100644 --- a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssFacade.java +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssFacade.java @@ -9,6 +9,12 @@ import java.io.InputStream; /** * 阿里云OSS服务MNS操作类 + * + * @author maxf + * @version 1.0 + * @ClassName OssFacade + * @Description + * @date 2018/11/1 10:30 */ public class OssFacade { diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssProperties.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssProperties.java index fee21b5..82f5cb4 100644 --- a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssProperties.java +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/OssProperties.java @@ -2,17 +2,34 @@ package com.yexuejc.springboot.base.autoconfigure; import org.springframework.boot.context.properties.ConfigurationProperties; -@ConfigurationProperties(prefix = "yexuejc.oss") +/** + * 阿里OSS相关配置 + * + * @author maxf + * @version 1.0 + * @ClassName OssProperties + * @Description + * @date 2018/11/1 10:30 + */ +@ConfigurationProperties(prefix = "yexuejc.alibaba.oss") public class OssProperties { - /** Endpoint 默认内网,外网需要自行配置:http://oss-cn-hangzhou.aliyuncs.com */ - private String endpoint = "oss-cn-hangzhou-internal.aliyuncs.com"; - /** 产品域名(固定) */ - private String accessKeyID = "LTAInCYwtsprAu8g"; - /** 区域ID(固定) */ - private String accessKeySecret = "6aqMtyFuJPUPChYpZSLsQ11cg4qby7"; - /** 默认bucket */ - private String bucket = "ecentm-res"; + /** + * Endpoint 默认内网,外网需要自行配置:http://oss-cn-hangzhou.aliyuncs.com + */ + private String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; + /** + * 产品域名(固定) + */ + private String accessKeyId = "xxxxx"; + /** + * 区域ID(固定) + */ + private String accessKeySecret = "xxxxx"; + /** + * 默认bucket + */ + private String bucket = "xxxxx"; public String getEndpoint() { return endpoint; @@ -22,12 +39,12 @@ public class OssProperties { this.endpoint = endpoint; } - public String getAccessKeyID() { - return accessKeyID; + public String getAccessKeyId() { + return accessKeyId; } - public void setAccessKeyID(String accessKeyID) { - this.accessKeyID = accessKeyID; + public void setAccessKeyId(String accessKeyId) { + this.accessKeyId = accessKeyId; } public String getAccessKeySecret() { diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/exception/ImageException.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/exception/ImageException.java new file mode 100644 index 0000000..35c478b --- /dev/null +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/exception/ImageException.java @@ -0,0 +1,24 @@ + +package com.yexuejc.springboot.base.exception; + +public class ImageException extends RuntimeException { + private static final long serialVersionUID = -2390195902982826130L; + private String code = "E"; + + public ImageException() { + super("图片异常"); + } + + public ImageException(String message) { + super(message); + } + + public ImageException(String msg, Throwable t) { + super(msg, t); + } + + public ImageException(String code, String msg, Throwable t) { + super(msg, t); + this.code = code; + } +} diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/util/LogUtil.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/util/LogUtil.java index b9f747b..8152160 100644 --- a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/util/LogUtil.java +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/util/LogUtil.java @@ -35,4 +35,5 @@ public class LogUtil { } return msg.replaceAll(";", "_"); } + } diff --git a/yexuejc-springboot-base/src/main/resources/META-INF/spring.factories b/yexuejc-springboot-base/src/main/resources/META-INF/spring.factories index 0a80197..fef1b9a 100644 --- a/yexuejc-springboot-base/src/main/resources/META-INF/spring.factories +++ b/yexuejc-springboot-base/src/main/resources/META-INF/spring.factories @@ -3,5 +3,6 @@ com.yexuejc.springboot.base.autoconfigure.WebAutoConfiguration,\ com.yexuejc.springboot.base.autoconfigure.OssAutoConfiguration,\ com.yexuejc.springboot.base.autoconfigure.MutiRedisAutoConfiguration,\ com.yexuejc.springboot.base.filter.ParamsRequestBodyAdvice,\ -com.yexuejc.springboot.base.filter.ParamsResponseBodyAdvice +com.yexuejc.springboot.base.filter.ParamsResponseBodyAdvice,\ +com.yexuejc.springboot.base.autoconfigure.MnsAutoConfiguration diff --git a/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/MnsTest.java b/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/MnsTest.java new file mode 100644 index 0000000..9df5d21 --- /dev/null +++ b/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/MnsTest.java @@ -0,0 +1,58 @@ +package com.yexuejc.springboot.base.test; + +import com.aliyun.mns.model.Message; +import com.yexuejc.base.util.JsonUtil; +import com.yexuejc.base.util.StrUtil; +import com.yexuejc.springboot.base.autoconfigure.MnsFacade; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author maxf + * @version 1.0 + * @ClassName MnsTest + * @Description + * @date 2018/11/1 14:25 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class MnsTest { + + @Autowired + MnsFacade mnsFacade; + + /** + * 发送MNS + */ + @Test + public void sendMms() { + Map map = new HashMap<>(); + map.put("id", StrUtil.genUUID()); + map.put("type", "shop"); + mnsFacade.sendMsg("SHOP", JsonUtil.obj2Json(map), 0); + } + + /** + * 取出消息->删除消息 + */ + public void get() { + Message msg = mnsFacade.popMsg("SHOP"); + if (msg == null) { + System.out.println("取出空消息"); + } + if (msg.getMessageBody() == null) { + System.out.println("取出消息无内容"); + //删除 + mnsFacade.deleteMsg("SHOP", msg); + } + Map map = JsonUtil.json2Obj(msg.getMessageBody(), Map.class); + System.out.println(String.format("取出消息:%s", JsonUtil.obj2Json(map))); + //操作完成后删除消息 + } +} diff --git a/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/OssTest.java b/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/OssTest.java new file mode 100644 index 0000000..b1250b4 --- /dev/null +++ b/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/OssTest.java @@ -0,0 +1,74 @@ +package com.yexuejc.springboot.base.test; + +import com.aliyun.oss.model.PutObjectResult; +import com.yexuejc.base.util.ImgUtil; +import com.yexuejc.base.util.StrUtil; +import com.yexuejc.springboot.base.autoconfigure.OssFacade; +import com.yexuejc.springboot.base.exception.ImageException; +import com.yexuejc.springboot.base.util.LogUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * @author maxf + * @version 1.0 + * @ClassName OssTest + * @Description + * @date 2018/11/1 14:58 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class OssTest { + @Autowired + OssFacade ossFacade; + + /** + * 上传图片到oss + *

+ * 这里是模拟微信登录时,获取到微信头像地址,缓存到base64,然后上传到OSS + *

+ */ + @Test + public void put(){ + putOss4Head(null, "https://avatar.csdn.net/7/8/1/3_wulex.jpg"); + } + + /** + * 上传网络头像至OSS + * + * @param url 网络图片地址 + * @return String 本OSS地址 + */ + public String putOss4Head(String name, String url) throws ImageException { + if (StrUtil.isEmpty(name)) { + name = StrUtil.genUUID(); + } + if (name.indexOf(".") == 0) { + throw new ImageException("图片名称第一个字符不能为."); + } + name = "head/" + name; + try { + LogUtil.bizLogger.info("[第三方登录/注册]上传头像开始:{}", url); + InputStream is = ImgUtil.getImageInputStreamFromUrl(url); + byte[] b = ImgUtil.getByteArray(is); + ImgUtil.ImageInfo imageInfo = ImgUtil.getImageInfoFromInputStream(b); + if (name.indexOf(".") < 0) { + name = name + "." + imageInfo.getType(); + } + PutObjectResult head = ossFacade.putObject(name, new ByteArrayInputStream(b)); + } catch (IOException e) { + LogUtil.bizLogger.error("[第三方登录/注册]读取网络头像为IO异常:{}", url); + e.printStackTrace(); + throw new ImageException("[第三方登录/注册]读取网络头像为IO异常:" + e.getMessage()); + } + LogUtil.bizLogger.info("[[第三方登录/注册]上传头像结束:{}", url); + return name; + } +} diff --git a/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/RedisTest.java b/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/RedisTest.java index 745cb38..2d4ada4 100644 --- a/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/RedisTest.java +++ b/yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/test/RedisTest.java @@ -1,7 +1,7 @@ package com.yexuejc.springboot.base.test; import com.yexuejc.base.util.JsonUtil; -import com.yexuejc.springboot.base.ApplicationRun; +import com.yexuejc.base.util.StrUtil; import com.yexuejc.springboot.base.autoconfigure.MutiRedisAutoConfiguration; import org.junit.Test; import org.junit.runner.RunWith; @@ -9,53 +9,100 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; - /** + * redis 操作 + * * @author maxf - * @PackageName com.yexuejc.springboot.base.test + * @version 1.0 + * @ClassName RedisTest * @Description - * @date 2018/9/26 16:28 + * @date 2018/11/1 11:16 */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = ApplicationRun.class) +@RunWith(SpringRunner.class) +@SpringBootTest public class RedisTest { + /** + * 指向redis 0库 + */ @Autowired @Qualifier(MutiRedisAutoConfiguration.BEAN_REDIS_TEMPLATE0) - RedisTemplate redisTemplate; + private RedisTemplate redisTemplate0; + /** + * 保存用户登录信息 + * name:张三 + */ @Test - public void insert() { - Map map = new HashMap<>(); - map.put("a", "b"); - map.put("c", 1); - map.put("aac", true); - map.put("q", "asdb"); - redisTemplate.opsForHash().putAll("test.redis", map); - redisTemplate.expire("test.redis", 10, TimeUnit.MINUTES); + public void save() { + redisTemplate0.afterPropertiesSet(); + redisTemplate0.opsForHash().put("login.user.userId1", "name", "张三"); + redisTemplate0.expire("login.user.userId1", 10, TimeUnit.MINUTES); } + /** + * 保存用户登录信息 + * name:张三 + * sex:男 + */ + @Test + public void save2() { + redisTemplate0.afterPropertiesSet(); + Map map = new HashMap<>(); + map.put("name", "张三"); + map.put("sex", "男"); + redisTemplate0.opsForHash().putAll("login.user.userId1", map); + redisTemplate0.expire("login.user.userId1", 10, TimeUnit.MINUTES); + } + + /** + * 保存授权zhangsan的token,过期时间10分钟 + */ + @Test + public void accessToken() { + redisTemplate0.afterPropertiesSet(); + redisTemplate0.opsForValue().append("zhangsan.access_token", StrUtil.genUUID()); + redisTemplate0.expire("zhangsan.access_token", 10, TimeUnit.MINUTES); + } + + + /** + * 获取用户登录信息 + * name:张三 + */ @Test public void get() { - Map map = redisTemplate.opsForHash().entries("test.redis"); - System.out.println(JsonUtil.obj2Json(map)); + redisTemplate0.afterPropertiesSet(); + String name = (String) redisTemplate0.opsForHash().get("login.user.userId1", "name"); + System.out.println(name); } + /** + * 修改用户登录信息 + * name:张三->李四 + * age:20 + */ @Test - public void update() { - Map map = redisTemplate.opsForHash().entries("test.redis"); - redisTemplate.opsForHash().put("test.redis", "q", "123456798"); + public void put() { + redisTemplate0.afterPropertiesSet(); + redisTemplate0.opsForHash().put("login.user.userId1", "name", "张三"); + redisTemplate0.opsForHash().put("login.user.userId1", "age", "20"); + Map map = (Map) redisTemplate0.opsForHash().entries("login.user.userId1"); System.out.println(JsonUtil.obj2Json(map)); } + /** + * 删除 + */ @Test public void del() { - Boolean delete = redisTemplate.delete("test.redis"); - System.out.println(delete); + redisTemplate0.afterPropertiesSet(); + redisTemplate0.delete("login.user.userId1"); } + } diff --git a/yexuejc-springboot-base/src/test/resources/application.properties b/yexuejc-springboot-base/src/test/resources/application.properties index 308233e..4d95402 100644 --- a/yexuejc-springboot-base/src/test/resources/application.properties +++ b/yexuejc-springboot-base/src/test/resources/application.properties @@ -3,7 +3,8 @@ spring.application.name=@pom.artifactId@ #log logging.level.root=info -logging.path=/logs/yexuejc-springboot-base +logging.path=/logs/yexuejc-springboot-parent + yexuejc.http.filter.type=0 @@ -28,8 +29,23 @@ server.tomcat.uri-encoding=UTF-8 #是否开启HTTPS(SSL)请求证书验证忽略:默认false yexuejc.enable.ssl-ignore=true +#reids +#开启指定redis库db0默认开启 yexuejc.redis.db1=true yexuejc.redis.db2=true spring.redis.host=121.42.165.89 spring.redis.password= spring.redis.port=16379 + + +#mns +yexuejc.alibaba.mns.access-key-id= +yexuejc.alibaba.mns.access-key-secret= +yexuejc.alibaba.mns.endpoint=http://1734411100610331.mns.cn-hangzhou.aliyuncs.com/ +yexuejc.alibaba.mns.queue-name-prefix=IVT-CONSUMER + +#OSS +yexuejc.alibaba.oss.endpoint=oss-cn-beijing.aliyuncs.com +yexuejc.alibaba.oss.access-key-secret= +yexuejc.alibaba.oss.access-key-id= +yexuejc.alibaba.oss.bucket=guansichou