|
|
|
@ -10,120 +10,48 @@
|
|
|
|
|
|
|
|
|
|
package com.yami.shop.common.config;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.MapperFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
|
|
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
|
|
|
import org.springframework.cache.CacheManager;
|
|
|
|
|
import org.springframework.cache.annotation.EnableCaching;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
|
|
|
import org.springframework.data.redis.cache.RedisCacheManager;
|
|
|
|
|
import org.springframework.data.redis.cache.RedisCacheWriter;
|
|
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
|
|
|
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
|
|
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import com.yami.shop.common.bean.AliDaYu;
|
|
|
|
|
import com.yami.shop.common.bean.ImgUpload;
|
|
|
|
|
import com.yami.shop.common.bean.Qiniu;
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
|
|
import org.springframework.context.annotation.PropertySource;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* redis 缓存配置,仅当配置文件中spring.cache.type = redis时生效
|
|
|
|
|
* 该类作为商城配置文件相关的实体类,用于绑定配置文件中的各项配置信息到对应的属性上,方便在整个项目中获取和使用商城相关的配置参数,
|
|
|
|
|
* 通过使用 Spring 的相关注解(@Component、@ConfigurationProperties、@PropertySource),可以将配置文件中的配置值自动注入到这些属性中,
|
|
|
|
|
* 使得项目中的不同模块能够方便地获取如七牛云配置、阿里大鱼短信平台配置、加解密 token 的密钥以及本地文件上传配置等信息,实现配置的统一管理和灵活使用。
|
|
|
|
|
*
|
|
|
|
|
* @author lgh
|
|
|
|
|
*/
|
|
|
|
|
@EnableCaching
|
|
|
|
|
@Configuration
|
|
|
|
|
public class RedisCacheConfig {
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, RedisSerializer<Object> redisSerializer) {
|
|
|
|
|
|
|
|
|
|
RedisCacheManager redisCacheManager = new RedisCacheManager(
|
|
|
|
|
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
|
|
|
|
|
// 默认策略,未配置的 key 会使用这个
|
|
|
|
|
this.getRedisCacheConfigurationWithTtl(3600,redisSerializer),
|
|
|
|
|
// 指定 key 策略
|
|
|
|
|
this.getRedisCacheConfigurationMap(redisSerializer)
|
|
|
|
|
);
|
|
|
|
|
redisCacheManager.setTransactionAware(true);
|
|
|
|
|
return redisCacheManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap(RedisSerializer<Object> redisSerializer) {
|
|
|
|
|
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(16);
|
|
|
|
|
redisCacheConfigurationMap.put("product", this.getRedisCacheConfigurationWithTtl(1800, redisSerializer));
|
|
|
|
|
return redisCacheConfigurationMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds,RedisSerializer<Object> redisSerializer) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
|
|
|
|
|
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
|
|
|
|
|
RedisSerializationContext
|
|
|
|
|
.SerializationPair
|
|
|
|
|
.fromSerializer(redisSerializer)
|
|
|
|
|
).entryTtl(Duration.ofSeconds(seconds));
|
|
|
|
|
|
|
|
|
|
return redisCacheConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory,RedisSerializer<Object> redisSerializer) {
|
|
|
|
|
|
|
|
|
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
|
|
|
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
|
|
|
|
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
|
|
|
|
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
|
|
|
|
redisTemplate.setValueSerializer(redisSerializer);
|
|
|
|
|
redisTemplate.setHashValueSerializer(redisSerializer);
|
|
|
|
|
redisTemplate.setEnableTransactionSupport(false);
|
|
|
|
|
|
|
|
|
|
redisTemplate.afterPropertiesSet();
|
|
|
|
|
return redisTemplate;
|
|
|
|
|
}
|
|
|
|
|
@Data
|
|
|
|
|
@Component
|
|
|
|
|
@PropertySource("classpath:shop.properties")
|
|
|
|
|
@ConfigurationProperties(prefix = "shop")
|
|
|
|
|
public class ShopBasicConfig {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 自定义redis序列化的机制,重新定义一个ObjectMapper.防止和MVC的冲突
|
|
|
|
|
* https://juejin.im/post/5e869d426fb9a03c6148c97e
|
|
|
|
|
* 用于存储七牛云的配置信息的属性,七牛云可能用于文件存储、图片上传等功能,其配置信息包含诸如访问密钥、存储区域等内容,
|
|
|
|
|
* 通过此属性可以在项目中方便地获取七牛云相关配置,以便与七牛云服务进行集成,实现文件上传、管理等操作。
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
public RedisSerializer<Object> redisSerializer() {
|
|
|
|
|
ObjectMapper objectMapper = JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build();
|
|
|
|
|
// 反序列化时候遇到不匹配的属性并不抛出异常
|
|
|
|
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
// 序列化时候遇到空对象不抛出异常
|
|
|
|
|
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
|
|
|
|
// 反序列化的时候如果是无效子类型,不抛出异常
|
|
|
|
|
objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
|
|
|
|
|
// 不使用默认的dateTime进行序列化,
|
|
|
|
|
objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
|
|
|
|
|
// 使用JSR310提供的序列化类,里面包含了大量的JDK8时间序列化类
|
|
|
|
|
objectMapper.registerModule(new JavaTimeModule());
|
|
|
|
|
// 启用反序列化所需的类型信息,在属性中添加@class
|
|
|
|
|
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL,
|
|
|
|
|
JsonTypeInfo.As.PROPERTY);
|
|
|
|
|
// 配置null值的序列化器
|
|
|
|
|
GenericJackson2JsonRedisSerializer.registerNullValueSerializer(objectMapper, null);
|
|
|
|
|
return new GenericJackson2JsonRedisSerializer(objectMapper);
|
|
|
|
|
}
|
|
|
|
|
private Qiniu qiniu;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用于存储阿里大鱼短信平台相关配置信息的属性,阿里大鱼短信平台可用于向用户发送短信通知、验证码等功能,
|
|
|
|
|
* 其配置信息包含访问密钥 ID、访问密钥秘密、短信签名等内容,通过该属性能够获取相应配置,进而调用阿里大鱼的短信服务接口来发送短信。
|
|
|
|
|
*/
|
|
|
|
|
private AliDaYu aLiDaYu;
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory){
|
|
|
|
|
StringRedisTemplate redisTemplate = new StringRedisTemplate(redisConnectionFactory);
|
|
|
|
|
redisTemplate.setEnableTransactionSupport(false);
|
|
|
|
|
return redisTemplate;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 用于存储加解密 token 的密钥信息的属性,在涉及用户认证、授权等场景中,token 常用于传递用户身份信息,
|
|
|
|
|
* 为了保证 token 的安全性,可能会使用此密钥进行加密和解密操作,该属性保存的密钥值会在相关的加密解密逻辑中被使用。
|
|
|
|
|
*/
|
|
|
|
|
private String tokenAesKey;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用于存储本地文件上传配置信息的属性,本地文件上传配置可能涉及到文件存储路径、文件大小限制、允许的文件类型等相关参数,
|
|
|
|
|
* 通过这个属性可以获取相应配置,从而在进行本地文件上传功能实现时,按照配置要求来处理文件上传的各种逻辑,确保文件上传操作符合业务需求。
|
|
|
|
|
*/
|
|
|
|
|
private ImgUpload imgUpload;
|
|
|
|
|
}
|