SharedConfiguration.java
5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.diligrp.assistant.shared;
import com.diligrp.assistant.shared.exception.PlatformServiceException;
import com.diligrp.assistant.shared.security.RsaCipher;
import com.diligrp.assistant.shared.util.JsonUtils;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Configuration
@ComponentScan("com.diligrp.assistant.shared")
public class SharedConfiguration {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
// Jackson DataBinding所有配置
@Bean
@ConditionalOnClass(JavaTimeModule.class)
public Jackson2ObjectMapperBuilderCustomizer customizeJacksonConfig() {
return JsonUtils::initObjectMapperBuilder;
}
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
// 不能使用lambda表达式,否则导致springboot启动问题
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
try {
return StringUtils.hasText(source) ? LocalDateTime.parse(source, DateTimeFormatter.ofPattern(Constants.DATE_TIME_FORMAT)) : null;
} catch (Exception ex) {
throw new IllegalArgumentException(String.format("Error parse %s to LocalDateTime", source), ex);
}
}
};
}
@Bean
public Converter<String, LocalDate> localDateConverter() {
// 不能使用lambda表达式,否则导致springboot启动问题
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
try {
return StringUtils.hasText(source) ? LocalDate.parse(source, DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)) : null;
} catch (Exception ex) {
throw new IllegalArgumentException(String.format("Error parse %s to LocalDate", source), ex);
}
}
};
}
@Bean
public Converter<String, LocalTime> localTimeConverter() {
// 不能使用lambda表达式,否则导致springboot启动问题
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(String source) {
try {
return StringUtils.hasText(source) ? LocalTime.parse(source, DateTimeFormatter.ofPattern(Constants.TIME_FORMAT)) : null;
} catch (Exception ex) {
throw new IllegalArgumentException(String.format("Error parse %s to LocalTime", source), ex);
}
}
};
}
@Bean
public Converter<String, Date> dateConverter() {
// 不能使用lambda表达式,否则导致springboot启动问题
return new Converter<String, Date>() {
@Override
public Date convert(String source) {
try {
return StringUtils.hasText(source) ? new SimpleDateFormat(Constants.DATE_TIME_FORMAT).parse(source) : null;
} catch (Exception ex) {
throw new IllegalArgumentException(String.format("Error parse %s to Date", source), ex);
}
}
};
}
@Component
@ConfigurationPropertiesBinding
public class PrivateKeyConverter implements Converter<String, PrivateKey> {
@Override
public PrivateKey convert(String source) {
try {
return RsaCipher.getPrivateKey(source);
} catch (Exception ex) {
throw new PlatformServiceException("privateKey configuration failed", ex);
}
}
}
@Component
@ConfigurationPropertiesBinding
public class PublicKeyConverter implements Converter<String, PublicKey> {
@Override
public PublicKey convert(String source) {
try {
return RsaCipher.getPublicKey(source);
} catch (Exception ex) {
throw new PlatformServiceException("publicKey configuration failed", ex);
}
}
}
}