Commit 7922024d8b174d8c57b9edfd0a031e722f366208

Authored by ci-aliyun
1 parent a476e3ab

#chore:升级版本为2.0.0

1、不再兼容1.x版本;
2、升级spring版本为2.0.5;
... ... @@ -6,20 +6,20 @@
6 6  
7 7 <groupId>com.irrigation</groupId>
8 8 <artifactId>common-lib</artifactId>
9   - <version>1.2.4</version>
  9 + <version>2.0.0</version>
10 10  
11 11 <properties>
12 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13 13 <java.version>1.8</java.version>
14   - <spring-boot.version>1.5.10.RELEASE</spring-boot.version>
  14 + <spring-boot.version>2.0.5.RELEASE</spring-boot.version>
15 15 <swagger.version>2.7.0</swagger.version>
16 16 <lombok.version>1.16.20</lombok.version>
17 17 <commons-codec.version>1.11</commons-codec.version>
18 18 <guava.vsersion>20.0</guava.vsersion>
19 19 <slf4j.version>1.7.24</slf4j.version>
20 20 <fastjson.version>1.2.49</fastjson.version>
21   - <spring-web.version>4.3.14.RELEASE</spring-web.version>
22   - <spring-security-core.version>4.2.4.RELEASE</spring-security-core.version>
  21 + <spring-web.version>5.0.9.RELEASE</spring-web.version>
  22 + <spring-security-core.version>5.0.9.RELEASE</spring-security-core.version>
23 23 <ehcache.version>2.6.11</ehcache.version>
24 24  
25 25 </properties>
... ...
src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java
... ... @@ -9,6 +9,8 @@ import org.springframework.cache.CacheManager;
9 9 import org.springframework.context.annotation.Bean;
10 10 import org.springframework.context.annotation.Conditional;
11 11 import org.springframework.context.annotation.Configuration;
  12 +import org.springframework.data.redis.cache.RedisCacheConfiguration;
  13 +import org.springframework.data.redis.cache.RedisCacheWriter;
12 14 import org.springframework.data.redis.connection.Message;
13 15 import org.springframework.data.redis.connection.MessageListener;
14 16 import org.springframework.data.redis.connection.RedisConnectionFactory;
... ... @@ -17,6 +19,8 @@ import org.springframework.data.redis.listener.PatternTopic;
17 19 import org.springframework.data.redis.listener.RedisMessageListenerContainer;
18 20 import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
19 21  
  22 +import java.time.Duration;
  23 +
20 24 /**
21 25 * @Author: boni
22 26 * @Date: 2018/8/24-下午2:37
... ... @@ -29,11 +33,19 @@ public class LayeringCacheAutoFactory {
29 33 @Bean
30 34 public LayeringCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate,
31 35 LayeringCacheProperties layeringCacheProperties) {
32   - LayeringCacheManager cacheManager = new LayeringCacheManager(redisTemplate, layeringCacheProperties);
33   - cacheManager.setUsePrefix(true);
  36 +
  37 + RedisCacheWriter redisCacheWriter = RedisCacheWriter
  38 + .nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
34 39 long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 120L;
35   - cacheManager.setExpires(layeringCacheProperties.getExpires());
36   - cacheManager.setDefaultExpiration(expire);
  40 + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
  41 + .defaultCacheConfig()
  42 + .entryTtl(Duration.ofSeconds(expire));
  43 + LayeringCacheManager cacheManager =
  44 + new LayeringCacheManager(
  45 + redisTemplate,
  46 + redisCacheWriter,
  47 + redisCacheConfiguration,
  48 + layeringCacheProperties);
37 49 return cacheManager;
38 50 }
39 51  
... ...
src/main/java/com/irrigation/icl/cache/core/LayeringCache.java
... ... @@ -14,7 +14,8 @@ import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
14 14 import org.springframework.cache.Cache;
15 15 import org.springframework.cache.support.SimpleValueWrapper;
16 16 import org.springframework.data.redis.cache.RedisCache;
17   -import org.springframework.data.redis.core.RedisOperations;
  17 +import org.springframework.data.redis.cache.RedisCacheConfiguration;
  18 +import org.springframework.data.redis.cache.RedisCacheWriter;
18 19 import org.springframework.util.StopWatch;
19 20  
20 21 import java.text.SimpleDateFormat;
... ... @@ -36,17 +37,14 @@ public class LayeringCache extends RedisCache {
36 37 private CacheManager layeringL1CacheManager;
37 38 private boolean layerL1Enable;
38 39 private String ID;
39   -// private String HOST;
40   -// private String CACHE_STORE;
41   -// private String CACHE_STORE_SYNC;
42 40  
43   -
44   - public LayeringCache(LayeringCacheManager layeringCacheManager, String name, byte[] prefix,
45   - RedisOperations<? extends Object, ? extends Object> redisOperations, long expiration) {
46   - super(name, prefix, redisOperations, expiration);
  41 + public LayeringCache(LayeringCacheManager layeringCacheManager, String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) {
  42 + super(name, cacheWriter, cacheConfig);
47 43 this.layeringCacheManager = layeringCacheManager;
  44 +
48 45 this.layeringCacheProperties = layeringCacheManager.getLayeringCacheProperties();
49 46 this.layerL1Enable = layeringCacheProperties.getCacheL1().isEnable();
  47 +
50 48 initEhcache();
51 49 }
52 50  
... ... @@ -54,6 +52,7 @@ public class LayeringCache extends RedisCache {
54 52 public Cache.ValueWrapper get(Object key) {
55 53 StopWatch stopWatch = new StopWatch();
56 54 stopWatch.start();
  55 +
57 56 Ehcache ehCache = null;
58 57 if (layerL1Enable) {
59 58 ehCache = getL1Cache(super.getName());
... ... @@ -157,7 +156,7 @@ public class LayeringCache extends RedisCache {
157 156 Callable<Ehcache> callable = () -> {
158 157 Ehcache cache = layeringL1CacheManager.getCache(name);
159 158 if (cache == null) {
160   - CacheConfiguration cacheConfiguration = getCacheConfiguration();
  159 + CacheConfiguration cacheConfiguration = getEncacheCacheConfiguration();
161 160 cacheConfiguration.setName(name);
162 161 Ehcache ehcache = new net.sf.ehcache.Cache(cacheConfiguration);
163 162 layeringL1CacheManager.addCache(ehcache);
... ... @@ -188,7 +187,7 @@ public class LayeringCache extends RedisCache {
188 187 return simpleDateFormat.format(new Date());
189 188 }
190 189  
191   - private CacheConfiguration getCacheConfiguration() {
  190 + private CacheConfiguration getEncacheCacheConfiguration() {
192 191 // Cache
193 192 CacheConfiguration cacheConfiguration = new CacheConfiguration();
194 193 cacheConfiguration.setEternal(false);
... ... @@ -205,12 +204,6 @@ public class LayeringCache extends RedisCache {
205 204 cacheConfiguration.copyOnRead(true);
206 205 cacheConfiguration.copyOnWrite(true);
207 206  
208   -// CopyStrategyConfiguration copyStrategyConfiguration = new CopyStrategyConfiguration();
209   -// copyStrategyConfiguration.setClass(MyCopyStrategy.class.getName());
210   -// copyStrategyConfiguration.setCopyStrategyInstance(new MyCopyStrategy());
211   -
212   -// cacheConfiguration.addCopyStrategy(copyStrategyConfiguration);
213   -
214 207 cacheConfiguration.setTimeToIdleSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToIdleSeconds());
215 208 cacheConfiguration.setTimeToLiveSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToLiveSeconds());
216 209  
... ... @@ -220,13 +213,6 @@ public class LayeringCache extends RedisCache {
220 213 private void initEhcache() {
221 214 if (layerL1Enable) {
222 215 this.ID = layeringCacheProperties.getCacheL1().getKey() + "." + getDate();
223   -// this.HOST = Utils.getLocalHostIP();
224   -// this.CACHE_STORE = layeringCacheProperties.getCacheL1().getKey()
225   -// + layeringCacheProperties.getCacheL1().getSeparator()
226   -// + "cache"
227   -// + layeringCacheProperties.getCacheL1().getSeparator()
228   -// + "store";
229   -// this.CACHE_STORE_SYNC = this.CACHE_STORE + layeringCacheProperties.getCacheL1().getSeparator() + "sync";
230 216  
231 217 Configuration configuration = new Configuration();
232 218 configuration.setName(this.ID);
... ... @@ -238,7 +224,7 @@ public class LayeringCache extends RedisCache {
238 224 dsc.setPath(layeringCacheProperties.getCacheL1().getLocalStoreLocation() + this.ID);
239 225 configuration.diskStore(dsc);
240 226  
241   - CacheConfiguration cacheConfiguration = getCacheConfiguration();
  227 + CacheConfiguration cacheConfiguration = getEncacheCacheConfiguration();
242 228  
243 229 configuration.setDefaultCacheConfiguration(cacheConfiguration);
244 230 configuration.setDynamicConfig(false);
... ... @@ -248,25 +234,4 @@ public class LayeringCache extends RedisCache {
248 234 layeringL1CacheManager = new CacheManager(configuration);
249 235 }
250 236 }
251   -//
252   -// class MyCopyStrategy implements ReadWriteCopyStrategy<Element> {
253   -// @Override
254   -// public Element copyForWrite(Element value) {
255   -// if (value != null) {
256   -// Object temp = (Serializable) value.getObjectValue();
257   -// return new Element(value.getObjectKey(), temp);
258   -// }
259   -// return value;
260   -// }
261   -//
262   -// @Override
263   -// public Element copyForRead(Element storedValue) {
264   -// if (storedValue != null) {
265   -// Object temp = (Serializable) storedValue.getObjectValue();
266   -// return new Element(storedValue.getObjectKey(), temp);
267   -// }
268   -// return storedValue;
269   -//
270   -// }
271   -// }
272 237 }
... ...
src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java
1 1 package com.irrigation.icl.cache.core;
2 2  
3   -import org.springframework.boot.bind.RelaxedPropertyResolver;
  3 +import org.springframework.boot.context.properties.bind.Binder;
4 4 import org.springframework.context.annotation.Condition;
5 5 import org.springframework.context.annotation.ConditionContext;
6 6 import org.springframework.core.type.AnnotatedTypeMetadata;
... ... @@ -12,13 +12,10 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
12 12 public class LayeringCacheLoadCondition implements Condition {
13 13 @Override
14 14 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
15   - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
16   - context.getEnvironment(), "springext.cache.");
17   -
18   - Boolean enable = resolver.getProperty("enable",Boolean.class);
19   - if(enable == null){
20   - return false;
21   - }
  15 + Boolean enable = Binder
  16 + .get(context.getEnvironment())
  17 + .bind("springext.cache.enable", Boolean.class)
  18 + .orElse(false);
22 19 return enable.booleanValue();
23 20  
24 21 }
... ...
src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java
1 1 package com.irrigation.icl.cache.core;
2 2  
3 3 import com.irrigation.icl.cache.Command;
  4 +import lombok.Getter;
4 5 import org.springframework.data.redis.cache.RedisCache;
  6 +import org.springframework.data.redis.cache.RedisCacheConfiguration;
5 7 import org.springframework.data.redis.cache.RedisCacheManager;
6   -import org.springframework.data.redis.core.RedisOperations;
  8 +import org.springframework.data.redis.cache.RedisCacheWriter;
  9 +import org.springframework.data.redis.core.RedisTemplate;
7 10  
8 11 /**
9 12 * @Author: boni
10 13 * @Date: 2018/8/24-下午2:29
11 14 */
  15 +@Getter
