AppContextHandler.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.persagy.dmp.common.handler;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.json.JSONObject;
  4. import com.persagy.dmp.common.context.AppContext;
  5. import com.persagy.dmp.common.helper.SpringHelper;
  6. import com.persagy.dmp.common.constant.CommonConstant;
  7. import org.springframework.web.servlet.ModelAndView;
  8. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. /**
  12. * 上下文 拦截器
  13. * @author Charlie Yu
  14. * @date 2021-06-25
  15. */
  16. public class AppContextHandler extends HandlerInterceptorAdapter {
  17. @Override
  18. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  19. // 运维平台token认证
  20. if(StrUtil.isNotBlank(request.getHeader("token"))) {
  21. accessTokenInfo(request.getHeader("token"));
  22. } else if(StrUtil.isNotBlank(request.getParameter("groupCode"))) {
  23. // 请求参数中包含groupCode,由于没有标识位,暂时用这种方式判断吧
  24. accessRequestInfo(request);
  25. } else {
  26. // 其他情况,按现有系统默认不验证的逻辑,其他情况就是默认放行,不必处理。最好是抛401
  27. }
  28. return true;
  29. }
  30. /**
  31. * 根据token获取上下文信息
  32. * @param token
  33. */
  34. private void accessTokenInfo(String token){
  35. // 是否启用token拦截
  36. boolean enabled = SpringHelper.getBoolean("persagy.common.token.enabled", true);
  37. if(!enabled) {
  38. return;
  39. }
  40. // 从token中解析数据
  41. // SecureAES aes = new SecureAES("63499E35378AE1B0733E3FED7F780B68", "C0E7BD39B52A15C7");
  42. JSONObject tokenObj = new JSONObject();
  43. // try {
  44. // tokenObj = aes.decryptToken(token);
  45. // } catch (UnsupportedEncodingException e) {
  46. // throw new AESDecryptException("token解析异常");
  47. // }
  48. // 获取值
  49. String groupCode = tokenObj.getStr("groupCode");
  50. String appId = tokenObj.getStr("appId");
  51. String accountId = tokenObj.getStr("accountId");
  52. AppContext.getContext().setGroupCode(groupCode);
  53. AppContext.getContext().setAppId(appId);
  54. AppContext.getContext().setAccountId(accountId);
  55. }
  56. /**
  57. * 从请求参数中获取上下文
  58. * @param request
  59. */
  60. private void accessRequestInfo(HttpServletRequest request) {
  61. AppContext.getContext().setGroupCode(request.getParameter("groupCode"));
  62. AppContext.getContext().setProjectId(request.getParameter("projectId"));
  63. AppContext.getContext().setAppId(request.getParameter("appId"));
  64. String userId = request.getParameter("userId");
  65. // 无用户时,默认为默认系统用户
  66. if(StrUtil.isBlank(userId)) {
  67. userId = CommonConstant.DEFAULT_ID;
  68. }
  69. AppContext.getContext().setAccountId(userId);
  70. }
  71. @Override
  72. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  73. AppContext.unload();
  74. }
  75. @Override
  76. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  77. AppContext.unload();
  78. }
  79. }