浏览代码

获取用户信息

yanguofeng 2 年之前
父节点
当前提交
0d8e96d476

+ 5 - 5
adm-business/adm-server/src/main/java/com/persagy/adm/server/custom/controller/AdmFileController.java

@@ -3,11 +3,11 @@ package com.persagy.adm.server.custom.controller;
 
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.persagy.adm.server.auth.service.AdmAuthService;
 import com.persagy.adm.server.custom.dto.CadFiles;
 import com.persagy.adm.server.custom.entity.CadFileQueryParam;
 import com.persagy.adm.server.custom.entity.db.AdmCad;
 import com.persagy.adm.server.custom.enums.MajorEnum;
+import com.persagy.adm.server.custom.interceptor.TokenInterceptor;
 import com.persagy.adm.server.custom.service.IAdmCadService;
 import com.persagy.dmp.common.context.AppContext;
 import com.persagy.dmp.common.model.response.CommonResult;
@@ -32,9 +32,6 @@ public class AdmFileController {
     @Autowired
     IAdmCadService iAdmCadService;
 
-    @Autowired
-    private AdmAuthService admAuthService;
-
     /**
      * CAD文件查询接口
      */
@@ -61,6 +58,9 @@ public class AdmFileController {
         AppContext context = AppContext.getContext();
         cadFiles.getFiles().forEach(file -> {
             AdmCad admCad = new AdmCad();
+            if (StringUtils.isNotBlank(file.getId())){
+                admCad.setId(file.getId());
+            }
             admCad.setGroupCode(context.getGroupCode());
             admCad.setProjectId(context.getProjectId());
             admCad.setBuildingId(cadFiles.getBuildingId());
@@ -70,7 +70,7 @@ public class AdmFileController {
             admCad.setCheckStatus(false);
             admCad.setFileKey(file.getFileKey());
             admCad.setFileName(file.getFileName());
-            admCad.setCreator(admAuthService.loginInfo().getName());
+            admCad.setCreator(TokenInterceptor.bossAuthUserThreadLocal.get().getName());
             admFiles.add(admCad);
         });
         iAdmCadService.saveOrUpdateBatch(admFiles);

+ 7 - 10
adm-business/adm-server/src/main/java/com/persagy/adm/server/custom/controller/AppController.java

@@ -1,7 +1,6 @@
 package com.persagy.adm.server.custom.controller;
 
 import cn.hutool.core.util.StrUtil;
-import com.persagy.adm.server.auth.service.AdmAuthService;
 import com.persagy.adm.server.custom.common.AdmRequest;
 import com.persagy.adm.server.custom.datatx.DataTxHandler;
 import com.persagy.adm.server.custom.dto.HasUpdateReq;
@@ -9,6 +8,7 @@ import com.persagy.adm.server.custom.entity.ConfigData;
 import com.persagy.adm.server.custom.entity.Dict;
 import com.persagy.adm.server.custom.entity.DownLoadData;
 import com.persagy.adm.server.custom.entity.UploadRtn;
+import com.persagy.adm.server.custom.interceptor.TokenInterceptor;
 import com.persagy.adm.server.custom.service.ISyncApp;
 import com.persagy.adm.server.custom.service.ServiceUtil;
 import com.persagy.dmp.auth.domain.BossAuthUser;
@@ -43,9 +43,6 @@ public class AppController {
 	@Autowired
 	private DataTxHandler dataTxHandler;
 
-	@Autowired
-	private AdmAuthService admAuthService;
-
 	@RequestMapping("/clientId")
 	public CommonResult<Map<String, Object>> clientId(@RequestBody AdmRequest req){
 		HashMap<String, Object> rtn = new HashMap<>();
@@ -55,25 +52,25 @@ public class AppController {
 
 	@RequestMapping("/dict")
 	public CommonResult<Dict> dict(@RequestBody AdmRequest req){
-		req.setUserId(admAuthService.loginInfo().getId());
+		req.setUserId(TokenInterceptor.bossAuthUserThreadLocal.get().getId());
 		return ResultHelper.single(syncApp.downloadDict(req.getGroupCode(), req.getProjectId(), req.getUserId()));
 	}
 
 	@RequestMapping("/config")
 	public CommonResult<ConfigData> config(@RequestBody AdmRequest req){
-		req.setUserId(admAuthService.loginInfo().getId());
+		req.setUserId(TokenInterceptor.bossAuthUserThreadLocal.get().getId());
 		return ResultHelper.single(syncApp.downloadConfig(req.getGroupCode(), req.getProjectId(), req.getUserId()));
 	}
 
 	@RequestMapping("/frame")
 	public CommonResult<Map<String, Object>> frame(@RequestBody AdmRequest req){
-		req.setUserId(admAuthService.loginInfo().getId());
+		req.setUserId(TokenInterceptor.bossAuthUserThreadLocal.get().getId());
 		return ResultHelper.single(syncApp.downloadFrameData(req.getGroupCode(), req.getProjectId(), req.getUserId()));
 	}
 
 	@RequestMapping("/download")
 	public CommonResult<DownLoadData> download(@RequestBody AdmRequest req){
-		req.setUserId(admAuthService.loginInfo().getId());
+		req.setUserId(TokenInterceptor.bossAuthUserThreadLocal.get().getId());
 		DownLoadData data;
 		if(StrUtil.isNotBlank(req.getBuildingId())) {
 			data = syncApp.downloadBuildingData(req.getGroupCode(), req.getProjectId(), req.getUserId(), req.getClientId(), req.getBuildingId(), req.getBdtpDownloadTs(), req.getAdmDownloadTs());
@@ -88,7 +85,7 @@ public class AppController {
 	 */
 	@RequestMapping("/downloadObjRel")
 	public CommonResult<DownLoadData> downloadObjRel(@RequestBody AdmRequest req){
-		req.setUserId(admAuthService.loginInfo().getId());
+		req.setUserId(TokenInterceptor.bossAuthUserThreadLocal.get().getId());
 		return ResultHelper.single(syncApp.downloadProjectData(req.getGroupCode(), req.getProjectId(), req.getUserId(), req.getClientId(), req.getBdtpDownloadTs(), req.getAdmDownloadTs()));
 	}
 
@@ -100,7 +97,7 @@ public class AppController {
 	@RequestMapping("/upload")
 	public CommonResult<UploadRtn> upload(HttpServletRequest request){
 		AdmRequest admRequest = dataTxHandler.handleRequest(request);
-		BossAuthUser bossAuthUser = admAuthService.loginInfo();
+		BossAuthUser bossAuthUser = TokenInterceptor.bossAuthUserThreadLocal.get();
 		log.info("用户:{},上传数据:{}",bossAuthUser,admRequest);
 		return ResultHelper.single(syncApp.uploadData(admRequest.getUploadData(), admRequest.getGroupCode(), admRequest.getProjectId(), admRequest.getUserId(), admRequest.getClientId()));
 	}

+ 1 - 0
adm-business/adm-server/src/main/java/com/persagy/adm/server/custom/entity/CadFileInfo.java

@@ -6,6 +6,7 @@ import javax.validation.constraints.NotBlank;
 
 @Data
 public class CadFileInfo {
+    private String id;
     @NotBlank(message = "fileKey不能为空")
     private String fileKey;
     @NotBlank(message = "fileName不能为空")

+ 6 - 0
adm-business/adm-server/src/main/java/com/persagy/adm/server/custom/interceptor/AppContextConfig.java

@@ -1,5 +1,6 @@
 package com.persagy.adm.server.custom.interceptor;
 
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@@ -8,6 +9,9 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 @Component
 public class AppContextConfig implements WebMvcConfigurer {
 
+    @Autowired
+    TokenInterceptor tokenInterceptor;
+
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
         InterceptorRegistration registration = registry.addInterceptor(new AppContextInterceptor());
@@ -16,5 +20,7 @@ public class AppContextConfig implements WebMvcConfigurer {
         InterceptorRegistration registration1 = registry.addInterceptor(new TraceIdInterceptor());
         registration1.addPathPatterns("/**");
 
+        registry.addInterceptor(tokenInterceptor).addPathPatterns("/adm/**");
+
     }
 }

+ 39 - 0
adm-business/adm-server/src/main/java/com/persagy/adm/server/custom/interceptor/TokenInterceptor.java

@@ -0,0 +1,39 @@
+package com.persagy.adm.server.custom.interceptor;
+
+import com.persagy.adm.server.auth.service.AdmAuthService;
+import com.persagy.dmp.auth.domain.BossAuthUser;
+import com.persagy.dmp.common.constant.ResponseCode;
+import com.persagy.dmp.common.exception.BusinessException;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@Component
+@Slf4j
+public class TokenInterceptor extends HandlerInterceptorAdapter {
+    public static ThreadLocal<BossAuthUser> bossAuthUserThreadLocal = new ThreadLocal<>();
+
+    @Autowired
+    private AdmAuthService admAuthService;
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        String token = request.getHeader("token");
+        if (StringUtils.isBlank(token)){
+            throw new BusinessException(ResponseCode.A0220.getCode(),ResponseCode.A0220.getDesc());
+        }
+
+        try {
+            BossAuthUser bossAuthUser = admAuthService.loginInfo();
+            bossAuthUserThreadLocal.set(bossAuthUser);
+        }catch (Exception e){
+            log.error("获取用户信息失败",e);
+            throw new BusinessException(ResponseCode.A0311.getCode(),ResponseCode.A0311.getDesc());
+        }
+        return super.preHandle(request, response, handler);
+    }
+}

+ 2 - 4
adm-business/adm-server/src/main/java/com/persagy/adm/server/custom/service/impl/SyncAppImpl.java

@@ -26,6 +26,7 @@ import com.persagy.adm.server.custom.dto.RelationDefineCollectDTO;
 import com.persagy.adm.server.custom.entity.*;
 import com.persagy.adm.server.custom.entity.db.*;
 import com.persagy.adm.server.custom.enums.DeliveryTypeEnum;
+import com.persagy.adm.server.custom.interceptor.TokenInterceptor;
 import com.persagy.adm.server.custom.service.*;
 import com.persagy.adm.server.custom.service.impl.check_update.CheckUpdateChain;
 import com.persagy.adm.server.custom.service.impl.check_update.CheckUpdateContext;
@@ -107,9 +108,6 @@ public class SyncAppImpl implements ISyncApp {
 	AdmDefineProblemInfoMapper admDefineProblemInfoMapper;
 
 	@Autowired
-	private AdmAuthService admAuthService;
-
-	@Autowired
 	public SyncAppImpl(ObjectMapper4Tx mapper4Tx) {
 		this.mapper4Tx = mapper4Tx;
 	}
@@ -453,7 +451,7 @@ public class SyncAppImpl implements ISyncApp {
 	public boolean hasUpdate(HasUpdateReq hasUpdateReq) {
 		CheckUpdateContext context = new CheckUpdateContext();
 		BeanUtil.copyProperties(hasUpdateReq,context);
-		BossAuthUser bossAuthUser = admAuthService.loginInfo();
+		BossAuthUser bossAuthUser = TokenInterceptor.bossAuthUserThreadLocal.get();
 		log.info("获取到用户信息:{}",bossAuthUser);
 		context.setUserId(bossAuthUser.getId());
 		return checkUpdateChain.doCheck(context);