package com.changhong.epc.zuul.filter; import com.changhong.epc.constter.exception.EPCServiceException; import com.changhong.epc.constter.system.PrivateRestConst; import com.iemsoft.framework.cloud.core.tools.Assert; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import com.iemsoft.framework.cloud.redis.service.CacheUtils; import com.iemsoft.framework.cloud.zuul.filter.url.UrlFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.http.ServletInputStreamWrapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.StreamUtils; import org.springframework.web.util.WebUtils; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.nio.charset.Charset; import java.util.Objects; /** * @author weihainan. * @since 0.1 created on 2017/2/23. */ @Slf4j @Service public class DecodeRequestFilter implements UrlFilter,PrivateRestConst { @Value("${tokenflag}") private String tokenFlag; public void checkLogin(HttpServletRequest request){ String currtoken = null; String openId = null; try { currtoken = Des1.desDecrypt(URLDecoder.decode(WebUtils.getCookie(request, TOKEN).getValue(),"UTF-8").replaceAll(" ","+"),Des1.SECRETKEY); openId = Des1.desDecrypt(URLDecoder.decode(WebUtils.getCookie(request, OPEN_ID).getValue(),"UTF-8").replaceAll(" ","+"),Des1.SECRETKEY); } catch (UnsupportedEncodingException|NullPointerException e) { log.error(e.getMessage(), e); } String tokenKey = String.format("token_%s" , openId); String token = CacheUtils.get(tokenKey, String.class); if(ObjectUtil.empty(token)){ return; } if(currtoken!= null && openId != null && Objects.equals("1",tokenFlag)) { log.debug("验证登陆状态---->"); log.debug("开始check-token---------:"); if (token == null || Objects.equals("", token)) { CacheUtils.del(); log.debug("登陆失效----->"); Assert.condition(true, new EPCServiceException("L0001", "登陆失效")); } else if (!Objects.equals(token, currtoken)) { log.debug("登陆被挤----->"); Assert.condition(true, new EPCServiceException("L0002", "账号在其他地点登陆")); } } } @Override public void filter(HttpServletRequest httpServletRequest) { checkLogin(httpServletRequest); run(); } @Override public String getRegExp() { return "http://[^:]+(:\\d+)?/epc/epc-[^-]+/.+"; } public String decodeBody(String body) { log.debug("body解密前:"+body); String rebody; if(body.indexOf('{') == 0){ rebody = body; }else { rebody = Des1.desDecrypt(body, Des1.SECRETKEY); } log.debug("body解密后:"+rebody); if(rebody!=null && !Objects.equals("",rebody)){ // System.out.println("进入:"+rebody.indexOf("=0, L0008); } return rebody; } public static void main(String... args){ System.out.println( new DecodeRequestFilter().decodeBody("{\"creatUser\":\"王鑫鑫\",\"fields\":{\"C_Type\":\" longtext \",\"agent\":\" varchar(150) \",\"bankl\":\" varchar(150) \",\"单行文本24\":\" varchar(150) \",\"单行文本30\":\" varchar(150) \",\"EmployeePosition\":\" varchar(150) \",\"zracct\":\" varchar(150) \",\"EmployeeNumber\":\" varchar(150) \",\"payWay\":\" longtext \",\"OutstandingAmount\":\" varchar(30) \",\"ApplicationAmount\":\" varchar(30) \",\"Remark\":\" varchar(150) \",\"expendText\":\" varchar(150) \",\"creditItem\":\" varchar(150) \",\"number_LoanCode\":\" varchar(50) \",\"zrcvert\":\" varchar(150) \",\"debitItem\":\" varchar(150) \",\"zzjjhh\":\" varchar(150) \",\"loanDescription\":\" varchar(150) \",\"alreadyRepaid\":\" varchar(30) \",\"netMoney\":\" varchar(30) \",\"currency\":\" longtext \",\"department\":\" longtext \",\"LoanPeople\":\" longtext \"},\"flag\":\"create\",\"formDataNum\":0,\"formId\":\"0929709fab634dd79beae3b73709ec4b\",\"mapperID\":\"0929709fab634dd79beae3b73709ec4b\",\"tableName\":\"formdata_100000444_0929709fab634dd79beae3b73709ec4b\",\"tenantId\":\"100000444\"}") ); } public Object run() { try { RequestContext context = RequestContext.getCurrentContext(); if(Objects.toString(context.getRequest().getHeader("Content-Type"), "").indexOf("application/json") < 0 && Objects.toString(context.getRequest().getHeader("content-type"), "").indexOf("text/plain") < 0){ return null; } log.debug("Content-Type:{}", context.getRequest().getHeader("Content-Type")); InputStream in = (InputStream) context.get("requestEntity"); if (in == null) { in = context.getRequest().getInputStream(); } String body = StreamUtils.copyToString(in, Charset.forName("UTF-8")); String rebody = this.decodeBody(body); final byte[] reqBodyBytes = rebody.getBytes(); context.setRequest(new HttpServletRequestWrapper(context.getRequest()) { @Override public ServletInputStream getInputStream() throws IOException { return new ServletInputStreamWrapper(reqBodyBytes); } @Override public int getContentLength() { return reqBodyBytes.length; } @Override public long getContentLengthLong() { return reqBodyBytes.length; } }); return null; } catch (IOException e) { log.error(e.getMessage(), e); } return null; } }