SharedConfiguration.java
7.32 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.diligrp.cashier.shared;
import com.diligrp.cashier.shared.exception.PlatformServiceException;
import com.diligrp.cashier.shared.security.RsaCipher;
import com.diligrp.cashier.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.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
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;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@Configuration
@ComponentScan("com.diligrp.cashier.shared")
public class SharedConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(@NonNull CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
};
}
@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(@NonNull 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(@NonNull 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(@NonNull 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(@NonNull 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 static class PrivateKeyConverter implements Converter<String, PrivateKey> {
@Override
public PrivateKey convert(@NonNull String source) {
try {
return RsaCipher.getPrivateKey(source);
} catch (Exception ex) {
throw new PlatformServiceException("privateKey configuration failed", ex);
}
}
}
@Component
@ConfigurationPropertiesBinding
public static class PublicKeyConverter implements Converter<String, PublicKey> {
@Override
public PublicKey convert(@NonNull String source) {
try {
return RsaCipher.getPublicKey(source);
} catch (Exception ex) {
throw new PlatformServiceException("publicKey configuration failed", ex);
}
}
}
@Component
@ConfigurationPropertiesBinding
public static class SecretKeyConverter implements Converter<String, SecretKeySpec> {
@Override
public SecretKeySpec convert(@NonNull String source) {
return new SecretKeySpec(source.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
}
}
/**
* 配置虚拟线程池
* 虚拟线程池不需要设置核心线程数等等factory
*/
@Bean(name = "virtualThreadPoolTaskExecutor")
public Executor virtualThreadPoolTaskExecutor() {
return Executors.newVirtualThreadPerTaskExecutor();
}
}