| @@ -1,11 +1,16 @@ | |||||
| package com.iformall.controller; | package com.iformall.controller; | ||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.utils.HttpUtil; | |||||
| 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.*; | ||||
| import java.util.Date; | |||||
| import java.util.HashMap; | |||||
| import java.util.Map; | import java.util.Map; | ||||
| @RestController | @RestController | ||||
| @@ -32,5 +37,27 @@ public class UserBasicInfoController extends BaseController { | |||||
| return new ResultData(); | return new ResultData(); | ||||
| } | } | ||||
| public static void main(String[] args) { | |||||
| String appId = "CP6nwHvZ"; | |||||
| String appKey = "b257eb97bb9380714306a077a66086e8b1f10639"; | |||||
| String signKey = "C61842C8570524AA81756C53B8096978A06AE00D"; | |||||
| Map<String,String> headMap = new HashMap<>(); | |||||
| headMap.put("cookie",appId+"&"+appKey); | |||||
| Map<String,String> paramMap = new HashMap<>(); | |||||
| paramMap.put("timeStamp",Long.toString(new Date().getTime()));//当前时间戳 | |||||
| paramMap.put("phone","17600293031"); | |||||
| paramMap.put("userName","昵称"); | |||||
| paramMap.put("userSex","1"); | |||||
| String sign = SignUtils.getSign(signKey, paramMap, "MD5"); | |||||
| headMap.put("sign",sign); | |||||
| HttpUtil.doPost("https://openapitest.malls.iformall.com/api/userInfo/register",headMap,paramMap); | |||||
| } | |||||
| } | } | ||||
| @@ -68,12 +68,12 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); | throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); | ||||
| } | } | ||||
| String nonceStr = request.getParameter("nonceStr"); | |||||
| //重复调用 | |||||
| Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+nonceStr); | |||||
| if (null != cache) { | |||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); | |||||
| } | |||||
| // String nonceStr = request.getParameter("nonceStr"); | |||||
| // //重复调用 | |||||
| // Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+nonceStr); | |||||
| // if (null != cache) { | |||||
| // throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); | |||||
| // } | |||||
| @@ -103,7 +103,11 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||||
| if (!signature.equals(newSignature)) { | if (!signature.equals(newSignature)) { | ||||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); | throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); | ||||
| } | } | ||||
| RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+nonceStr, 1, 300); | |||||
| 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; | return true; | ||||
| } | } | ||||
| @@ -33,10 +33,7 @@ import java.net.URL; | |||||
| import java.nio.charset.Charset; | import java.nio.charset.Charset; | ||||
| import java.security.KeyStore; | import java.security.KeyStore; | ||||
| import java.security.SecureRandom; | import java.security.SecureRandom; | ||||
| import java.util.ArrayList; | |||||
| import java.util.Iterator; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import java.util.*; | |||||
| /** | /** | ||||
| @@ -169,6 +166,72 @@ public class HttpUtil { | |||||
| return null; | return null; | ||||
| } | } | ||||
| /** | |||||
| * post请求(用于key-value格式的参数) | |||||
| * @param url | |||||
| * @param params | |||||
| * @return | |||||
| */ | |||||
| public static String doPost(String url,Map<String,String> headMap, Map params){ | |||||
| // 定义HttpClient | |||||
| CloseableHttpClient client = HttpClients.createDefault(); | |||||
| BufferedReader in = null; | |||||
| try { | |||||
| // 实例化HTTP方法 | |||||
| HttpPost request = new HttpPost(); | |||||
| request.setURI(new URI(url)); | |||||
| if(headMap != null){ | |||||
| Set<String> set = headMap.keySet(); | |||||
| for(String key: set){ | |||||
| request.addHeader(key,headMap.get(key)); | |||||
| } | |||||
| } | |||||
| //设置参数 | |||||
| List<NameValuePair> nvps = new ArrayList<NameValuePair>(); | |||||
| for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { | |||||
| String name = (String) iter.next(); | |||||
| String value = String.valueOf(params.get(name)); | |||||
| nvps.add(new BasicNameValuePair(name, value)); | |||||
| //System.out.println(name +"-"+value); | |||||
| } | |||||
| request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8)); | |||||
| HttpResponse response = client.execute(request); | |||||
| int code = response.getStatusLine().getStatusCode(); | |||||
| if(code == 200){ //请求成功 | |||||
| in = new BufferedReader(new InputStreamReader(response.getEntity() | |||||
| .getContent(),"utf-8")); | |||||
| StringBuilder sb = new StringBuilder(""); | |||||
| String line = ""; | |||||
| String NL = System.getProperty("line.separator"); | |||||
| while ((line = in.readLine()) != null) { | |||||
| sb.append(line + NL); | |||||
| } | |||||
| in.close(); | |||||
| client.close(); | |||||
| return sb.toString(); | |||||
| } | |||||
| else{ // | |||||
| logger.info("状态码:" + code); | |||||
| client.close(); | |||||
| } | |||||
| } | |||||
| catch(Exception e){ | |||||
| logger.error(e.getMessage()); | |||||
| return null; | |||||
| } | |||||
| return null; | |||||
| } | |||||
| /** | /** | ||||
| * post请求(用于key-value格式的参数,wiwide) | * post请求(用于key-value格式的参数,wiwide) | ||||
| * @param url | * @param url | ||||