Compare commits
	
		
			11 Commits 
		
	
	
		
			master
			...
			taobao_mod
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | d7bfe646bc | 5 years ago | 
|  | 88802f14cd | 5 years ago | 
|  | 59c4ae348a | 5 years ago | 
|  | ae1aaa7da2 | 6 years ago | 
|  | 14a8a6b10b | 6 years ago | 
|  | 333c9a55c4 | 6 years ago | 
|  | cb0930a368 | 6 years ago | 
|  | 9b74afcfd3 | 6 years ago | 
|  | 154a3b1636 | 6 years ago | 
|  | f0dcb217ab | 6 years ago | 
|  | bcd762c309 | 6 years ago | 
| @ -0,0 +1,10 @@ | ||||
| package com.gitee.sop.gatewaycommon.validate.taobao; | ||||
| 
 | ||||
| /** | ||||
|  * @author tanghc | ||||
|  */ | ||||
| public class Constants { | ||||
|     public static final String SIGN_METHOD_HMAC = "hmac"; | ||||
|     public static final String SIGN_METHOD_MD5 = "md5"; | ||||
|     public static final String CHARSET_UTF8 = "UTF-8"; | ||||
| } | ||||
| @ -0,0 +1,97 @@ | ||||
| package com.gitee.sop.gatewaycommon.validate.taobao; | ||||
| 
 | ||||
| import com.gitee.sop.gatewaycommon.validate.alipay.StringUtils; | ||||
| 
 | ||||
| import javax.crypto.Mac; | ||||
| import javax.crypto.SecretKey; | ||||
| import javax.crypto.spec.SecretKeySpec; | ||||
| import java.io.IOException; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.security.GeneralSecurityException; | ||||
| import java.security.MessageDigest; | ||||
| import java.security.NoSuchAlgorithmException; | ||||
| import java.util.Arrays; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| /** | ||||
|  * @author tanghc | ||||
|  */ | ||||
| public class TaobaoSignature { | ||||
| 
 | ||||
| 
 | ||||
|     public static String signTopRequest(Map<String, String> params, String secret, String signMethod) throws IOException { | ||||
|         String content = getSignContent(params); | ||||
|         return doSign(content, secret, signMethod); | ||||
|     } | ||||
| 
 | ||||
|     public static String getSignContent(Map<String, ?> params) { | ||||
|         // 第一步:检查参数是否已经排序
 | ||||
|         String[] keys = params.keySet().toArray(new String[0]); | ||||
|         Arrays.sort(keys); | ||||
| 
 | ||||
|         // 第二步:把所有参数名和参数值串在一起
 | ||||
|         StringBuilder query = new StringBuilder(); | ||||
| 
 | ||||
|         for (String key : keys) { | ||||
|             Object val = params.get(key); | ||||
|             String value = val == null ? null : val.toString(); | ||||
|             if (StringUtils.areNotEmpty(key, value)) { | ||||
|                 query.append(key).append(value); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return query.toString(); | ||||
|     } | ||||
| 
 | ||||
|     public static String doSign(String content,String secret, String signMethod) throws IOException { | ||||
|         // 第三步:使用MD5/HMAC加密
 | ||||
|         byte[] bytes; | ||||
|         if (Constants.SIGN_METHOD_HMAC.equals(signMethod)) { | ||||
|             bytes = encryptHMAC(content, secret); | ||||
|         } else { | ||||
|             bytes = encryptMD5(secret + content + secret); | ||||
|         } | ||||
| 
 | ||||
|         // 第四步:把二进制转化为大写的十六进制(正确签名应该为32大写字符串,此方法需要时使用)
 | ||||
|         return byte2hex(bytes); | ||||
|     } | ||||
| 
 | ||||
|     public static byte[] encryptHMAC(String data, String secret) throws IOException { | ||||
|         byte[] bytes = null; | ||||
|         try { | ||||
|             SecretKey secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacMD5"); | ||||
|             Mac mac = Mac.getInstance(secretKey.getAlgorithm()); | ||||
|             mac.init(secretKey); | ||||
|             bytes = mac.doFinal(data.getBytes(StandardCharsets.UTF_8)); | ||||
|         } catch (GeneralSecurityException gse) { | ||||
|             throw new IOException(gse.toString()); | ||||
|         } | ||||
|         return bytes; | ||||
|     } | ||||
| 
 | ||||
|     public static byte[] encryptMD5(String data) throws IOException { | ||||
|         return encryptMD5(data.getBytes(StandardCharsets.UTF_8)); | ||||
|     } | ||||
| 
 | ||||
|     public static byte[] encryptMD5(byte[] data) { | ||||
|         try { | ||||
|             MessageDigest digest = MessageDigest.getInstance(Constants.SIGN_METHOD_MD5); | ||||
|             return digest.digest(data); | ||||
|         } catch (NoSuchAlgorithmException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     public static String byte2hex(byte[] bytes) { | ||||
|         StringBuilder sign = new StringBuilder(); | ||||
|         for (int i = 0; i < bytes.length; i++) { | ||||
|             String hex = Integer.toHexString(bytes[i] & 0xFF); | ||||
|             if (hex.length() == 1) { | ||||
|                 sign.append("0"); | ||||
|             } | ||||
|             sign.append(hex.toUpperCase()); | ||||
|         } | ||||
|         return sign.toString(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,45 @@ | ||||
| package com.gitee.sop.storyweb.controller; | ||||
| 
 | ||||
| import com.gitee.sop.servercommon.annotation.ApiAbility; | ||||
| import com.gitee.sop.storyweb.controller.param.StoryParam; | ||||
| import com.gitee.sop.storyweb.controller.result.StoryResult; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.web.bind.annotation.RequestBody; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RequestParam; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| 
 | ||||
| /** | ||||
|  * | ||||
|  * @author tanghc | ||||
|  */ | ||||
| @RestController | ||||
| @Slf4j | ||||
| // 注解放在这里,表示类中的方法都具备接口开放能力
 | ||||
| @ApiAbility | ||||
| public class TaobaoController { | ||||
| 
 | ||||
|     @RequestMapping("/order/get") | ||||
|     public StoryResult get(@RequestParam String data) { | ||||
|         StoryResult story = new StoryResult(); | ||||
|         story.setId(1L); | ||||
|         story.setName(data); | ||||
|         return story; | ||||
|     } | ||||
| 
 | ||||
|     @RequestMapping("/order/save") | ||||
|     public StoryResult save(StoryParam param) { | ||||
|         StoryResult story = new StoryResult(); | ||||
|         story.setId((long) param.getId()); | ||||
|         story.setName(param.getName()); | ||||
|         return story; | ||||
|     } | ||||
| 
 | ||||
|     @RequestMapping("/order/json") | ||||
|     public StoryResult json(@RequestBody StoryParam param) { | ||||
|         StoryResult story = new StoryResult(); | ||||
|         story.setId((long) param.getId()); | ||||
|         story.setName(param.getName()); | ||||
|         return story; | ||||
|     } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue