From 22c9cfbfdd056b11832e5511acb354214f152d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Wed, 30 Dec 2020 13:51:16 +0800 Subject: [PATCH] perf($AuthCenter): customize RedisTemplate [skip ci] --- .../security/service/impl/JwtServiceImpl.java | 5 +- .../configuration/RedisConfiguration.java | 75 +++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 auth-center/src/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RedisConfiguration.java diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/security/service/impl/JwtServiceImpl.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/security/service/impl/JwtServiceImpl.java index db147a0a..384f5a99 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/security/service/impl/JwtServiceImpl.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/security/service/impl/JwtServiceImpl.java @@ -15,7 +15,6 @@ import lombok.extern.slf4j.Slf4j; import lombok.val; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.security.core.Authentication; @@ -56,8 +55,8 @@ private void init() { secretKey = Keys.hmacShaKeyFor(jwtConfiguration.getSigningKey().getBytes(StandardCharsets.UTF_8)); log.warn("Secret key for JWT was generated. Algorithm: {}", secretKey.getAlgorithm()); jwtParser = Jwts.parserBuilder().setSigningKey(secretKey).build(); - redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setValueSerializer(new StringRedisSerializer()); +// redisTemplate.setKeySerializer(new StringRedisSerializer()); +// redisTemplate.setValueSerializer(new StringRedisSerializer()); } @Override diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RedisConfiguration.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RedisConfiguration.java new file mode 100644 index 00000000..ce683840 --- /dev/null +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RedisConfiguration.java @@ -0,0 +1,75 @@ +package com.jmsoftware.maf.authcenter.universal.configuration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.ReactiveRedisTemplate; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * Description: RedisConfiguration, change description here. + * + * @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/30/2020 1:08 PM + **/ +@Slf4j +@Configuration +@RequiredArgsConstructor +public class RedisConfiguration { + private final ObjectMapper objectMapper; + + /** + * RedisTemplate uses JDK byte code serialization (byte[]), which is not that readable and friendly to + * human reading. + *

+ * In order to replace that, we have to customize RedisTemplate. + * + * @param redisConnectionFactory the redis connection factory + * @return RedisTemplate redis template + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/30/2020 1:18 PM + */ + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { + log.warn("Initial bean: {}", RedisTemplate.class.getSimpleName()); + + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = + new Jackson2JsonRedisSerializer<>(Object.class); + jackson2JsonRedisSerializer.setObjectMapper(objectMapper); + + // Set key serializers as StringRedisSerializer + redisTemplate.setKeySerializer(new StringRedisSerializer()); + // Use Jackson2JsonRedisSerialize to replace default JDK serialization + redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } + + /** + * Reactive redis template factory. + * + * @param connectionFactory the reactive redis connection factory + * @return the reactive redis template + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/30/2020 1:43 PM + */ + @Bean + ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) { + log.warn("Initial bean: {}", ReactiveRedisTemplate.class.getSimpleName()); + StringRedisSerializer keySerializer = new StringRedisSerializer(); + Jackson2JsonRedisSerializer valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class); + RedisSerializationContext serializationContext = RedisSerializationContext + .newSerializationContext(keySerializer) + .value(valueSerializer) + .hashValue(valueSerializer) + .build(); + return new ReactiveRedisTemplate<>(connectionFactory, serializationContext); + } +}