Browse Source

校验token拦截器

dengsixing 2 years ago
parent
commit
a4fb48f7b8

+ 57 - 0
src/main/java/com/fdkankan/scene/Interceptor/TokenInterceptor.java

@@ -0,0 +1,57 @@
+package com.fdkankan.scene.Interceptor;
+
+
+import com.alibaba.fastjson.JSON;
+import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.redis.constant.RedisKey;
+import com.fdkankan.redis.util.RedisUtil;
+import com.fdkankan.web.response.ResultData;
+import java.io.IOException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.HandlerInterceptor;
+
+@Component
+@Slf4j
+public class TokenInterceptor implements HandlerInterceptor {
+
+	@Autowired
+	private RedisUtil redisUtil;
+
+	@Override
+	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+		response.setContentType("text/html;charset=UTF-8");
+		String token = request.getHeader("token");
+		if(StringUtils.isEmpty(token)){
+			this.needLogin(request,response);
+			return false;
+		}
+		try {
+			String redisKey = String.format(RedisKey.USER_TOKEN_KEY, token);
+			if(redisUtil.hasKey(redisKey)){
+				redisUtil.expire(redisKey,2 * 60 * 60);
+				return true;
+			}
+		}catch (Exception e){
+			log.error("校验token缓存出错", e);
+		}
+		this.needLogin(request,response);
+		return false;
+	}
+
+	private void needLogin(HttpServletRequest request, HttpServletResponse response) {
+		try {
+			ResultData error = ResultData.error(ErrorCode.TOKEN_NOT_FOUND);
+			response.getWriter().append(JSON.toJSONString(error));
+		} catch (IOException e) {
+			log.info("LoginInterceptor|needLogin|IOException|" + e);
+			e.printStackTrace();
+		}
+	}
+
+}
+

+ 38 - 0
src/main/java/com/fdkankan/scene/config/InterceptorConfig.java

@@ -0,0 +1,38 @@
+package com.fdkankan.scene.config;
+
+import com.fdkankan.scene.Interceptor.TokenInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+
+@Configuration
+public class InterceptorConfig implements WebMvcConfigurer {
+
+	@Autowired
+	private TokenInterceptor tokenInterceptor;
+
+	@Override
+	public void addCorsMappings(CorsRegistry registry) {
+		registry.addMapping("/**").allowCredentials(true).allowedHeaders("*").allowedOrigins("*").allowedMethods("*");
+
+	}
+
+	@Override
+	public void addInterceptors(InterceptorRegistry registry) {
+		registry.addInterceptor(tokenInterceptor).addPathPatterns("/service/scene/edit/**");
+//				.excludePathPatterns("/**/sso/**","/app/**","/test/**","/**/inner/**")
+
+		WebMvcConfigurer.super.addInterceptors(registry);
+	}
+
+	@Override
+	public void addResourceHandlers(ResourceHandlerRegistry registry) {
+		WebMvcConfigurer.super.addResourceHandlers(registry);
+	}
+
+}
+

+ 0 - 21
src/main/java/com/fdkankan/scene/controller/PicSceneProgressController.java

@@ -1,21 +0,0 @@
-package com.fdkankan.scene.controller;
-
-
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * 视频重算进度 前端控制器
- * </p>
- *
- * @author 
- * @since 2022-01-14
- */
-@RestController
-@RequestMapping("/scene/picSceneProgress")
-public class PicSceneProgressController {
-
-}
-

+ 0 - 23
src/main/java/com/fdkankan/scene/controller/SceneCooperationController.java

@@ -1,23 +0,0 @@
-package com.fdkankan.scene.controller;
-
-
-import com.fdkankan.web.controller.BaseController;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- *  前端控制器
- * </p>
- *
- * @author dengsixing
- * @since 2021-12-23
- */
-@RestController
-@RequestMapping("/service/scene/user/scene/cooperation")
-public class SceneCooperationController extends BaseController {
-
-
-
-}
-

+ 0 - 21
src/main/java/com/fdkankan/scene/controller/SceneDataDownloadController.java

@@ -1,21 +0,0 @@
-package com.fdkankan.scene.controller;
-
-
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * 安居客场景数据下载 前端控制器
- * </p>
- *
- * @author dengsixing
- * @since 2021-12-23
- */
-@RestController
-@RequestMapping("/scene/sceneDataDownload")
-public class SceneDataDownloadController {
-
-}
-

+ 0 - 21
src/main/java/com/fdkankan/scene/controller/SceneProEditV3Controller.java

@@ -1,21 +0,0 @@
-package com.fdkankan.scene.controller;
-
-
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * pro场景编辑数据表 前端控制器
- * </p>
- *
- * @author 
- * @since 2022-04-22
- */
-@RestController
-@RequestMapping("/scene/sceneProEditV3")
-public class SceneProEditV3Controller {
-
-}
-

+ 0 - 21
src/main/java/com/fdkankan/scene/controller/SceneProV3Controller.java

@@ -1,21 +0,0 @@
-package com.fdkankan.scene.controller;
-
-
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * pro场景表 前端控制器
- * </p>
- *
- * @author 
- * @since 2022-04-22
- */
-@RestController
-@RequestMapping("/scene/sceneProV3")
-public class SceneProV3Controller {
-
-}
-

+ 0 - 21
src/main/java/com/fdkankan/scene/controller/SceneResourceController.java

@@ -1,21 +0,0 @@
-package com.fdkankan.scene.controller;
-
-
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * 场景资源表 前端控制器
- * </p>
- *
- * @author dengsixing
- * @since 2021-12-23
- */
-@RestController
-@RequestMapping("/scene/sceneResource")
-public class SceneResourceController {
-
-}
-

