Ver código fonte

增加待考列表接口

wangliang 4 anos atrás
pai
commit
c358f5effa
25 arquivos alterados com 179 adições e 229 exclusões
  1. 0 7
      pom.xml
  2. 48 4
      themis-backend/src/main/java/com/qmth/themis/backend/ThemisBackendApplication.java
  3. 6 7
      themis-backend/src/main/java/com/qmth/themis/backend/api/TBUserController.java
  4. 11 0
      themis-backend/src/main/java/com/qmth/themis/backend/config/DictionaryConfig.java
  5. 4 5
      themis-backend/src/main/java/com/qmth/themis/backend/interceptor/AuthInterceptor.java
  6. 3 0
      themis-backend/src/main/resources/application.properties
  7. 0 47
      themis-backend/src/main/resources/ehcache.xml
  8. 2 0
      themis-business/src/main/java/com/qmth/themis/business/constant/SystemConstant.java
  9. 3 1
      themis-business/src/main/java/com/qmth/themis/business/domain/AliYunOssDomain.java
  10. 2 1
      themis-business/src/main/java/com/qmth/themis/business/domain/AuthNoUrlDomain.java
  11. 41 0
      themis-business/src/main/java/com/qmth/themis/business/domain/CacheConfigDomain.java
  12. 3 1
      themis-business/src/main/java/com/qmth/themis/business/domain/MqConfigDomain.java
  13. 3 1
      themis-business/src/main/java/com/qmth/themis/business/domain/PrefixUrlDomain.java
  14. 3 1
      themis-business/src/main/java/com/qmth/themis/business/domain/QuartzConfigDomain.java
  15. 2 1
      themis-business/src/main/java/com/qmth/themis/business/domain/SysDomain.java
  16. 2 1
      themis-business/src/main/java/com/qmth/themis/business/domain/SystemUrlDomain.java
  17. 3 3
      themis-business/src/main/java/com/qmth/themis/business/service/CacheService.java
  18. 10 15
      themis-business/src/main/java/com/qmth/themis/business/service/impl/CacheServiceImpl.java
  19. 0 72
      themis-business/src/main/java/com/qmth/themis/business/util/EhcacheUtil.java
  20. 10 4
      themis-exam/src/main/java/com/qmth/themis/exam/ThemisExamApplication.java
  21. 4 11
      themis-exam/src/main/java/com/qmth/themis/exam/api/TEStudentController.java
  22. 11 0
      themis-exam/src/main/java/com/qmth/themis/exam/config/DictionaryConfig.java
  23. 4 10
      themis-exam/src/main/java/com/qmth/themis/exam/interceptor/AuthInterceptor.java
  24. 4 0
      themis-exam/src/main/resources/application.properties
  25. 0 37
      themis-exam/src/main/resources/ehcache.xml

+ 0 - 7
pom.xml

@@ -42,7 +42,6 @@
         <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
         <gson.version>2.8.6</gson.version>
         <commons.version>3.10</commons.version>
-        <ehcache.version>2.10.6</ehcache.version>
         <jackson.version>2.11.0</jackson.version>
     </properties>
 
@@ -176,12 +175,6 @@
                 <artifactId>bcprov-jdk15on</artifactId>
                 <version>${bcprov.version}</version>
             </dependency>
-            <!-- ehcache 缓存 -->
-            <dependency>
-                <groupId>net.sf.ehcache</groupId>
-                <artifactId>ehcache</artifactId>
-                <version>${ehcache.version}</version>
-            </dependency>
             <!-- redis -->
             <dependency>
                 <groupId>org.springframework.data</groupId>

+ 48 - 4
themis-backend/src/main/java/com/qmth/themis/backend/ThemisBackendApplication.java

@@ -3,28 +3,37 @@ package com.qmth.themis.backend;
 import com.fasterxml.jackson.annotation.JsonAutoDetect;
 import com.fasterxml.jackson.annotation.PropertyAccessor;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.qmth.themis.backend.config.DictionaryConfig;
+import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.common.contanst.Constants;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.domain.EntityScan;
-import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
 import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Primary;
-//import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.cache.RedisCacheWriter;
 import org.springframework.data.redis.connection.RedisConnectionFactory;
 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;
 import org.springframework.scheduling.annotation.EnableAsync;
-import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 import org.springframework.web.multipart.MultipartResolver;
 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
 
+import javax.annotation.Resource;
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
+
+//import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
+
 @SpringBootApplication
 @ComponentScan(basePackages = {"com.qmth.themis"})
 @MapperScan("com.qmth.themis.business.dao")
@@ -40,6 +49,9 @@ public class ThemisBackendApplication {
         SpringApplication.run(ThemisBackendApplication.class, args);
     }
 
