Skip to content

Commit

Permalink
perf($AuthCenter): customize RedisTemplate
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
Johnny Miller (锺俊) committed Dec 30, 2020
1 parent 40bac8b commit 22c9cfb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* In order to replace that, we have to <b>customize</b> 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<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
log.warn("Initial bean: {}", RedisTemplate.class.getSimpleName());

RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);

Jackson2JsonRedisSerializer<Object> 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<Object, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) {
log.warn("Initial bean: {}", ReactiveRedisTemplate.class.getSimpleName());
StringRedisSerializer keySerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext
.newSerializationContext(keySerializer)
.value(valueSerializer)
.hashValue(valueSerializer)
.build();
return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
}

0 comments on commit 22c9cfb

Please sign in to comment.