+ 0 - 21
src/main/java/com/fdkankan/scene/controller/SceneResourceCooperationController.java

@@ -1,21 +0,0 @@
-package com.fdkankan.scene.controller;
-
-
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * 场景资源和协作用户关联表 前端控制器
- * </p>
- *
- * @author dengsixing
- * @since 2021-12-23
- */
-@RestController
-@RequestMapping("/scene/sceneResourceCooperation")
-public class SceneResourceCooperationController {
-
-}
-

+ 0 - 126
src/main/java/com/fdkankan/scene/controller/V3Controller.java

@@ -1,126 +0,0 @@
-package com.fdkankan.scene.controller;
-
-import com.fdkankan.web.response.ResultData;
-import com.fdkankan.scene.service.ISceneEditInfoService;
-import com.fdkankan.scene.service.IScenePlusService;
-import com.fdkankan.scene.service.ISceneUpgradeToV4Service;
-import com.fdkankan.scene.vo.MoveSceneParamVO;
-import com.fdkankan.scene.vo.UnbindOrBindCameraParamVO;
-import com.fdkankan.scene.vo.UpgradeToV4ParamVO;
-import com.fdkankan.web.controller.BaseController;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * TODO
- * </p>
- *
- * @author dengsixing
- * @since 2022/5/20
- **/
-@RestController
-@RequestMapping("/service-v3/scene")
-public class V3Controller extends BaseController {
-
-    @Autowired
-    private ISceneUpgradeToV4Service sceneUpgradeToV4Service;
-    @Autowired
-    private ISceneEditInfoService sceneEditInfoService;
-    @Autowired
-    private IScenePlusService scenePlusService;
-
-//    /**
-//     * <p>
-//     场景升级
-//     此接口用于V3\V4系统并存过渡期,场景升级v4做数据迁移用,待V3关闭可以删除
-//     * </p>
-//     * @author dengsixing
-//     * @date 2022/4/21
-//     * @return com.fdkankan.web.response.ResultData
-//     **/
-//    @PostMapping(value = "/upgradeToV4")
-//    public ResultData upgradeToV4(@RequestBody @Validated UpgradeToV4ParamVO param){
-//        ResultData resultData = sceneUpgradeToV4Service.upgradeToV4(param);
-//        return resultData;
-//    }
-
-    /**
-     * <p>
-     场景升级进度查询
-     此接口用于V3\V4系统并存过渡期,场景升级v4做数据迁移用,待V3关闭可以删除
-     * </p>
-     * @author dengsixing
-     * @date 2022/4/21
-     * @return com.fdkankan.web.response.ResultData
-     **/
-    @GetMapping(value = "/getUpgradeToV4Progress")
-    public ResultData getUpgradeToV4Progress(@RequestParam(value = "num") String num){
-        ResultData resultData = sceneUpgradeToV4Service.getUpgradeToV4Progress(num);
-        return resultData;
-    }
-
-    /**
-     * <p>
-     查询场景scenejson
-     此接口用于V3\V4系统并存过渡期,待V3关闭可以删除
-     * </p>
-     * @author dengsixing
-     * @date 2022/4/21
-     * @return com.fdkankan.web.response.ResultData
-     **/
-    @GetMapping(value = "/getSceneViewInfo")
-    public ResultData getSceneViewInfo(@RequestParam(value = "num") String num){
-        return sceneUpgradeToV4Service.getSceneViewInfo(num);
-    }
-
-    /**
-     * <p>
-            v3相机解绑和绑定,需要同步v4场景的userid
-     * </p>
-     * @author dengsixing
-     * @date 2022/6/23
-     * @return com.fdkankan.web.response.ResultData
-     **/
-    @PostMapping(value = "/updateScenePlusUserIdByCameraId")
-    public ResultData updateScenePlusUserIdByCameraId(@RequestBody @Validated UnbindOrBindCameraParamVO param){
-        return scenePlusService.updateScenePlusUserIdByCameraId(param.getCameraIdList(), param.getUserId());
-    }
-
-    /**
-     * <p>
-            场景迁移,修改相机id和用户id
-     * </p>
-     * @author dengsixing
-     * @date 2022/6/23
-     * @return com.fdkankan.web.response.ResultData
-     **/
-    @PostMapping(value = "/moveScene")
-    public ResultData moveScene(
-        @RequestBody @Validated MoveSceneParamVO param){
-        return scenePlusService.moveScene(param.getNumList(), param.getCameraId(), param.getUserId());
-    }
-
-//    /**
-//     * <p>
-//         场景拷贝
-//     * </p>
-//     * @author tianboguang
-//     * @date 2022/6/29
-//     * @return com.fdkankan.web.response.ResultData
-//     **/
-//    @PostMapping(value = "/copyScene")
-//    public ResultData copyScene(
-//            @RequestBody @Validated CopySceneParamVO param) throws Exception {
-//        sceneService.copyScene(param);
-//        return ResultData.ok();
-//    }
-
-
-}

+ 0 - 21
src/main/java/com/fdkankan/scene/controller/VideoSceneProgressController.java

@@ -1,21 +0,0 @@
-package com.fdkankan.scene.controller;
-
-
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * 视频重算进度 前端控制器
- * </p>
- *
- * @author 
- * @since 2022-01-14
- */
-@RestController
-@RequestMapping("/scene/videoSceneProgress")
-public class VideoSceneProgressController {
-
-}
-