|
@@ -0,0 +1,90 @@
|
|
|
|
+package com.persagy.dc.common.handler;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.persagy.dc.common.context.AppContext;
|
|
|
|
+import com.persagy.dc.common.helper.SpringHelper;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+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 {
|
|
|
|
+
|
|
|
|
+ /** 忽略的url - swagger的文档不校验 */
|
|
|
|
+ private static final String[] IGNORE_URL = {".html", ".js", ".css", "/swagger-resources"};
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
|
+ String requestURI = request.getRequestURI();
|
|
|
|
+ if (StringUtils.containsAny(requestURI, IGNORE_URL)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ // 运维平台token认证
|
|
|
|
+ if(StrUtil.isNotBlank(request.getHeader("token"))) {
|
|
|
|
+ accessTokenInfo(request.getHeader("token"));
|
|
|
|
+ } else if(StrUtil.isNotBlank(request.getParameter("groupCode"))) {
|
|
|
|
+ // 请求参数中包含groupCode,由于没有标识位,暂时用这种方式判断吧
|
|
|
|
+ // TODO 需要系统用户ID
|
|
|
|
+ accessRequestInfo(request);
|
|
|
|
+ } else {
|
|
|
|
+ // 其他情况,按现有系统默认不验证的逻辑,其他情况就是默认放行,不必处理。最好是抛401
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据token获取上下文信息
|
|
|
|
+ * @param token
|
|
|
|
+ */
|
|
|
|
+ private void accessTokenInfo(String token){
|
|
|
|
+ // 是否启用token拦截
|
|
|
|
+ boolean enabled = SpringHelper.getBoolean("persagy.common.token.enabled", true);
|
|
|
|
+ if(!enabled) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ // 从token中解析数据
|
|
|
|
+// SecureAES aes = new SecureAES("63499E35378AE1B0733E3FED7F780B68", "C0E7BD39B52A15C7");
|
|
|
|
+ JSONObject tokenObj = new JSONObject();
|
|
|
|
+// try {
|
|
|
|
+// tokenObj = aes.decryptToken(token);
|
|
|
|
+// } catch (UnsupportedEncodingException e) {
|
|
|
|
+// throw new AESDecryptException("token解析异常");
|
|
|
|
+// }
|
|
|
|
+ // 获取值
|
|
|
|
+ String accountId = tokenObj.getString("accountId");
|
|
|
|
+ String groupCode = tokenObj.getString("groupCode");
|
|
|
|
+ String appId = tokenObj.getString("appId");
|
|
|
|
+ AppContext.getContext().setAccountId(accountId);
|
|
|
|
+ AppContext.getContext().setGroupCode(groupCode);
|
|
|
|
+ AppContext.getContext().setAppId(appId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 从请求参数中获取上下文
|
|
|
|
+ * @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"));
|
|
|
|
+ AppContext.getContext().setAccountId(request.getParameter("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();
|
|
|
|
+ }
|
|
|
|
+}
|