|
@@ -1,87 +0,0 @@
|
|
|
-package com.persagy.dmp.common.handler;
|
|
|
-
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.json.JSONObject;
|
|
|
-import com.persagy.dmp.common.context.AppContext;
|
|
|
-import com.persagy.dmp.common.helper.SpringHelper;
|
|
|
-import com.persagy.dmp.common.constant.CommonConstant;
|
|
|
-import org.springframework.web.servlet.ModelAndView;
|
|
|
-import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-
|
|
|
- * 上下文 拦截器
|
|
|
- * @author Charlie Yu
|
|
|
- * @date 2021-06-25
|
|
|
- */
|
|
|
-public class AppContextHandler extends HandlerInterceptorAdapter {
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
-
|
|
|
- if(StrUtil.isNotBlank(request.getHeader("token"))) {
|
|
|
- accessTokenInfo(request.getHeader("token"));
|
|
|
- } else if(StrUtil.isNotBlank(request.getParameter("groupCode"))) {
|
|
|
-
|
|
|
- accessRequestInfo(request);
|
|
|
- } else {
|
|
|
-
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 根据token获取上下文信息
|
|
|
- * @param token
|
|
|
- */
|
|
|
- private void accessTokenInfo(String token){
|
|
|
-
|
|
|
- boolean enabled = SpringHelper.getBoolean("persagy.common.token.enabled", true);
|
|
|
- if(!enabled) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- JSONObject tokenObj = new JSONObject();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- String groupCode = tokenObj.getStr("groupCode");
|
|
|
- String appId = tokenObj.getStr("appId");
|
|
|
- String accountId = tokenObj.getStr("accountId");
|
|
|
- AppContext.getContext().setGroupCode(groupCode);
|
|
|
- AppContext.getContext().setAppId(appId);
|
|
|
- AppContext.getContext().setAccountId(accountId);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 从请求参数中获取上下文
|
|
|
- * @param request
|
|
|
- */
|
|
|
- private void accessRequestInfo(HttpServletRequest request) {
|
|
|
- AppContext.getContext().setGroupCode(request.getParameter("groupCode"));
|
|
|
- AppContext.getContext().setProjectId(request.getParameter("projectId"));
|
|
|
- AppContext.getContext().setAppId(request.getParameter("appId"));
|
|
|
- String userId = request.getParameter("userId");
|
|
|
-
|
|
|
- if(StrUtil.isBlank(userId)) {
|
|
|
- userId = CommonConstant.DEFAULT_ID;
|
|
|
- }
|
|
|
- AppContext.getContext().setAccountId(userId);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
|
|
- AppContext.unload();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
|
|
- AppContext.unload();
|
|
|
- }
|
|
|
-}
|