master
parent
01e942350f
commit
1480226af5
Binary file not shown.
@ -0,0 +1,69 @@ |
||||
package com.order.controller.notify; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hfkj.common.utils.HttpsUtils; |
||||
import com.hfkj.config.CommonSysConst; |
||||
import com.hfkj.jd.JdService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.PrintWriter; |
||||
|
||||
@Controller |
||||
@RequestMapping(value = "/jdNotify") |
||||
@Api(value = "京东回调") |
||||
public class JdNotify { |
||||
private static final Logger log = LoggerFactory.getLogger(JdService.class); |
||||
|
||||
@RequestMapping(value = "/notify", method = RequestMethod.POST) |
||||
@ApiOperation(value = "回调") |
||||
@ResponseBody |
||||
public void notify(@RequestBody String reqBodyStr, HttpServletRequest request, HttpServletResponse response) { |
||||
try { |
||||
|
||||
JSONObject dataObject = JSONObject.parseObject(reqBodyStr, JSONObject.class); |
||||
|
||||
log.info("============回调任务Start============="); |
||||
log.info("尖椒订单充值-回调参数: " + dataObject); |
||||
log.info("============回调任务End============="); |
||||
|
||||
|
||||
response.setCharacterEncoding("UTF-8"); |
||||
response.setContentType("text/html;charset=utf-8"); |
||||
PrintWriter writer= response.getWriter(); |
||||
writer.write("SUCCESS"); |
||||
|
||||
|
||||
} catch (Exception e) { |
||||
log.error("WechatPayController --> wechatNotify() error!", e); |
||||
} |
||||
} |
||||
|
||||
@RequestMapping(value = "/callbackToken", method = RequestMethod.GET) |
||||
@ApiOperation(value = "token回调") |
||||
@ResponseBody |
||||
public void notify(@RequestParam("code") String code) { |
||||
try { |
||||
|
||||
log.info("============token回调任务Start============="); |
||||
log.info("token回调-回调参数: " + code); |
||||
JSONObject object = new JSONObject(); |
||||
object.put("app_key", CommonSysConst.getSysConfig().getJDAppKey()); |
||||
object.put("app_secret", CommonSysConst.getSysConfig().getJDAppSecret()); |
||||
object.put("code", code); |
||||
object.put("grant_type", "authorization_code"); |
||||
JSONObject jsonObject = HttpsUtils.doGet("https://open-oauth.jd.com/oauth2/authorizeForVOP", object); |
||||
log.info("token回调-回调参数: " + jsonObject); |
||||
log.info("============token回调任务End============="); |
||||
|
||||
} catch (Exception e) { |
||||
log.error("WechatPayController --> wechatNotify() error!", e); |
||||
} |
||||
} |
||||
} |
Binary file not shown.
@ -0,0 +1,113 @@ |
||||
package com.hfkj.common.utils; |
||||
|
||||
import javafx.util.Pair; |
||||
import org.apache.commons.codec.binary.Base64; |
||||
|
||||
import javax.crypto.Cipher; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.security.*; |
||||
import java.security.interfaces.RSAPrivateKey; |
||||
import java.security.interfaces.RSAPublicKey; |
||||
import java.security.spec.PKCS8EncodedKeySpec; |
||||
import java.security.spec.X509EncodedKeySpec; |
||||
|
||||
public final class RsaCoderUtils { |
||||
|
||||
/** |
||||
* 生成公私钥 |
||||
* |
||||
* @return |
||||
* @throws NoSuchAlgorithmException |
||||
*/ |
||||
public static Pair<String, String> genKeyPair() throws NoSuchAlgorithmException { |
||||
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
|
||||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); |
||||
// 初始化密钥对生成器,密钥大小为96-1024位
|
||||
keyPairGen.initialize(1024, new SecureRandom()); |
||||
// 生成一个密钥对,保存在keyPair中
|
||||
KeyPair keyPair = keyPairGen.generateKeyPair(); |
||||
// 得到私钥
|
||||
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); |
||||
// 得到公钥
|
||||
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); |
||||
String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded())); |
||||
// 得到私钥字符串
|
||||
String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded()))); |
||||
return new Pair<>(publicKeyString, privateKeyString); |
||||
} |
||||
|
||||
/** |
||||
* RSA私钥加密 |
||||
* |
||||
* @param str 加密字符串 |
||||
* @param privateKey 私钥 |
||||
* @return 密文 |
||||
* @throws Exception 加密过程中的异常信息 |
||||
*/ |
||||
public static String encryptByPrivateKey(String str, String privateKey) throws Exception { |
||||
//base64编码的公钥
|
||||
byte[] decoded = Base64.decodeBase64(privateKey); |
||||
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); |
||||
//RSA解密
|
||||
Cipher cipher = Cipher.getInstance("RSA"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, priKey); |
||||
return Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8))); |
||||
} |
||||
|
||||
/** |
||||
* RSA公钥加密 |
||||
* |
||||
* @param str 加密字符串 |
||||
* @return 密文 |
||||
* @throws Exception 加密过程中的异常信息 |
||||
*/ |
||||
public static String encryptByPublicKey(String str, String publicKey) throws Exception { |
||||
//base64编码的公钥
|
||||
byte[] decoded = Base64.decodeBase64(publicKey); |
||||
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); |
||||
//RSA加密
|
||||
Cipher cipher = Cipher.getInstance("RSA"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, pubKey); |
||||
return Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8))); |
||||
} |
||||
|
||||
/** |
||||
* RSA公钥解密 |
||||
* |
||||
* @param str 加密字符串 |
||||
* @param publicKey 公钥 |
||||
* @return 铭文 |
||||
* @throws Exception 解密过程中的异常信息 |
||||
*/ |
||||
public static String decryptByPublicKey(String str, String publicKey) throws Exception { |
||||
//64位解码加密后的字符串
|
||||
byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8)); |
||||
//base64编码的私钥
|
||||
byte[] decoded = Base64.decodeBase64(publicKey); |
||||
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); |
||||
//RSA加密
|
||||
Cipher cipher = Cipher.getInstance("RSA"); |
||||
cipher.init(Cipher.DECRYPT_MODE, pubKey); |
||||
return new String(cipher.doFinal(inputByte)); |
||||
} |
||||
|
||||
/** |
||||
* RSA私钥解密 |
||||
* |
||||
* @param str 加密字符串 |
||||
* @return 铭文 |
||||
* @throws Exception 解密过程中的异常信息 |
||||
*/ |
||||
public static String decryptByPrivateKey(String str, String privateKey) throws Exception { |
||||
//64位解码加密后的字符串
|
||||
byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8)); |
||||
//base64编码的私钥
|
||||
byte[] decoded = Base64.decodeBase64(privateKey); |
||||
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); |
||||
//RSA解密
|
||||
Cipher cipher = Cipher.getInstance("RSA"); |
||||
cipher.init(Cipher.DECRYPT_MODE, priKey); |
||||
return new String(cipher.doFinal(inputByte)); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,63 @@ |
||||
package com.hfkj.jd; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hfkj.common.utils.HttpsUtils; |
||||
import com.hfkj.common.utils.RsaCoderUtils; |
||||
import com.hfkj.config.CommonSysConst; |
||||
import com.hfkj.meituan.MeiTuanService; |
||||
import com.jd.open.api.sdk.DefaultJdClient; |
||||
import com.jd.open.api.sdk.JdClient; |
||||
import org.apache.commons.codec.digest.DigestUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.net.URLEncoder; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @ClassName JdService |
||||
* @Author Sum1Dream |
||||
* @Description 京东服务 |
||||
* @Date 2024/10/11 下午3:25 |
||||
**/ |
||||
public class JdService { |
||||
private static Logger log = LoggerFactory.getLogger(JdService.class); |
||||
|
||||
/** |
||||
* UTF_8 |
||||
*/ |
||||
public static final String UTF_8 = "utf-8"; |
||||
/** |
||||
* 用户名 |
||||
*/ |
||||
private static final String USERNAME = "惠兑重庆VOP", |
||||
/** |
||||
* 明文密码,后续需要通过RSA加密 |
||||
*/ |
||||
PLAINTEXT_PASSWORD = "hdcs1234", |
||||
/** |
||||
* 回调地址,注册应用时配置的授权回调地址 |
||||
*/ |
||||
REDIRECT_URI = CommonSysConst.getSysConfig().getDomainName() + "/order/jdNotify/callbackToken", |
||||
/** |
||||
* RSA私钥 |
||||
*/ |
||||
PRIVATE_RSA_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMEtt0kF02YBxPXzXyqzCtr1zkuj8NXLiqqSCDAfU6zWB1T1GUEpCfNukPBeZ9nXGj+jbTom8RFllNXbnOkW01Zn2VoyePh8TRWQbToXi+i/JQ162GzpF0GdPsUhJSrB5Z2QzK4UVXXkpPHhQHR8NKQhvTRkoRLfOy+oXWL8PS2hAgMBAAECgYBySo/j/jRiZ62WLlUhuCg1/7P8AJSeiPwTiq6Zeg9RdJeF5jT43kTq54GNFO2wbpkzCYe4Hg4GUulJ1dLx/PUvYquRpM4jGznxDxBA10mNyjx0O+SHcY+ZlOYWIKhEq4MeeJhtHhoFrlgcMNIyofhRDbFzrRak48GrcZDQC4/cIQJBAPwxtEwMnetPveLExR1UsygdVKwoWoGNL5Hkhcx6b2N2Qapk5aHB5haUEXeg1ShrL4C/kE1lsMAIdpVS9lWd4x0CQQDEGASnnB2oPHjvNfj88klpYTvPJRi3ORQPtlfz36kv+29QFxB9Mt1u39ttopw5G2x7QvTtL542w878brrmS8lVAkEA4k7QFjZ0N8cVBLvCjrGFG4hGhT6pCPxjJa5GCtoLvttNzdRA5EkVaklw60LeRSj6NbSxj2Kjm498qj8KYoYOWQJATJhRISMy1mcgmdeMNUlycW4gjY4g9FigRG7mNgU0MeDVnwQTVcQLiGZ6cH2m5guXOSJzRz5lX2DmteWOrawGrQJBAMKLfeTnYkVpXw9MABcR/9L9myG3FKwOHBIqD0wK+QVahi9byFk8GFXD4OlBQ1ZSxHqsUuxWqLVSa3sWBCKpLo8="; |
||||
|
||||
public static JSONObject getAccessToken() throws Exception { |
||||
String encodeRedirectUri = URLEncoder.encode(REDIRECT_URI, UTF_8); |
||||
String encodeUsername = URLEncoder.encode(USERNAME, UTF_8); |
||||
String md5Password = DigestUtils.md5Hex(PLAINTEXT_PASSWORD); |
||||
String ciphertextPassword = RsaCoderUtils.encryptByPrivateKey(md5Password, PRIVATE_RSA_KEY); |
||||
String encodePassword = URLEncoder.encode(ciphertextPassword, UTF_8); |
||||
|
||||
JSONObject object = new JSONObject(); |
||||
object.put("app_key", CommonSysConst.getSysConfig().getJDAppKey()); |
||||
object.put("redirect_uri", encodeRedirectUri); |
||||
object.put("username", encodeUsername); |
||||
object.put("password", encodePassword); |
||||
object.put("response_type", "code"); |
||||
object.put("scope", "snsapi_base"); |
||||
return HttpsUtils.doGet("https://open-oauth.jd.com/oauth2/authorizeForVOP", object); |
||||
} |
||||
} |
Loading…
Reference in new issue