package com.miniframe.service; import java.util.HashMap; import java.util.Map; import com.miniframe.bisiness.service.LoginService; import com.miniframe.core.exception.BaseException; import com.miniframe.core.exception.BusinessException; import com.miniframe.core.exception.CommException; import com.miniframe.core.ext.UtilTools; import com.miniframe.httpproxy.ProxyInterface; import com.miniframe.httpproxy.ProxyRetunTransInfo; import com.miniframe.httpproxy.ProxySendTransInfo; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpRequest; /** * 上送的Map中,值部分可能存在数组,需要动态反射判断 * * @author tianyubing * */ @SuppressWarnings({"rawtypes","unchecked"}) public class PreHttpRouteProxy implements ProxyInterface { private static String transCodekey = "transCode"; private static String clientTokenkey = "clientToken"; private static String userIdkey = "userId"; /** * 返回空表示前处理,没有拦截,所以正常需要返回null,异常返回结果或抛异常。 */ @Override public ProxyRetunTransInfo exec(ProxySendTransInfo sendinfo) throws CommException { try { Map sendMap = new HashMap(); if(sendinfo.getTransObj()!=null) { Map map =(Map) sendinfo.getTransObj(); sendMap.putAll(map); } // 如果令牌在HttpHead上不用解包,直接处理,否则需要解包 if (sendinfo.getHeadMap() != null) { sendMap.putAll(sendinfo.getHeadMap()); } Object clientToken = sendMap.get(clientTokenkey); clientToken = sendMap.get(clientTokenkey); // 获取交易码 Object transCode = sendMap.get(transCodekey); Object userId=sendMap.get(userIdkey); // // 获取验证图片和登录交易不处理 // if (transCode.equals("HM0000") || transCode.equals("HM0001") || transCode.equals("HM0002")) { // return null; // } String checkUserId = LoginService.checkSecurity((String) clientToken, (String) transCode); if(UtilTools.isNullOrBlank(checkUserId)){ //系统没有登录或会话超时! throw new BusinessException("EB8000006"); } } catch (Exception ee) { if(ee instanceof BusinessException){ BusinessException be=(BusinessException)ee; throw new CommException(be.getErrCode()); } else if (ee instanceof BaseException) { BaseException be = (BaseException) ee; throw new CommException("错误码:" + be.getErrCode() + ",错误信息:" + be.getErrMsg()); } else { throw new CommException(ee.getMessage()); } } //如果需要修改报文,需要将解包后的Map处理完数据后再打包成json或xml,继续发送 //......................... Map headMap=sendinfo.getHeadMap(); Map transMap =(Map) sendinfo.getTransObj(); String uri = UtilTools.getHttpRequest().uri(); //转发去掉userId,否则会修改数据库的userId if(transMap!=null && transMap.containsKey(userIdkey)) { transMap.remove(userIdkey); sendinfo.setTransObj(transMap); } if(uri!=null && uri.contains(userIdkey+"=")){ uri=uri.replace(userIdkey+"=",userIdkey+"NotUse="); sendinfo.setUri(uri); } //根据转发信息修改报文头 if(uri.contains("/managersvr/manager/cae/TransServlet")){ String projectName="cae"; String serviceName="service"; String authUserId="manager"; headMap.put("x-auth-user",authUserId); headMap.put("x-auth-token",AccessClient.getToken(projectName,serviceName,authUserId)); sendinfo.setHeadMap(headMap); } return null; } }