+    @Resource
+    DictionaryConfig dictionaryConfig;
+
     /**
      * 附件上传配置
      *
@@ -55,6 +67,38 @@ public class ThemisBackendApplication {
         return resolver;
     }
 
+    @Bean
+    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
+        return new RedisCacheManager(
+                RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
+                this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_DEFAULT_EXPIRE_TIME), // 默认策略,未配置的 key 会使用这个
+                this.getRedisCacheConfigurationMap() // 指定 key 策略
+        );
+    }
+
+    private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
+        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
+        redisCacheConfigurationMap.put(dictionaryConfig.cacheConfigDomain().getUserOauth(), this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_OAUTH_EXPIRE_TIME));
+        redisCacheConfigurationMap.put(dictionaryConfig.cacheConfigDomain().getConfigOauth(), this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_EXPIRE_TIME));
+        return redisCacheConfigurationMap;
+    }
+
+    private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Long seconds) {
+        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
+        ObjectMapper om = new ObjectMapper();
+        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
+        jackson2JsonRedisSerializer.setObjectMapper(om);
+
+        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
+        redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
+                RedisSerializationContext
+                        .SerializationPair
+                        .fromSerializer(jackson2JsonRedisSerializer)
+        ).entryTtl(Duration.ofSeconds(seconds));
+        return redisCacheConfiguration;
+    }
+
     @Bean
     @Primary
     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {

+ 6 - 7
themis-backend/src/main/java/com/qmth/themis/backend/api/TBUserController.java

@@ -15,10 +15,9 @@ import com.qmth.themis.business.entity.TBUserRole;
 import com.qmth.themis.business.enums.MqEnum;
 import com.qmth.themis.business.enums.RoleEnum;
 import com.qmth.themis.business.enums.SystemOperationEnum;
-import com.qmth.themis.business.service.EhcacheService;
+import com.qmth.themis.business.service.CacheService;
 import com.qmth.themis.business.service.TBUserRoleService;
 import com.qmth.themis.business.service.TBUserService;
-import com.qmth.themis.business.util.EhcacheUtil;
 import com.qmth.themis.business.util.JacksonUtil;
 import com.qmth.themis.business.util.RedisUtil;
 import com.qmth.themis.business.util.SessionUtil;
@@ -61,7 +60,7 @@ public class TBUserController {
     TBUserService tbUserService;
 
     @Resource
-    EhcacheService ehcacheService;
+    CacheService cacheService;
 
     @Resource
     DictionaryConfig dictionaryConfig;
@@ -146,7 +145,7 @@ public class TBUserController {
         Platform platform = Platform.valueOf(ServletUtil.getRequestPlatform());
         String deviceId = ServletUtil.getRequestDeviceId();
         //添加用户鉴权缓存
-        AuthDto authDto = ehcacheService.addAccountCache(user);
+        AuthDto authDto = cacheService.addAccountCache(user.getId());
         //生成token
         String token = RandomStringUtils.randomAlphanumeric(32);
         //添加用户缓存
@@ -486,7 +485,7 @@ public class TBUserController {
     public Result logout() throws NoSuchAlgorithmException {
         TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
         TBSession tbSession = (TBSession) ServletUtil.getRequestSession();
-        AuthDto authDto = (AuthDto) EhcacheUtil.get(SystemConstant.AUTH_CACHE, tbUser.getId());
+        AuthDto authDto = (AuthDto) redisUtil.get(dictionaryConfig.cacheConfigDomain().getUserOauth() + "::" + tbUser.getId());
         if (Objects.isNull(tbSession)) {
             throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
         }
@@ -502,7 +501,7 @@ public class TBUserController {
         }
         if (delete) {
             redisUtil.deleteUser(tbUser.getId());
-            ehcacheService.removeAccountCache(tbUser.getId());
+            cacheService.removeAccountCache(tbUser.getId());
         }
         //mq发送消息start
         mqDtoService.assembleSendOneWayMsg(dictionaryConfig.mqConfigDomain().getUserLogTopic(), authDto.getRoleCodes().toString().contains(RoleEnum.STUDENT.name()) ? dictionaryConfig.mqConfigDomain().getUserLogTopicStudentTag() : dictionaryConfig.mqConfigDomain().getUserLogTopicUserTag(), SystemOperationEnum.LOGOUT, MqEnum.USER_LOG.name(), tbUser.getId(), tbUser.getLoginName());
@@ -567,7 +566,7 @@ public class TBUserController {
                     TBUserRole tbUserRole = new TBUserRole(tbUser.getId(), s);
                     tbUserRoleService.save(tbUserRole);
                 });
-                ehcacheService.removeAccountCache(tbUser.getId());
+                cacheService.removeAccountCache(tbUser.getId());
             }
             tbUser.setUpdateId(tbUser.getId());
         }

+ 11 - 0
themis-backend/src/main/java/com/qmth/themis/backend/config/DictionaryConfig.java

@@ -80,4 +80,15 @@ public class DictionaryConfig {
     public MqConfigDomain mqConfigDomain() {
         return new MqConfigDomain();
     }
+
+    /**
+     * 缓存配置
+     *
+     * @return
+     */
+    @Bean
+    @ConfigurationProperties(prefix = "cache.config", ignoreUnknownFields = false)
+    public CacheConfigDomain cacheConfigDomain() {
+        return new CacheConfigDomain();
+    }
 }

