From 1fdbfb97b166ed02e47056707212c2d2161d7cd3 Mon Sep 17 00:00:00 2001
From: "DESKTOP-G8BCEP0\\HP" <2037158277@qq.com>
Date: Tue, 3 Aug 2021 18:03:44 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=89=8B=E6=9C=BA?=
=?UTF-8?q?=E5=8F=B7=E7=9F=AD=E4=BF=A1=E9=AA=8C=E8=AF=81=E7=A0=81=E6=B3=A8?=
=?UTF-8?q?=E5=86=8C=E7=9A=84=E4=BB=A3=E7=A0=81=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 22 +
.../config/RedisCacheAutoConfiguration.java | 30 +
.../com/xkrs/common/config/RedisConfig.java | 161 +++
.../xkrs/common/config/WebSecurityConfig.java | 1 +
.../xkrs/controller/SysUserController.java | 22 +-
.../java/com/xkrs/model/qo/SysUserQo.java | 10 +
.../java/com/xkrs/service/RedisService.java | 993 ++++++++++++++++++
.../java/com/xkrs/service/SysUserService.java | 2 +-
.../service/impl/FirePointServiceImpl.java | 3 +-
.../xkrs/service/impl/SysUserServiceImpl.java | 12 +-
.../java/com/xkrs/utils/AliyunSmsUtils.java | 68 ++
src/main/java/com/xkrs/utils/RandomUtil.java | 9 +
src/main/resources/application.properties | 13 +
13 files changed, 1341 insertions(+), 5 deletions(-)
create mode 100644 src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java
create mode 100644 src/main/java/com/xkrs/common/config/RedisConfig.java
create mode 100644 src/main/java/com/xkrs/service/RedisService.java
create mode 100644 src/main/java/com/xkrs/utils/AliyunSmsUtils.java
diff --git a/pom.xml b/pom.xml
index f63154a..23bc21a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,6 +32,7 @@
4.5.2
5.0.0
5.0.0
+ 2.0.4
@@ -39,6 +40,10 @@
org.springframework.boot
spring-boot-starter-aop
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
org.springframework.boot
spring-boot-starter-web
@@ -193,6 +198,23 @@
${poi-ooxml.version}
+
+
+ com.aliyun
+ aliyun-java-sdk-core
+ 4.0.6
+
+
+
+ com.aliyun
+ aliyun-java-sdk-dysmsapi
+ 1.1.0
+
+
diff --git a/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java b/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java
new file mode 100644
index 0000000..6a0970b
--- /dev/null
+++ b/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java
@@ -0,0 +1,30 @@
+package com.xkrs.common.config;
+
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;
+import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.io.Serializable;
+
+/**
+ * Redis 缓存自动配置
+ * @author tajochen
+ */
+@Configuration
+@AutoConfigureAfter(RedisAutoConfiguration.class)
+public class RedisCacheAutoConfiguration {
+
+ @Bean
+ public RedisTemplate redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
+ RedisTemplate template = new RedisTemplate<>();
+ template.setKeySerializer(new StringRedisSerializer());
+ template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
+ template.setConnectionFactory(redisConnectionFactory);
+ return template;
+ }
+}
diff --git a/src/main/java/com/xkrs/common/config/RedisConfig.java b/src/main/java/com/xkrs/common/config/RedisConfig.java
new file mode 100644
index 0000000..6c68f2c
--- /dev/null
+++ b/src/main/java/com/xkrs/common/config/RedisConfig.java
@@ -0,0 +1,161 @@
+package com.xkrs.common.config;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.annotation.CachingConfigurerSupport;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.interceptor.CacheErrorHandler;
+import org.springframework.cache.interceptor.CacheResolver;
+import org.springframework.cache.interceptor.KeyGenerator;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+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.RedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.time.Duration;
+
+/**
+ * redis配置
+ * @author tajochen
+ */
+@Configuration
+@EnableCaching
+public class RedisConfig extends CachingConfigurerSupport {
+
+ private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
+
+ @Resource
+ private LettuceConnectionFactory lettuceConnectionFactory;
+
+ private Duration timeToLive = Duration.ofSeconds(3600*6);
+
+ @Bean
+ @Override
+ public KeyGenerator keyGenerator() {
+
+ // 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
+ return (target, method, params) -> {
+ StringBuilder sb = new StringBuilder();
+ sb.append(target.getClass().getName());
+ sb.append(":");
+ sb.append(method.getName());
+ for (Object obj : params) {
+ sb.append(":" + String.valueOf(obj));
+ }
+ String rsToUse = String.valueOf(sb);
+// logger.info("自动生成Redis Key -> [{}]", rsToUse);
+ return rsToUse;
+ };
+ }
+
+ @Bean
+ @Override
+ public CacheManager cacheManager() {
+ // 关键点,spring cache的注解使用的序列化都从这来,没有这个配置的话使用的jdk自己的序列化
+ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
+ //key序列化方式
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
+ //value序列化方式
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
+ .disableCachingNullValues()
+ .entryTtl(timeToLive);
+
+ RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
+ .fromConnectionFactory(lettuceConnectionFactory)
+ .cacheDefaults(config)
+ .transactionAware();
+
+ return builder.build();
+ }
+
+
+ /**
+ * RedisTemplate配置 在单独使用redisTemplate的时候 重新定义序列化方式
+ */
+ @Bean
+ public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
+ // 设置序列化
+ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
+ ObjectMapper om = new ObjectMapper();
+ // 配置null值序列化成空字符串
+ om.getSerializerProvider().setNullValueSerializer(new JsonSerializer