wangliang 4 жил өмнө
parent
commit
1eb2610a9a

+ 18 - 15
themis-backend/src/main/java/com/qmth/themis/backend/api/SysController.java

@@ -15,7 +15,6 @@ import com.qmth.themis.business.enums.DownloadFileEnum;
 import com.qmth.themis.business.enums.RoleEnum;
 import com.qmth.themis.business.enums.UploadFileEnum;
 import com.qmth.themis.business.service.*;
-import com.qmth.themis.business.util.EhcacheUtil;
 import com.qmth.themis.business.util.RedisUtil;
 import com.qmth.themis.business.util.ServletUtil;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
@@ -193,19 +192,23 @@ public class SysController {
         if (Objects.isNull(code) || Objects.equals(code, "")) {
             throw new BusinessException(ExceptionResultEnum.ORG_CODE_IS_NULL);
         }
-        TBOrg tbOrg = (TBOrg) EhcacheUtil.get(SystemConstant.orgCodeCache, code);
-        if (Objects.isNull(tbOrg)) {
-            QueryWrapper<TBOrg> tbOrgQueryWrapper = new QueryWrapper<>();
-            tbOrgQueryWrapper.lambda().eq(TBOrg::getCode, code);
-            tbOrg = tbOrgService.getOne(tbOrgQueryWrapper);
-            if (Objects.nonNull(tbOrg)) {
-                EhcacheUtil.put(SystemConstant.orgCodeCache, code, tbOrg);
+        if (!Objects.equals(code.toUpperCase(), SystemConstant.ADMIN)) {
+            TBOrg tbOrg = (TBOrg) redisUtil.getOrgCode(code);
+            if (Objects.isNull(tbOrg)) {
+                QueryWrapper<TBOrg> tbOrgQueryWrapper = new QueryWrapper<>();
+                tbOrgQueryWrapper.lambda().eq(TBOrg::getCode, code);
+                tbOrg = tbOrgService.getOne(tbOrgQueryWrapper);
+                if (Objects.nonNull(tbOrg)) {
+                    redisUtil.setOrgCode(code, tbOrg);
+                }
             }
+            if (Objects.isNull(tbOrg)) {
+                throw new BusinessException(ExceptionResultEnum.ORG_NO);
+            }
+            return ResultUtil.ok(Collections.singletonMap("logo", tbOrg.getLogo()));
+        } else {
+            return ResultUtil.ok(Collections.singletonMap("logo", null));
         }
-        if (Objects.isNull(tbOrg)) {
-            throw new BusinessException(ExceptionResultEnum.ORG_NO);
-        }
-        return ResultUtil.ok(Collections.singletonMap("logo", tbOrg.getLogo()));
     }
 
     @ApiOperation(value = "机构查询接口")
@@ -232,13 +235,13 @@ public class SysController {
     @RequestMapping(value = "/role/query", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "角色信息", response = TBRole.class)})
     public Result roleList() {
-        List<TBRole> tbRoleList = (List<TBRole>) EhcacheUtil.get(SystemConstant.roleCache, SystemConstant.roleCache);
-        if (Objects.isNull(tbRoleList)) {
+        List<TBRole> tbRoleList = (List<TBRole>) redisUtil.getRole();
+        if (Objects.isNull(tbRoleList) || tbRoleList.size() == 0) {
             QueryWrapper<TBRole> tbRoleQueryWrapper = new QueryWrapper<>();
             tbRoleQueryWrapper.lambda().notIn(TBRole::getRoleCode, RoleEnum.SUPER_ADMIN.name(), RoleEnum.STUDENT.name());
             tbRoleList = tbRoleService.list(tbRoleQueryWrapper);
             if (Objects.nonNull(tbRoleList)) {
-                EhcacheUtil.put(SystemConstant.roleCache, SystemConstant.roleCache, tbRoleList);
+                redisUtil.setRole(tbRoleList);
             }
         }
         return ResultUtil.ok(tbRoleList);

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

@@ -3,13 +3,11 @@ package com.qmth.themis.backend.api;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.qmth.themis.business.annotation.ApiJsonObject;
 import com.qmth.themis.business.annotation.ApiJsonProperty;
-import com.qmth.themis.business.config.SystemConfig;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dto.response.TBOrgDto;
 import com.qmth.themis.business.entity.TBOrg;
 import com.qmth.themis.business.entity.TBUser;
 import com.qmth.themis.business.service.TBOrgService;
-import com.qmth.themis.business.util.EhcacheUtil;
 import com.qmth.themis.business.util.RedisUtil;
 import com.qmth.themis.business.util.ServletUtil;
 import com.qmth.themis.common.contanst.Constants;
@@ -18,7 +16,6 @@ import com.qmth.themis.common.exception.BusinessException;
 import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.ResultUtil;
 import io.swagger.annotations.*;
-import org.springframework.cache.annotation.CachePut;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
@@ -45,7 +42,7 @@ public class TBOrgController {
     @Resource
     RedisUtil redisUtil;
 
-//    @CachePut(value = "org_cache", key = "'orgCacheQuery'")
+    //    @CachePut(value = "org_cache", key = "'orgCacheQuery'")
 //    @Cacheable(value = "org_cache", key = "#p0")
     @ApiOperation(value = "机构查询分页接口")
     @RequestMapping(value = "/query", method = RequestMethod.POST)
@@ -63,6 +60,9 @@ public class TBOrgController {
         if (Objects.isNull(tbOrg)) {
             throw new BusinessException(ExceptionResultEnum.ORG_INFO_IS_NULL);
         }
+        if (Objects.nonNull(tbOrg.getCode()) && Objects.equals(tbOrg.getCode().toUpperCase(), SystemConstant.ADMIN)) {
+            throw new BusinessException("admin为超级管理员专属code,请重新输入");
+        }
         TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
         if (Objects.isNull(tbOrg.getId())) {
             tbOrg.setId(Constants.idGen.next());
@@ -71,7 +71,7 @@ public class TBOrgController {
             tbOrg.setUpdateId(tbUser.getId());
         }
         redisUtil.setOrg(tbOrg.getId(), tbOrg);
-        EhcacheUtil.put(SystemConstant.orgCodeCache, tbOrg.getCode(), tbOrg);
+        redisUtil.setOrgCode(tbOrg.getCode(), tbOrg);
         tbOrgService.saveOrUpdate(tbOrg);
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }
@@ -103,7 +103,7 @@ public class TBOrgController {
         tbOrg.setEnable(enable);
         tbOrgService.updateById(tbOrg);
         redisUtil.setOrg(orgId, tbOrg);
-        EhcacheUtil.put(SystemConstant.orgCodeCache, tbOrg.getCode(), tbOrg);
+        redisUtil.setOrgCode(tbOrg.getCode(), tbOrg);
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }
 }

+ 30 - 17
themis-backend/src/main/java/com/qmth/themis/backend/api/TBUserController.java

@@ -19,7 +19,6 @@ import com.qmth.themis.business.enums.MqTagEnum;
 import com.qmth.themis.business.enums.MqTopicEnum;
 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.RedisUtil;
 import com.qmth.themis.business.util.ServletUtil;
 import com.qmth.themis.business.util.SessionUtil;
@@ -99,31 +98,45 @@ public class TBUserController {
         String loginName = String.valueOf(mapParameter.get("loginName"));
         String password = String.valueOf(mapParameter.get("password"));
         String orgCode = String.valueOf(mapParameter.get("code"));
-        TBOrg tbOrg = (TBOrg) EhcacheUtil.get(SystemConstant.orgCodeCache, orgCode);
-        if (Objects.isNull(tbOrg)) {
-            QueryWrapper<TBOrg> tbOrgQueryWrapper = new QueryWrapper<>();
-            tbOrgQueryWrapper.lambda().eq(TBOrg::getCode, orgCode);
-            tbOrg = tbOrgService.getOne(tbOrgQueryWrapper);
-            if (Objects.nonNull(tbOrg)) {
-                EhcacheUtil.put(SystemConstant.orgCodeCache, orgCode, tbOrg);
+        TBOrg tbOrg = null;
+        if (!Objects.equals(orgCode.toUpperCase(), SystemConstant.ADMIN)) {
+            tbOrg = (TBOrg) redisUtil.getOrgCode(orgCode);
+            if (Objects.isNull(tbOrg)) {
+                QueryWrapper<TBOrg> tbOrgQueryWrapper = new QueryWrapper<>();
+                tbOrgQueryWrapper.lambda().eq(TBOrg::getCode, orgCode);
+                tbOrg = tbOrgService.getOne(tbOrgQueryWrapper);
+                if (Objects.nonNull(tbOrg)) {
+                    redisUtil.setOrgCode(orgCode, tbOrg);
+                }
+            }
+            if (Objects.isNull(tbOrg)) {
+                throw new BusinessException(ExceptionResultEnum.ORG_NO);
+            }
+            if (Objects.nonNull(tbOrg.getEnable()) && tbOrg.getEnable().intValue() == 0) {
+                throw new BusinessException(ExceptionResultEnum.ORG_ENABLE);
             }
-        }
-        if (Objects.isNull(tbOrg)) {
-            throw new BusinessException(ExceptionResultEnum.ORG_NO);
-        }
-        if (Objects.nonNull(tbOrg.getEnable()) && tbOrg.getEnable().intValue() == 0) {
-            throw new BusinessException(ExceptionResultEnum.ORG_ENABLE);
         }
         QueryWrapper<TBUser> wrapper = new QueryWrapper<>();
         wrapper.lambda().eq(TBUser::getLoginName, loginName);
-        if (!loginName.contains(SystemConstant.SYSTEM_ACCOUNT)) {
+        if (!Objects.equals(orgCode.toUpperCase(), SystemConstant.ADMIN)) {
             wrapper.lambda().eq(TBUser::getOrgId, tbOrg.getId());
         }
-        TBUser user = tbUserService.getOne(wrapper);
+        List<TBUser> userList = tbUserService.list(wrapper);
         //用户不存在
-        if (Objects.isNull(user)) {
+        if (Objects.isNull(userList) || userList.size() == 0) {
             throw new BusinessException(ExceptionResultEnum.USER_NO);
         }
+        if (Objects.equals(orgCode.toUpperCase(), SystemConstant.ADMIN)) {
+            userList.forEach(s -> {
+                if (!Objects.equals(s.getRemark(), SystemConstant.SYSADMIN)) {
+                    throw new BusinessException("机构code为admin只允许超级管理员登录");
+                }
+            });
+        }
+        if (userList.size() > 1) {
+            throw new BusinessException("查询的用户有多条");
+        }
+        TBUser user = userList.get(0);
         if (Objects.nonNull(user.getOrgId()) && user.getOrgId().longValue() != tbOrg.getId().longValue()) {
             throw new BusinessException("用户机构不匹配");
         }

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

@@ -1,60 +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="true"
-            maxElementsInMemory="1"
-            maxElementsOnDisk="0"
-            overflowToDisk="true"
-            diskPersistent="true"
-            timeToIdleSeconds="0"
-            timeToLiveSeconds="43200"
-            memoryStoreEvictionPolicy="LRU" />
-
-    <cache
-            name="org_code_cache"
-            eternal="true"
-            maxElementsInMemory="1"
-            maxElementsOnDisk="0"
-            overflowToDisk="true"
-            diskPersistent="true"
-            timeToIdleSeconds="0"
-            timeToLiveSeconds="43200"
-            memoryStoreEvictionPolicy="LRU" />
-
-    <cache
-            name="role_cache"
-            eternal="true"
-            maxElementsInMemory="1"
-            maxElementsOnDisk="0"
-            overflowToDisk="true"
-            diskPersistent="true"
-            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>

+ 4 - 4
themis-business/pom.xml

@@ -36,10 +36,10 @@
 			<artifactId>poi</artifactId>
 		</dependency>
 		<!-- ehcache 缓存 -->
-		<dependency>
-			<groupId>net.sf.ehcache</groupId>
-			<artifactId>ehcache</artifactId>
-		</dependency>
+<!--		<dependency>-->
+<!--			<groupId>net.sf.ehcache</groupId>-->
+<!--			<artifactId>ehcache</artifactId>-->
+<!--		</dependency>-->
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-cache</artifactId>

+ 8 - 6
themis-business/src/main/java/com/qmth/themis/business/constant/SystemConstant.java

@@ -41,18 +41,20 @@ public class SystemConstant {
     public static final String EXPORT_INIT = "准备开始处理导出数据";
     public static final String RECORD_ID = "recordId";
     public static final String MESSAGE = "message";
+    public static final String ADMIN = "ADMIN";
     public static final String BREACH_STATUS = "breachStatus";
     public static final String USER = "user:";
     public static final String STUDENT = "student:";
     public static final String SESSION = "session:";
     public static final String ORG = "org:cache:";
+    public static final String ROLE = "role:cache";
     public static final String LINK = "LINK";
+    public static final String SYSADMIN = "系统管理员";
     public static final String ALL_PATH = "/**";
     public static final String ADMIN_ALL_PATH = "/api/admin/**";
     public static final String THIRD_ALL_PATH = "/api/open/**";
     public static final String ACCOUNT = "account";
     public static final String STUDENT_ACCOUNT = "student";
-    public static final String SYSTEM_ACCOUNT = "sysadmin";
     public static final String COUNT = "count";
     public static final String ERROR = "/error";
     public static final String MQ_TOPIC_BUFFER_LIST = "mq:topic:buffer:list";
@@ -176,11 +178,11 @@ public class SystemConstant {
     public static final String userOauth = "user:oauth:cache";
     public static final String studentOauth = "student:oauth:cache";
     public static final String configCache = "config:cache";
-    /**
-     * ehcache配置
-     */
-    public static final String orgCodeCache = "org_code_cache";
-    public static final String roleCache = "role_cache";
+//    /**
+//     * ehcache配置
+//     */
+//    public static final String orgCodeCache = "org_code_cache";
+//    public static final String roleCache = "role_cache";
 
     static {
         String[] strs = delayLevel.split(",");

+ 1 - 1
themis-business/src/main/java/com/qmth/themis/business/service/CacheService.java

@@ -3,7 +3,7 @@ package com.qmth.themis.business.service;
 import com.qmth.themis.business.dto.AuthDto;
 
 /**
- * @Description: ehcache 服务类
+ * @Description: 缓存 服务类
  * @Param:
  * @return:
  * @Author: wangliang

+ 1 - 1
themis-business/src/main/java/com/qmth/themis/business/service/impl/CacheServiceImpl.java

@@ -23,7 +23,7 @@ import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
- * @Description: ehcache 服务实现类
+ * @Description: 缓存 服务实现类
  * @Param:
  * @return:
  * @Author: wangliang

+ 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);
-    }
-}

+ 48 - 0
themis-business/src/main/java/com/qmth/themis/business/util/RedisUtil.java

@@ -44,6 +44,45 @@ public class RedisUtil {
         redisTemplate.opsForValue().set(SystemConstant.ORG + orgId, o, SystemConstant.REDIS_EXPIRE_TIME, TimeUnit.SECONDS);
     }
 
+    /**
+     * 获取机构信息
+     *
+     * @param orgCode
+     * @return
+     */
+    public Object getOrgCode(String orgCode) {
+        return redisTemplate.opsForValue().get(SystemConstant.ORG + orgCode);
+    }
+
+    /**
+     * 设置机构信息
+     *
+     * @param o
+     */
+    public void setRole(Object o) {
+        redisTemplate.opsForList().leftPushAll(SystemConstant.ROLE, o);
+        redisTemplate.expire(SystemConstant.ROLE, SystemConstant.REDIS_EXPIRE_TIME, TimeUnit.SECONDS);
+    }
+
+    /**
+     * 获取机构信息
+     *
+     * @return
+     */
+    public Object getRole() {
+        return redisTemplate.opsForList().range(SystemConstant.ROLE, 0, -1);
+    }
+
+    /**
+     * 设置机构信息
+     *
+     * @param orgCode
+     * @param o
+     */
+    public void setOrgCode(String orgCode, Object o) {
+        redisTemplate.opsForValue().set(SystemConstant.ORG + orgCode, o, SystemConstant.REDIS_EXPIRE_TIME, TimeUnit.SECONDS);
+    }
+
     /**
      * 删除机构缓存
      *
@@ -53,6 +92,15 @@ public class RedisUtil {
         redisTemplate.delete(SystemConstant.ORG + orgId);
     }
 
+    /**
+     * 删除机构缓存
+     *
+     * @param orgId
+     */
+    public void deleteOrgCode(String orgCode) {
+        redisTemplate.delete(SystemConstant.ORG + orgCode);
+    }
+
     /**
      * 获取用户信息
      *