| @@ -15,9 +15,11 @@ import org.springframework.context.annotation.Bean; | |||||
| import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.http.converter.HttpMessageConverter; | import org.springframework.http.converter.HttpMessageConverter; | ||||
| import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||||
| import org.springframework.web.filter.CharacterEncodingFilter; | |||||
| import org.springframework.web.method.support.HandlerMethodArgumentResolver; | import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||||
| import org.springframework.web.servlet.config.annotation.*; | import org.springframework.web.servlet.config.annotation.*; | ||||
| import javax.servlet.Filter; | |||||
| import java.math.BigDecimal; | import java.math.BigDecimal; | ||||
| import java.math.BigInteger; | import java.math.BigInteger; | ||||
| import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||||
| @@ -1,10 +1,12 @@ | |||||
| package com.iformall.controller; | package com.iformall.controller; | ||||
| import com.alibaba.fastjson.JSON; | |||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.utils.HttpUtil; | import com.iformall.utils.HttpUtil; | ||||
| import com.iformall.utils.sign.SignUtils; | import com.iformall.utils.sign.SignUtils; | ||||
| import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.web.bind.annotation.*; | import org.springframework.web.bind.annotation.*; | ||||
| @@ -14,7 +16,7 @@ import java.util.HashMap; | |||||
| import java.util.Map; | import java.util.Map; | ||||
| @RestController | @RestController | ||||
| @RequestMapping("/api/userInfo") | |||||
| @RequestMapping("api/userInfo") | |||||
| @Api(description = "会员相关接口") | @Api(description = "会员相关接口") | ||||
| public class UserBasicInfoController extends BaseController { | public class UserBasicInfoController extends BaseController { | ||||
| @@ -24,15 +26,14 @@ public class UserBasicInfoController extends BaseController { | |||||
| /** | /** | ||||
| * 用户注册 | * 用户注册 | ||||
| * | * | ||||
| * @param map | |||||
| * @param | |||||
| * @return | * @return | ||||
| */ | */ | ||||
| @PostMapping("/register") | @PostMapping("/register") | ||||
| @ApiOperation(value = "用户注册", notes = "") | @ApiOperation(value = "用户注册", notes = "") | ||||
| public ResultData userRegister(@RequestParam Map<String, String> map) { | |||||
| logger.info("UserBasicInfoController >>>>>>>> userRegister >>>>>>>>>>>>>>>>>"+map.toString()); | |||||
| String phone = map.get("phone"); | |||||
| logger.info("UserBasicInfoController >>>>>>>> userRegister >>>>>>>>>>>>>>>>>phone="+phone); | |||||
| public ResultData userRegister(@RequestBody Map<String,Object> map) { | |||||
| logger.info("UserBasicInfoController >>>>>>>>>>>>>>>>>>>>>>>>>"+map.toString()); | |||||
| // logger.info("UserBasicInfoController >>>>>>>>phone >>>>>>>>>>>>>>>>>"+phone); | |||||
| return new ResultData(); | return new ResultData(); | ||||
| } | } | ||||
| @@ -44,18 +45,23 @@ public class UserBasicInfoController extends BaseController { | |||||
| String signKey = "C61842C8570524AA81756C53B8096978A06AE00D"; | String signKey = "C61842C8570524AA81756C53B8096978A06AE00D"; | ||||
| Map<String,String> headMap = new HashMap<>(); | Map<String,String> headMap = new HashMap<>(); | ||||
| headMap.put("cookie",appId+"&"+appKey); | |||||
| headMap.put("appkey",appId+"&"+appKey); | |||||
| Map<String,String> paramMap = new HashMap<>(); | Map<String,String> paramMap = new HashMap<>(); | ||||
| paramMap.put("timeStamp",Long.toString(new Date().getTime()));//当前时间戳 | |||||
| String timeStamp = Long.toString(new Date().getTime()); | |||||
| System.out.println("timeStamp="+timeStamp); | |||||
| paramMap.put("timeStamp",timeStamp);//当前时间戳 | |||||
| paramMap.put("phone","17600293031"); | paramMap.put("phone","17600293031"); | ||||
| paramMap.put("userName","昵称"); | paramMap.put("userName","昵称"); | ||||
| paramMap.put("userSex","1"); | paramMap.put("userSex","1"); | ||||
| String sign = SignUtils.getSign(signKey, paramMap, "MD5"); | String sign = SignUtils.getSign(signKey, paramMap, "MD5"); | ||||
| System.out.println("sign="+sign); | |||||
| headMap.put("sign",sign); | headMap.put("sign",sign); | ||||
| HttpUtil.doPost("https://openapitest.malls.iformall.com/api/userInfo/register",headMap,paramMap); | |||||
| String s = HttpUtil.doPost("https://openapitest.malls.iformall.com/api/api/userInfo/register", headMap, JSON.toJSONString(paramMap)); | |||||
| System.out.println(s); | |||||
| } | } | ||||
| @@ -1,6 +1,9 @@ | |||||
| package com.iformall.interceptor; | package com.iformall.interceptor; | ||||
| import com.alibaba.fastjson.JSON; | |||||
| import com.alibaba.fastjson.JSONObject; | |||||
| import com.alibaba.fastjson.TypeReference; | |||||
| import com.iformall.common.ErrorCode; | import com.iformall.common.ErrorCode; | ||||
| import com.iformall.domain.po.WxThirdPartyApi; | import com.iformall.domain.po.WxThirdPartyApi; | ||||
| import com.iformall.exception.MallinkException; | import com.iformall.exception.MallinkException; | ||||
| @@ -18,8 +21,15 @@ import org.springframework.data.redis.core.RedisTemplate; | |||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
| import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||||
| import javax.servlet.ServletException; | |||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||
| import java.io.BufferedReader; | |||||
| import java.io.IOException; | |||||
| import java.io.InputStreamReader; | |||||
| import java.io.UnsupportedEncodingException; | |||||
| import java.net.URLDecoder; | |||||
| import java.net.URLEncoder; | |||||
| import java.util.*; | import java.util.*; | ||||
| /** | /** | ||||
| @@ -43,11 +53,13 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||||
| @Override | @Override | ||||
| public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||||
| String cookie = request.getHeader("cookie"); | |||||
| if (StringUtils.isBlank(cookie) || !cookie.contains("&")) { | |||||
| response.setHeader("Content-type", "application/json;charset=UTF-8"); | |||||
| String appkey = request.getHeader("appkey"); | |||||
| if (StringUtils.isBlank(appkey) || !appkey.contains("&")) { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"非法请求"); | throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"非法请求"); | ||||
| } | } | ||||
| String[] split = cookie.split("&"); | |||||
| String[] split = appkey.split("&"); | |||||
| WxThirdPartyApi apiConfig = wxThirdPartyApiService.findByApp(split[0], split[1]); | WxThirdPartyApi apiConfig = wxThirdPartyApiService.findByApp(split[0], split[1]); | ||||
| if(apiConfig == null){ | if(apiConfig == null){ | ||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"非法请求"); | throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"非法请求"); | ||||
| @@ -60,15 +72,57 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"缺少加密串"); | throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"缺少加密串"); | ||||
| } | } | ||||
| String timeStamp = request.getParameter("timeStamp"); | |||||
| long timestampDate = Long.valueOf(timeStamp) + 1000*60*5;//五分钟有效 | |||||
| long currDate = System.currentTimeMillis(); | |||||
| // 请求过期 | |||||
| if (timestampDate < currDate) { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); | |||||
| String body = ((BodyReaderHttpServletRequestWrapper) request).getBody(); | |||||
| if(StringUtils.isBlank(body)){ | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"缺少参数"); | |||||
| } | |||||
| try{ | |||||
| // JSONObject parameterMap = JSON.parseObject(body); | |||||
| Map<String, String> parameterMap = JSONObject.parseObject(body, new TypeReference<Map<String, String>>() { | |||||
| }); | |||||
| String timeStamp = parameterMap.get("timeStamp"); | |||||
| long timestampDate = Long.valueOf(timeStamp) + 1000*60*5;//五分钟有效 | |||||
| long currDate = System.currentTimeMillis(); | |||||
| // 请求过期 | |||||
| if (timestampDate < currDate) { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); | |||||
| } | |||||
| logger.info("sign={}"+signature); | |||||
| String signKey = apiConfig.getSignKey();//TODO 根据appId查询密钥,缓存 | |||||
| String newSignature = SignUtils.getSign(signKey, parameterMap, "MD5"); | |||||
| //加密串不匹配 | |||||
| if (!signature.equals(newSignature)) { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); | |||||
| } | |||||
| Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+signature); | |||||
| if (null != cache) { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); | |||||
| } | |||||
| RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+signature, 1, 300); | |||||
| return true; | |||||
| }catch(Exception e){ | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"参数格式不正确"); | |||||
| } | } | ||||
| // String nonceStr = request.getParameter("nonceStr"); | |||||
| // Map<String, String> param = StringToMap(body); | |||||
| // if(param.isEmpty()){ | |||||
| // throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"参数格式不正确"); | |||||
| // } | |||||
| // | |||||
| // String timeStamp = param.get("timeStamp"); | |||||
| // long timestampDate = Long.valueOf(timeStamp) + 1000*60*5;//五分钟有效 | |||||
| // long currDate = System.currentTimeMillis(); | |||||
| // // 请求过期 | |||||
| // if (timestampDate < currDate) { | |||||
| // throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); | |||||
| // } | |||||
| // String nonceStr = param.get("nonceStr"); | |||||
| // //重复调用 | // //重复调用 | ||||
| // Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+nonceStr); | // Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+nonceStr); | ||||
| // if (null != cache) { | // if (null != cache) { | ||||
| @@ -77,38 +131,69 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||||
| Enumeration<String> paramNames = request.getParameterNames(); | |||||
| Map map = new HashMap(); | |||||
| //获取所有的请求参数 | |||||
| while (paramNames.hasMoreElements()) { | |||||
| String paramName = paramNames.nextElement(); | |||||
| String[] paramValues = request.getParameterValues(paramName); | |||||
| // Enumeration<String> paramNames = request.getParameterNames(); | |||||
| // Map<String, String> map = new HashMap(); | |||||
| // | |||||
| // //获取所有的请求参数 | |||||
| // while (paramNames.hasMoreElements()) { | |||||
| // String paramName = paramNames.nextElement(); | |||||
| // String[] paramValues = request.getParameterValues(paramName); | |||||
| // | |||||
| // if (paramValues.length > 0) { | |||||
| // String paramValue = paramValues[0]; | |||||
| // if (paramValue.length() != 0 | |||||
| // && !"sign".equals(paramName) | |||||
| // && !"appId".equals(paramName) | |||||
| // && !"appKey".equals(paramName) | |||||
| // && !"signKey".equals(paramName)) { | |||||
| // map.put(paramName, paramValue); | |||||
| // } | |||||
| // } | |||||
| // } | |||||
| if (paramValues.length > 0) { | |||||
| String paramValue = paramValues[0]; | |||||
| if (paramValue.length() != 0 | |||||
| && !"sign".equals(paramName) | |||||
| && !"appId".equals(paramName) | |||||
| && !"appKey".equals(paramName) | |||||
| && !"signKey".equals(paramName)) { | |||||
| map.put(paramName, paramValue); | |||||
| } | |||||
| } | |||||
| } | |||||
| logger.info("sign={}"+signature); | |||||
| String signKey = apiConfig.getSignKey();//TODO 根据appId查询密钥,缓存 | |||||
| String newSignature = SignUtils.getSign(signKey, map, "MD5"); | |||||
| //加密串不匹配 | |||||
| if (!signature.equals(newSignature)) { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); | |||||
| } | |||||
| Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+signature); | |||||
| if (null != cache) { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); | |||||
| } | |||||
| RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+signature, 1, 300); | |||||
| return true; | |||||
| // logger.info("sign={}"+signature); | |||||
| // String signKey = apiConfig.getSignKey();//TODO 根据appId查询密钥,缓存 | |||||
| // String newSignature = SignUtils.getSign(signKey, param, "MD5"); | |||||
| // //加密串不匹配 | |||||
| // if (!signature.equals(newSignature)) { | |||||
| // throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); | |||||
| // } | |||||
| // Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+signature); | |||||
| // if (null != cache) { | |||||
| // throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); | |||||
| // } | |||||
| // RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+signature, 1, 300); | |||||
| // return true; | |||||
| } | } | ||||
| // private Map<String, String> StringToMap(String body){ | |||||
| // Map<String, String> map = new HashMap<String, String>(); | |||||
| // String bodyDecode = ""; | |||||
| // try { | |||||
| // bodyDecode = URLDecoder.decode(body, "utf-8"); | |||||
| // } catch (UnsupportedEncodingException e) { | |||||
| // e.printStackTrace(); | |||||
| // return map; | |||||
| // } | |||||
| // | |||||
| // String[] keyValues = bodyDecode.split("&"); | |||||
| // for (String kv:keyValues) { | |||||
| // if(StringUtils.isBlank(kv) || !kv.contains("=")){ | |||||
| // continue; | |||||
| // } | |||||
| // String[] key_value = kv.split("="); | |||||
| // String key = key_value[0]; | |||||
| // if(StringUtils.isNotBlank(key) | |||||
| // && !"sign".equals(key) | |||||
| // && !"appId".equals(key) | |||||
| // && !"appKey".equals(key) | |||||
| // && !"signKey".equals(key)){ | |||||
| // String value = kv.substring(kv.indexOf('=') + 1, kv.length()); | |||||
| // map.put(key_value[0],value); | |||||
| // } | |||||
| // } | |||||
| // return map; | |||||
| // } | |||||
| } | } | ||||
| @@ -13,7 +13,7 @@ public class BodyReaderHttpServletRequestWrapper extends XssHttpServletRequestWr | |||||
| StringBuilder stringBuilder = new StringBuilder(); | StringBuilder stringBuilder = new StringBuilder(); | ||||
| BufferedReader bufferedReader = null; | BufferedReader bufferedReader = null; | ||||
| try { | try { | ||||
| InputStream inputStream = request.getInputStream(); | |||||
| InputStream inputStream = cloneInputStream(request.getInputStream()); | |||||
| if (inputStream != null) { | if (inputStream != null) { | ||||
| bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8")); | bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8")); | ||||
| char[] charBuffer = new char[1024]; | char[] charBuffer = new char[1024]; | ||||
| @@ -70,6 +70,29 @@ public class BodyReaderHttpServletRequestWrapper extends XssHttpServletRequestWr | |||||
| return new BufferedReader(new InputStreamReader(this.getInputStream())); | return new BufferedReader(new InputStreamReader(this.getInputStream())); | ||||
| } | } | ||||
| /** | |||||
| * Description: 复制输入流</br> | |||||
| * | |||||
| * @param inputStream | |||||
| * @return</br> | |||||
| */ | |||||
| public InputStream cloneInputStream(ServletInputStream inputStream) { | |||||
| ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |||||
| byte[] buffer = new byte[1024]; | |||||
| int len; | |||||
| try { | |||||
| while ((len = inputStream.read(buffer)) > -1) { | |||||
| byteArrayOutputStream.write(buffer, 0, len); | |||||
| } | |||||
| byteArrayOutputStream.flush(); | |||||
| } | |||||
| catch (IOException e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| InputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); | |||||
| return byteArrayInputStream; | |||||
| } | |||||
| public String getBody() { | public String getBody() { | ||||
| return this.body; | return this.body; | ||||
| } | } | ||||
| @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; | |||||
| import javax.servlet.*; | import javax.servlet.*; | ||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| public class HttpServletRequestWrapperFilter implements Filter { | public class HttpServletRequestWrapperFilter implements Filter { | ||||
| @@ -7,7 +7,7 @@ import lombok.EqualsAndHashCode; | |||||
| import java.util.Date; | import java.util.Date; | ||||
| @TableName(value = "wx_third_party_Api") | |||||
| @TableName(value = "wx_third_party_api") | |||||
| @Data | @Data | ||||
| @EqualsAndHashCode(callSuper = true) | @EqualsAndHashCode(callSuper = true) | ||||
| public class WxThirdPartyApi extends TenantEntity { | public class WxThirdPartyApi extends TenantEntity { | ||||
| @@ -23,6 +23,7 @@ public class WxThirdPartyApiServiceImpl implements WxThirdPartyApiService { | |||||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||
| @Autowired | |||||
| WxThirdPartyApiMapper wxThirdPartyApiMapper; | WxThirdPartyApiMapper wxThirdPartyApiMapper; | ||||
| @Autowired | @Autowired | ||||
| @@ -413,6 +413,65 @@ public class HttpUtil { | |||||
| return null; | return null; | ||||
| } | } | ||||
| /** | |||||
| * post请求(用于请求json格式的参数) | |||||
| * @param url | |||||
| * @param params | |||||
| * @return | |||||
| */ | |||||
| public static String doPost(String url, Map<String,String> headMap, String params){ | |||||
| CloseableHttpClient httpclient = HttpClients.createDefault(); | |||||
| HttpPost httpPost = new HttpPost(url);// 创建httpPost | |||||
| httpPost.setHeader("Accept", "application/json"); | |||||
| httpPost.setHeader("Content-Type", "application/json"); | |||||
| if(headMap != null){ | |||||
| Set<String> set = headMap.keySet(); | |||||
| for(String key: set){ | |||||
| httpPost.addHeader(key,headMap.get(key)); | |||||
| } | |||||
| } | |||||
| String charSet = "UTF-8"; | |||||
| StringEntity entity = new StringEntity(params, charSet); | |||||
| httpPost.setEntity(entity); | |||||
| CloseableHttpResponse response = null; | |||||
| try { | |||||
| response = httpclient.execute(httpPost); | |||||
| StatusLine status = response.getStatusLine(); | |||||
| int state = status.getStatusCode(); | |||||
| if (state == HttpStatus.SC_OK) { | |||||
| HttpEntity responseEntity = response.getEntity(); | |||||
| String jsonString = EntityUtils.toString(responseEntity); | |||||
| return jsonString; | |||||
| } | |||||
| else{ | |||||
| logger.info("请求返回:"+state+"("+url+")"); | |||||
| } | |||||
| } | |||||
| catch(Exception e){ | |||||
| logger.error(e.getMessage()); | |||||
| return null; | |||||
| } | |||||
| finally { | |||||
| if (response != null) { | |||||
| try { | |||||
| response.close(); | |||||
| } catch (IOException e) { | |||||
| logger.error(e.getMessage()); | |||||
| } | |||||
| } | |||||
| try { | |||||
| httpclient.close(); | |||||
| } catch (IOException e) { | |||||
| logger.error(e.getMessage()); | |||||
| } | |||||
| } | |||||
| return null; | |||||
| } | |||||
| public static String payPost(String url, String params) { | public static String payPost(String url, String params) { | ||||
| RequestBody body = RequestBody.create(CONTENT_TYPE_FORM, params); | RequestBody body = RequestBody.create(CONTENT_TYPE_FORM, params); | ||||