12 16 public class LayeringCacheManager extends RedisCacheManager {
13 17  
  18 + final RedisCacheWriter cacheWriter;
  19 + final RedisCacheConfiguration redisCacheConfiguration;
14 20 private LayeringCacheProperties layeringCacheProperties;
15   -
16   - public LayeringCacheProperties getLayeringCacheProperties() {
17   - return layeringCacheProperties;
18   - }
19   -
20   - public void setLayeringCacheProperties(LayeringCacheProperties layeringCacheProperties) {
  21 + private RedisTemplate redisTemplate;
  22 +
  23 + public LayeringCacheManager(RedisTemplate redisTemplate, RedisCacheWriter cacheWriter, RedisCacheConfiguration redisCacheConfiguration,
  24 + LayeringCacheProperties layeringCacheProperties) {
  25 + super(cacheWriter, redisCacheConfiguration);
  26 + this.cacheWriter = cacheWriter;
  27 + this.redisCacheConfiguration = redisCacheConfiguration;
  28 + this.redisTemplate = redisTemplate;
21 29 this.layeringCacheProperties = layeringCacheProperties;
22 30 }
23 31  
24   - public LayeringCacheManager(RedisOperations redisOperations, LayeringCacheProperties layeringCacheProperties) {
25   - super(redisOperations);
26   - this.layeringCacheProperties = layeringCacheProperties;
27   - }
28   -
29   - @SuppressWarnings("unchecked")
30 32 @Override
31   - protected RedisCache createCache(String cacheName) {
32   - long expiration = computeExpiration(cacheName);
33   - return new LayeringCache(
34   - this,
35   - cacheName,
36   - (this.isUsePrefix() ? this.getCachePrefix().prefix(cacheName) : null),
37   - this.getRedisOperations(),
38   - expiration);
  33 + protected RedisCache getMissingCache(String name) {
  34 + return new LayeringCache(this, name, this.cacheWriter, this.redisCacheConfiguration);
39 35 }
40 36  
41   -
42 37 //notify other redis clent to update L1
43 38 public void publishMessage(Command command) {
44   - this.getRedisOperations().convertAndSend(this.getLayeringCacheProperties().getCacheL1().getTopic(), command);
  39 + this.getRedisTemplate().convertAndSend(this.getLayeringCacheProperties().getCacheL1().getTopic(), command);
45 40 }
46 41  
47 42  
... ...
src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java
... ... @@ -73,10 +73,10 @@ public class LayeringCacheProperties {
73 73 /**
74 74 * 本地缓存过期时间,单位秒,默认10分钟
75 75 */
76   - private int localTimeToLiveSeconds = 1 * 60;
77   - private int localTimeToIdleSeconds = 0;
  76 + private int localTimeToLiveSeconds = 10 * 60;
  77 + private int localTimeToIdleSeconds = 30;
78 78 /**
79   - * 本地缓存清理周期,单位秒,默认3分钟
  79 + * 本地缓存清理周期,单位秒,默认90秒
80 80 */
81 81 private int localDiskExpiryThreadIntervalSeconds = 90;
82 82 }
... ...
src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java
1 1 package com.irrigation.icl.utils;
2 2  
3   -import org.springframework.security.authentication.encoding.BasePasswordEncoder;
4   -import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
  3 +
  4 +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  5 +import org.springframework.security.crypto.password.PasswordEncoder;
5 6  
6 7 public class PasswordEncoderUtil {
7 8  
8   - private final static BasePasswordEncoder encoder = new Md5PasswordEncoder();
  9 + private final static PasswordEncoder encoder = new BCryptPasswordEncoder(4);
9 10  
10 11 public static String encodePassword(String password) {
11   - return encoder.encodePassword(password, null);
  12 + return encoder.encode(password);
12 13 }
13 14  
14 15 public static boolean isPasswordValid(String rawPassword, String encodedPassword) {
15   - return encoder.isPasswordValid(encodedPassword, rawPassword, null);
  16 + return encoder.matches(rawPassword, encodedPassword);
16 17 }
17 18  
18 19 }
... ...