+ 4 - 5
themis-backend/src/main/java/com/qmth/themis/backend/interceptor/AuthInterceptor.java

@@ -8,9 +8,8 @@ import com.qmth.themis.business.dto.AuthDto;
 import com.qmth.themis.business.entity.TBSession;
 import com.qmth.themis.business.entity.TBUser;
 import com.qmth.themis.business.enums.RoleEnum;
-import com.qmth.themis.business.service.EhcacheService;
+import com.qmth.themis.business.service.CacheService;
 import com.qmth.themis.business.service.TBUserService;
-import com.qmth.themis.business.util.EhcacheUtil;
 import com.qmth.themis.business.util.RedisUtil;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.enums.Platform;
@@ -41,7 +40,7 @@ public class AuthInterceptor implements HandlerInterceptor {
     private final static Logger log = LoggerFactory.getLogger(AuthInterceptor.class);
 
     @Resource
-    EhcacheService ehcacheService;
+    CacheService cacheService;
 
     @Resource
     DictionaryConfig dictionaryConfig;
@@ -105,10 +104,10 @@ public class AuthInterceptor implements HandlerInterceptor {
                     request.setAttribute(SystemConstant.SESSION, tbSession);
                     request.setAttribute(SystemConstant.ACCOUNT, tbUser);
 
-                    AuthDto authDto = (AuthDto) EhcacheUtil.get(SystemConstant.AUTH_CACHE, userId);
+                    AuthDto authDto = (AuthDto) redisUtil.get(dictionaryConfig.cacheConfigDomain().getUserOauth() + "::" + userId);
                     //验证权限
                     if (Objects.isNull(authDto)) {
-                        authDto = ehcacheService.addAccountCache(userId);
+                        authDto = cacheService.addAccountCache(userId);
                     }
                     request.setAttribute(SystemConstant.ORG, authDto.getTbOrg());
                     //系统管理员拥有所有权限

+ 3 - 0
themis-backend/src/main/resources/application.properties

@@ -128,6 +128,9 @@ sys.config.fileHost=localhost:6001${server.servlet.context-path}
 sys.config.serverHost=localhost:6001${server.servlet.context-path}
 spring.resources.static-locations=file:${sys.config.serverUpload},classpath:/META-INF/resources/,classpath:/resources/
 
+#\u7F13\u5B58\u914D\u7F6E
+cache.config.userOauth=user:oauth:cache
+cache.config.configOauth=config:cache
 #============================================================================
 # \u914D\u7F6Erocketmq
 #============================================================================

+ 0 - 47
themis-backend/src/main/resources/ehcache.xml

@@ -1,47 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
-         updateCheck="false">
-    <diskStore path="/Users/king/Downloads/ehcache" />
-
-    <!--overflowToDisk:是否保存到磁盘,当系统宕机时-->
-    <!--diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。-->
-    <defaultCache
-            eternal="false"
-            maxElementsInMemory="10000"
-            overflowToDisk="false"
-            diskPersistent="false"
-            timeToIdleSeconds="0"
-            timeToLiveSeconds="2400"
-            memoryStoreEvictionPolicy="LRU" />
-
-    <cache
-            name="auth_cache"
-            eternal="false"
-            maxElementsInMemory="1000"
-            overflowToDisk="false"
-            diskPersistent="false"
-            timeToIdleSeconds="0"
-            timeToLiveSeconds="43200"
-            memoryStoreEvictionPolicy="LRU" />
-
-<!--    <cache-->
-<!--            name="org_cache"-->
-<!--            eternal="false"-->
-<!--            maxElementsInMemory="1000"-->
-<!--            overflowToDisk="false"-->
-<!--            diskPersistent="false"-->
-<!--            timeToIdleSeconds="0"-->
-<!--            timeToLiveSeconds="43200"-->
-<!--            memoryStoreEvictionPolicy="LRU" />-->
-
-<!--    <cache-->
-<!--            name="user_cache"-->
-<!--            eternal="false"-->
-<!--            maxElementsInMemory="1000"-->
-<!--            overflowToDisk="false"-->
-<!--            diskPersistent="false"-->
-<!--            timeToIdleSeconds="0"-->
-<!--            timeToLiveSeconds="43200"-->
-<!--            memoryStoreEvictionPolicy="LRU" />-->
-</ehcache>

+ 2 - 0
themis-business/src/main/java/com/qmth/themis/business/constant/SystemConstant.java

@@ -99,6 +99,8 @@ public class SystemConstant {
      */
     public static final long REDIS_EXPIRE_TIME = 60L * 1440L;//过期时间24小时
     public static final long REDIS_REFRESH_EXPIRE_TIME = 60L * 60L;//刷新时间1小时,理论上说24小时还不睡觉也已经快不行了
+    public static final long REDIS_OAUTH_EXPIRE_TIME = 2 * 60L * 60L;
+    public static final long REDIS_DEFAULT_EXPIRE_TIME = 1 * 60L * 60L;
 //    public static final long REFRESH_EXPIRE_TIME = 60L * 5L;//过期剩余时间5分钟
     /**
      * rocket mq

+ 3 - 1
themis-business/src/main/java/com/qmth/themis/business/domain/AliYunOssDomain.java

@@ -1,5 +1,7 @@
 package com.qmth.themis.business.domain;
 
+import java.io.Serializable;
+
 /**
  * @Description: 阿里云oss config
  * @Param:
@@ -7,7 +9,7 @@ package com.qmth.themis.business.domain;
  * @Author: wangliang
  * @Date: 2020/3/30
  */
-public class AliYunOssDomain {
+public class AliYunOssDomain implements Serializable {
 
     private String endpoint;
 

+ 2 - 1
themis-business/src/main/java/com/qmth/themis/business/domain/AuthNoUrlDomain.java

@@ -1,5 +1,6 @@
 package com.qmth.themis.business.domain;
 
+import java.io.Serializable;
 import java.util.List;
 
 /**
@@ -9,7 +10,7 @@ import java.util.List;
  * @Author: wangliang
  * @Date: 2020/4/10
  */
-public class AuthNoUrlDomain {
+public class AuthNoUrlDomain implements Serializable {
 
     List urls;
 

+ 41 - 0
themis-business/src/main/java/com/qmth/themis/business/domain/CacheConfigDomain.java

@@ -0,0 +1,41 @@
+package com.qmth.themis.business.domain;
+
+import java.io.Serializable;
+
+/**
+ * @Description: 缓存配置
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2020/7/1
+ */
+public class CacheConfigDomain implements Serializable {
+
+    private String userOauth;
+    private String configOauth;
+    private String studentOauth;
+
+    public String getUserOauth() {
+        return userOauth;
+    }
+
+    public void setUserOauth(String userOauth) {
+        this.userOauth = userOauth;
+    }
+
+    public String getConfigOauth() {
+        return configOauth;
+    }
+
+    public void setConfigOauth(String configOauth) {
+        this.configOauth = configOauth;
+    }
+
+    public String getStudentOauth() {
+        return studentOauth;
+    }
+
+    public void setStudentOauth(String studentOauth) {
+        this.studentOauth = studentOauth;
+    }
+}

+ 3 - 1
themis-business/src/main/java/com/qmth/themis/business/domain/MqConfigDomain.java

@@ -1,5 +1,7 @@
 package com.qmth.themis.business.domain;
 
+import java.io.Serializable;
+
 /**
  * @Description: mq配置
  * @Param:
@@ -7,7 +9,7 @@ package com.qmth.themis.business.domain;
  * @Author: wangliang
  * @Date: 2020/7/1
  */
-public class MqConfigDomain {
+public class MqConfigDomain implements Serializable {
 
     private String server;
     private String sessionTopic;

+ 3 - 1
themis-business/src/main/java/com/qmth/themis/business/domain/PrefixUrlDomain.java

@@ -1,5 +1,7 @@
 package com.qmth.themis.business.domain;
 
+import java.io.Serializable;
+
 /**
  * @Description: 模块前缀
  * @Param:
@@ -7,7 +9,7 @@ package com.qmth.themis.business.domain;
  * @Author: wangliang
  * @Date: 2020/4/10
  */
-public class PrefixUrlDomain {
+public class PrefixUrlDomain implements Serializable {
 
     String admin;
 

+ 3 - 1
themis-business/src/main/java/com/qmth/themis/business/domain/QuartzConfigDomain.java

@@ -1,5 +1,7 @@
 package com.qmth.themis.business.domain;
 
+import java.io.Serializable;
+
 /**
  * @Description: quartz配置
  * @Param:
@@ -7,7 +9,7 @@ package com.qmth.themis.business.domain;
  * @Author: wangliang
  * @Date: 2020/7/1
  */
-public class QuartzConfigDomain {
+public class QuartzConfigDomain implements Serializable {
 
     private String backendJobName;
     private String backendJobGroupName;

+ 2 - 1
themis-business/src/main/java/com/qmth/themis/business/domain/SysDomain.java

@@ -1,5 +1,6 @@
 package com.qmth.themis.business.domain;
 
+import java.io.Serializable;
 import java.util.List;
 
 /**
@@ -9,7 +10,7 @@ import java.util.List;
  * @Author: wangliang
  * @Date: 2020/4/10
  */
-public class SysDomain {
+public class SysDomain implements Serializable {
 
     boolean oss;
 

+ 2 - 1
themis-business/src/main/java/com/qmth/themis/business/domain/SystemUrlDomain.java

@@ -1,5 +1,6 @@
 package com.qmth.themis.business.domain;
 
+import java.io.Serializable;
 import java.util.List;
 
 /**
@@ -9,7 +10,7 @@ import java.util.List;
  * @Author: wangliang
  * @Date: 2020/4/10
  */
-public class SystemUrlDomain {
+public class SystemUrlDomain implements Serializable {
 
     List urls;
 

+ 3 - 3
themis-business/src/main/java/com/qmth/themis/business/service/EhcacheService.java → themis-business/src/main/java/com/qmth/themis/business/service/CacheService.java

@@ -9,15 +9,15 @@ import com.qmth.themis.business.dto.AuthDto;
  * @Author: wangliang
  * @Date: 2020/6/27
  */
-public interface EhcacheService {
+public interface CacheService {
 
     /**
      * 添加用户缓存
      *
-     * @param o
+     * @param userId
      * @return
      */
-    public AuthDto addAccountCache(Object o);
+    public AuthDto addAccountCache(Long userId);
 
     /**
      * 删除用户缓存

+ 10 - 15
themis-business/src/main/java/com/qmth/themis/business/service/impl/EhcacheServiceImpl.java → themis-business/src/main/java/com/qmth/themis/business/service/impl/CacheServiceImpl.java

@@ -6,13 +6,14 @@ import com.qmth.themis.business.dto.AuthDto;
 import com.qmth.themis.business.entity.*;
 import com.qmth.themis.business.enums.RoleEnum;
 import com.qmth.themis.business.service.*;
-import com.qmth.themis.business.util.EhcacheUtil;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.exception.BusinessException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.core.annotation.AliasFor;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -30,8 +31,8 @@ import java.util.stream.Collectors;
  * @Date: 2020/6/27
  */
 @Service
-public class EhcacheServiceImpl implements EhcacheService {
-    private final static Logger log = LoggerFactory.getLogger(EhcacheServiceImpl.class);
+public class CacheServiceImpl implements CacheService {
+    private final static Logger log = LoggerFactory.getLogger(CacheServiceImpl.class);
 
     @Resource
     TBUserRoleService tbUserRoleService;
@@ -54,19 +55,15 @@ public class EhcacheServiceImpl implements EhcacheService {
     /**
      * 添加用户缓存
      *
-     * @param o
+     * @param userId
      * @return
      */
     @Override
-    public AuthDto addAccountCache(Object o) {
+    @Cacheable(value = "user:oauth:cache", key = "#p0")
+    public AuthDto addAccountCache(Long userId) {
         AuthDto authDto = null;
         try {
-            TBUser user = null;
-            if (o instanceof TBUser) {
-                user = (TBUser) o;
-            } else {
-                user = tbUserService.getById(Long.parseLong(String.valueOf(o)));
-            }
+            TBUser user = tbUserService.getById(userId);
             if (Objects.isNull(user)) {
                 throw new BusinessException(ExceptionResultEnum.USER_NO);
             }
@@ -83,7 +80,6 @@ public class EhcacheServiceImpl implements EhcacheService {
                     pWrapper.lambda().eq(TBPrivilege::getType, SystemConstant.LINK);
                     List<TBPrivilege> tbPrivilegeList = tbPrivilegeService.list(pWrapper);
                     authDto = new AuthDto(tbUserRoleList.stream().map(s -> s.getRoleCode()).collect(Collectors.toSet()), tbPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()));
-//                    EhcacheUtil.put(SystemConstant.AUTH_CACHE, user.getId(), authDto);
                 } else {
                     TBOrg tbOrg = tbOrgService.getById(user.getOrgId());
                     //根据角色名查权限
@@ -96,7 +92,6 @@ public class EhcacheServiceImpl implements EhcacheService {
                     pWrapper.lambda().in(TBPrivilege::getId, privilegeIds).eq(TBPrivilege::getType, SystemConstant.LINK);
                     List<TBPrivilege> tbPrivilegeList = tbPrivilegeService.list(pWrapper);
                     authDto = new AuthDto(roleCodes, tbPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), tbOrg);
-//                    EhcacheUtil.put(SystemConstant.AUTH_CACHE, user.getId(), authDto);
                 }
             }
         } catch (Exception e) {
@@ -112,8 +107,9 @@ public class EhcacheServiceImpl implements EhcacheService {
      * @param userId
      */
     @Override
+    @CacheEvict(value = "user:oauth:cache", key = "#p0")
     public void removeAccountCache(Long userId) {
-        EhcacheUtil.remove(SystemConstant.AUTH_CACHE, userId);
+
     }
 
     /**
@@ -143,7 +139,6 @@ public class EhcacheServiceImpl implements EhcacheService {
             Set<String> roleCodes = new HashSet<>();
             roleCodes.add(RoleEnum.STUDENT.name());
             authDto = new AuthDto(roleCodes, tbPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), tbOrg);
-//            EhcacheUtil.put(SystemConstant.STUDENT_CACHE, teStudent.getId(), authDto);
         } catch (Exception e) {
             e.printStackTrace();
             throw new BusinessException("添加学生缓存失败");

+ 0 - 72
themis-business/src/main/java/com/qmth/themis/business/util/EhcacheUtil.java

@@ -1,72 +0,0 @@
-package com.qmth.themis.business.util;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import net.sf.ehcache.Cache;
-import net.sf.ehcache.CacheManager;
-import net.sf.ehcache.Element;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.net.URL;
-import java.util.Objects;
-
-/**
- * @Description: ehcache util
- * @Param:
- * @return:
- * @Author: wangliang
- * @Date: 2020/6/23
- */
-public class EhcacheUtil {
-    private final static Logger log = LoggerFactory.getLogger(EhcacheUtil.class);
-
-    private static final String path = "/ehcache.xml";
-
-    private URL url;
-    private static CacheManager manager;
-    private volatile static EhcacheUtil ehCache;
-
-    private EhcacheUtil(String path) {
-        url = getClass().getResource(path);
-        manager = CacheManager.create(url);
-    }
-
-    public static EhcacheUtil getInstance() {
-        if (Objects.isNull(ehCache)) {
-            synchronized (EhcacheUtil.class) {
-                if (Objects.isNull(ehCache)) {
-                    ehCache = new EhcacheUtil(path);
-                }
-            }
-        }
-        return ehCache;
-    }
-
-    static {
-        getInstance();
-    }
-
-    public static void put(String cacheName, Object key, Object value) {
-        Cache cache = manager.getCache(cacheName);
-        Element element = new Element(key, value);
-        cache.put(element);
-    }
-
-    public static Object get(String cacheName, Object key) {
-        Cache cache = manager.getCache(cacheName);
-        Element element = cache.get(key);
-        if (Objects.nonNull(element)) {
-            log.info("Element:{}", JacksonUtil.parseJson(element));
-        }
-        return element == null ? null : element.getObjectValue();
-    }
-
-    public static Cache get(String cacheName) {
-        return manager.getCache(cacheName);
-    }
-
-    public static void remove(String cacheName, Object key) {
-        Cache cache = manager.getCache(cacheName);
-        cache.remove(key);
-    }
-}

+ 10 - 4
themis-exam/src/main/java/com/qmth/themis/exam/ThemisExamApplication.java

@@ -3,7 +3,9 @@ package com.qmth.themis.exam;
 import com.fasterxml.jackson.annotation.JsonAutoDetect;
 import com.fasterxml.jackson.annotation.PropertyAccessor;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.common.contanst.Constants;
+import com.qmth.themis.exam.config.DictionaryConfig;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -25,6 +27,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
 import org.springframework.web.multipart.MultipartResolver;
 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
 
+import javax.annotation.Resource;
 import java.time.Duration;
 import java.util.HashMap;
 import java.util.Map;
@@ -43,6 +46,9 @@ public class ThemisExamApplication {
         SpringApplication.run(ThemisExamApplication.class, args);
     }
 
+    @Resource
+    DictionaryConfig dictionaryConfig;
+
     /**
      * 附件上传配置
      *
@@ -62,19 +68,19 @@ public class ThemisExamApplication {
     public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
         return new RedisCacheManager(
                 RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
-                this.getRedisCacheConfigurationWithTtl(40 * 60), // 默认策略,未配置的 key 会使用这个
+                this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_DEFAULT_EXPIRE_TIME), // 默认策略,未配置的 key 会使用这个
                 this.getRedisCacheConfigurationMap() // 指定 key 策略
         );
     }
 
     private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
         Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
-        redisCacheConfigurationMap.put("student:oauth:cache", this.getRedisCacheConfigurationWithTtl(2 * 60 * 60));//一个小时
-        redisCacheConfigurationMap.put("config:cache", this.getRedisCacheConfigurationWithTtl(24 * 60 * 60));
+        redisCacheConfigurationMap.put(dictionaryConfig.cacheConfigDomain().getStudentOauth(), this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_OAUTH_EXPIRE_TIME));
+        redisCacheConfigurationMap.put(dictionaryConfig.cacheConfigDomain().getConfigOauth(), this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_EXPIRE_TIME));
         return redisCacheConfigurationMap;
     }
 
-    private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
+    private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Long seconds) {
         Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
         ObjectMapper om = new ObjectMapper();
         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

+ 4 - 11
themis-exam/src/main/java/com/qmth/themis/exam/api/TEStudentController.java

@@ -1,7 +1,6 @@
 package com.qmth.themis.exam.api;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.qmth.themis.business.constant.SpringContextHolder;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dto.AuthDto;
 import com.qmth.themis.business.entity.TBSession;
@@ -11,7 +10,6 @@ import com.qmth.themis.business.enums.MqEnum;
 import com.qmth.themis.business.enums.RoleEnum;
 import com.qmth.themis.business.enums.SystemOperationEnum;
 import com.qmth.themis.business.service.*;
-import com.qmth.themis.business.util.EhcacheUtil;
 import com.qmth.themis.business.util.JacksonUtil;
 import com.qmth.themis.business.util.RedisUtil;
 import com.qmth.themis.business.util.SessionUtil;
@@ -30,7 +28,6 @@ import com.qmth.themis.exam.util.ServletUtil;
 import com.qmth.themis.mq.service.MqDtoService;
 import io.swagger.annotations.*;
 import org.apache.commons.lang3.RandomStringUtils;
-import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -56,7 +53,7 @@ public class TEStudentController {
     TEStudentService teStudentService;
 
     @Resource
-    EhcacheService ehcacheService;
+    CacheService cacheService;
 
     @Resource
     DictionaryConfig dictionaryConfig;
@@ -76,9 +73,6 @@ public class TEStudentController {
     @Resource
     TOeExamRecordService tOeExamRecordService;
 
-    @Resource
-    RedisTemplate redisTemplate;
-
     @ApiOperation(value = "学生登录接口")
     @RequestMapping(value = "/login", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "学生信息", response = TEStudent.class)})
@@ -126,7 +120,7 @@ public class TEStudentController {
         Platform platform = Platform.valueOf(ServletUtil.getRequestPlatform());
         String deviceId = ServletUtil.getRequestDeviceId();
         //添加用户鉴权缓存
-        AuthDto authDto = ehcacheService.addStudentCache(teStudent.getId());
+        AuthDto authDto = cacheService.addStudentCache(teStudent.getId());
         //生成token
         String token = RandomStringUtils.randomAlphanumeric(32);
         //添加用户缓存
@@ -172,8 +166,7 @@ public class TEStudentController {
         if (Objects.isNull(tbSession)) {
             throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
         }
-        AuthDto authDto = (AuthDto) redisTemplate.opsForValue().get("student:oauth:cache::" + teStudent.getId());
-//        AuthDto authDto = (AuthDto) EhcacheUtil.get(SystemConstant.STUDENT_CACHE, teStudent.getId());
+        AuthDto authDto = (AuthDto) redisUtil.get(dictionaryConfig.cacheConfigDomain().getStudentOauth() + "::" + teStudent.getId());
         redisUtil.deleteUserSession(tbSession.getId());
         //循环检查该用户下其他平台是否存在session,不存在则删除用户缓存和鉴权缓存
         boolean delete = true;
@@ -186,7 +179,7 @@ public class TEStudentController {
         }
         if (delete) {
             redisUtil.deleteStudent(teStudent.getId());
-            ehcacheService.removeStudentCache(teStudent.getId());
+            cacheService.removeStudentCache(teStudent.getId());
         }
         //mq发送消息start
         mqDtoService.assembleSendOneWayMsg(dictionaryConfig.mqConfigDomain().getUserLogTopic(), authDto.getRoleCodes().toString().contains(RoleEnum.STUDENT.name()) ? dictionaryConfig.mqConfigDomain().getUserLogTopicStudentTag() : dictionaryConfig.mqConfigDomain().getUserLogTopicUserTag(), SystemOperationEnum.LOGOUT, MqEnum.EXAM_STUDENT_LOG.name(), teStudent.getId(), teStudent.getIdentity());

+ 11 - 0
themis-exam/src/main/java/com/qmth/themis/exam/config/DictionaryConfig.java

@@ -80,4 +80,15 @@ public class DictionaryConfig {
     public MqConfigDomain mqConfigDomain() {
         return new MqConfigDomain();
     }
+
+    /**
+     * 缓存配置
+     *
+     * @return
+     */
+    @Bean
+    @ConfigurationProperties(prefix = "cache.config", ignoreUnknownFields = false)
+    public CacheConfigDomain cacheConfigDomain() {
+        return new CacheConfigDomain();
+    }
 }

+ 4 - 10
themis-exam/src/main/java/com/qmth/themis/exam/interceptor/AuthInterceptor.java

@@ -6,9 +6,8 @@ import com.qmth.themis.business.dto.AuthDto;
 import com.qmth.themis.business.entity.TBSession;
 import com.qmth.themis.business.entity.TEStudent;
 import com.qmth.themis.business.enums.RoleEnum;
-import com.qmth.themis.business.service.EhcacheService;
+import com.qmth.themis.business.service.CacheService;
 import com.qmth.themis.business.service.TEStudentService;
-import com.qmth.themis.business.util.EhcacheUtil;
 import com.qmth.themis.business.util.RedisUtil;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.enums.Platform;
@@ -19,7 +18,6 @@ import com.qmth.themis.exam.config.DictionaryConfig;
 import com.qmth.themis.exam.util.ServletUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.ModelAndView;
 
@@ -42,7 +40,7 @@ public class AuthInterceptor implements HandlerInterceptor {
     private final static Logger log = LoggerFactory.getLogger(AuthInterceptor.class);
 
     @Resource
-    EhcacheService ehcacheService;
+    CacheService cacheService;
 
     @Resource
     DictionaryConfig dictionaryConfig;
@@ -53,9 +51,6 @@ public class AuthInterceptor implements HandlerInterceptor {
     @Resource
     RedisUtil redisUtil;
 
-    @Resource
-    RedisTemplate redisTemplate;
-
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
         log.info("exam HandlerInterceptor preHandle is come in");
@@ -109,11 +104,10 @@ public class AuthInterceptor implements HandlerInterceptor {
                     request.setAttribute(SystemConstant.SESSION, tbSession);
                     request.setAttribute(SystemConstant.STUDENT_ACCOUNT, teStudent);
 
-//                    AuthDto authDto = (AuthDto) EhcacheUtil.get(SystemConstant.STUDENT_CACHE, userId);
-                    AuthDto authDto = (AuthDto) redisTemplate.opsForValue().get("student:oauth:cache::" + userId);
+                    AuthDto authDto = (AuthDto) redisUtil.get(dictionaryConfig.cacheConfigDomain().getStudentOauth() + userId);
                     //验证权限
                     if (Objects.isNull(authDto)) {
-                        authDto = ehcacheService.addStudentCache(userId);
+                        authDto = cacheService.addStudentCache(userId);
                     }
                     request.setAttribute(SystemConstant.ORG, authDto.getTbOrg());
                     //系统管理员拥有所有权限

+ 4 - 0
themis-exam/src/main/resources/application.properties

@@ -188,6 +188,10 @@ sys.config.fileHost=localhost:6002${server.servlet.context-path}
 sys.config.serverHost=localhost:6002${server.servlet.context-path}
 spring.resources.static-locations=file:${sys.config.serverUpload},classpath:/META-INF/resources/,classpath:/resources/
 
+#\u7F13\u5B58\u914D\u7F6E
+cache.config.studentOauth=student:oauth:cache
+cache.config.configOauth=config:cache
+
 #api\u524D\u7F00
 prefix.url.exam=api/oe
 

+ 0 - 37
themis-exam/src/main/resources/ehcache.xml

@@ -1,37 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
-         updateCheck="false">
-    <diskStore path="/Users/king/Downloads/ehcache" />
-
-    <!--overflowToDisk:是否保存到磁盘,当系统宕机时-->
-    <!--diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。-->
-    <defaultCache
-            eternal="false"
-            maxElementsInMemory="10000"
-            overflowToDisk="false"
-            diskPersistent="false"
-            timeToIdleSeconds="0"
-            timeToLiveSeconds="2400"
-            memoryStoreEvictionPolicy="LRU" />
-
-    <cache
-            name="student_cache"
-            eternal="false"
-            maxElementsInMemory="1000"
-            overflowToDisk="false"
-            diskPersistent="false"
-            timeToIdleSeconds="0"
-            timeToLiveSeconds="43200"
-            memoryStoreEvictionPolicy="LRU" />
-
-<!--    <cache-->
-<!--            name="config_cache"-->
-<!--            eternal="false"-->
-<!--            maxElementsInMemory="1000"-->
-<!--            overflowToDisk="false"-->
-<!--            diskPersistent="false"-->
-<!--            timeToIdleSeconds="0"-->
-<!--            timeToLiveSeconds="43200"-->
-<!--            memoryStoreEvictionPolicy="LRU" />-->
-</ehcache>