PreHttpRouteProxy.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.miniframe.service;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import com.miniframe.bisiness.service.LoginService;
  5. import com.miniframe.core.exception.BaseException;
  6. import com.miniframe.core.exception.BusinessException;
  7. import com.miniframe.core.exception.CommException;
  8. import com.miniframe.core.ext.UtilTools;
  9. import com.miniframe.httpproxy.ProxyInterface;
  10. import com.miniframe.httpproxy.ProxyRetunTransInfo;
  11. import com.miniframe.httpproxy.ProxySendTransInfo;
  12. import io.netty.handler.codec.http.HttpHeaders;
  13. import io.netty.handler.codec.http.HttpRequest;
  14. /**
  15. * 上送的Map中,值部分可能存在数组,需要动态反射判断
  16. *
  17. * @author tianyubing
  18. *
  19. */
  20. @SuppressWarnings({"rawtypes","unchecked"})
  21. public class PreHttpRouteProxy implements ProxyInterface {
  22. private static String transCodekey = "transCode";
  23. private static String clientTokenkey = "clientToken";
  24. private static String userIdkey = "userId";
  25. /**
  26. * 返回空表示前处理,没有拦截,所以正常需要返回null,异常返回结果或抛异常。
  27. */
  28. @Override
  29. public ProxyRetunTransInfo exec(ProxySendTransInfo sendinfo) throws CommException {
  30. try {
  31. Map sendMap = new HashMap();
  32. if(sendinfo.getTransObj()!=null) {
  33. Map map =(Map) sendinfo.getTransObj();
  34. sendMap.putAll(map);
  35. }
  36. // 如果令牌在HttpHead上不用解包,直接处理,否则需要解包
  37. if (sendinfo.getHeadMap() != null) {
  38. sendMap.putAll(sendinfo.getHeadMap());
  39. }
  40. Object clientToken = sendMap.get(clientTokenkey);
  41. clientToken = sendMap.get(clientTokenkey);
  42. // 获取交易码
  43. Object transCode = sendMap.get(transCodekey);
  44. Object userId=sendMap.get(userIdkey);
  45. // // 获取验证图片和登录交易不处理
  46. // if (transCode.equals("HM0000") || transCode.equals("HM0001") || transCode.equals("HM0002")) {
  47. // return null;
  48. // }
  49. String checkUserId = LoginService.checkSecurity((String) clientToken, (String) transCode);
  50. if(UtilTools.isNullOrBlank(checkUserId)){
  51. //系统没有登录或会话超时!
  52. throw new BusinessException("EB8000006");
  53. }
  54. } catch (Exception ee) {
  55. if(ee instanceof BusinessException){
  56. BusinessException be=(BusinessException)ee;
  57. throw new CommException(be.getErrCode());
  58. }
  59. else if (ee instanceof BaseException) {
  60. BaseException be = (BaseException) ee;
  61. throw new CommException("错误码:" + be.getErrCode() + ",错误信息:" + be.getErrMsg());
  62. } else {
  63. throw new CommException(ee.getMessage());
  64. }
  65. }
  66. //如果需要修改报文,需要将解包后的Map处理完数据后再打包成json或xml,继续发送
  67. //.........................
  68. Map headMap=sendinfo.getHeadMap();
  69. Map transMap =(Map) sendinfo.getTransObj();
  70. String uri = UtilTools.getHttpRequest().uri();
  71. //转发去掉userId,否则会修改数据库的userId
  72. if(transMap!=null && transMap.containsKey(userIdkey)) {
  73. transMap.remove(userIdkey);
  74. sendinfo.setTransObj(transMap);
  75. }
  76. if(uri!=null && uri.contains(userIdkey+"=")){
  77. uri=uri.replace(userIdkey+"=",userIdkey+"NotUse=");
  78. sendinfo.setUri(uri);
  79. }
  80. //根据转发信息修改报文头
  81. if(uri.contains("/managersvr/manager/cae/TransServlet")){
  82. String projectName="cae";
  83. String serviceName="service";
  84. String authUserId="manager";
  85. headMap.put("x-auth-user",authUserId);
  86. headMap.put("x-auth-token",AccessClient.getToken(projectName,serviceName,authUserId));
  87. sendinfo.setHeadMap(headMap);
  88. }
  89. return null;
  90. }
  91. }