![在这里插入图片描述](https://img-blog.csdnimg.cn/bd163b0c3f56482593476d65dee8fe4d.png)
1.restTemplate
配置
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
return factory;
}
}
2.请求体参数组装方法
public static String messageAssembly(String areaId){
Map<String, Object> map = new HashMap<>();
map.put("areaId",areaId);
return JSONUtil.toJsonStr(map);
}
3.完整请求体组装
public static String messageAssemblyResult(String encryptParam){
Map<String, Object> map = new HashMap<>();
map.put("appId", PrePayConstant.APP_ID_TEST);
map.put("encryptParam",encryptParam);
return JSONUtil.toJsonStr(map);
}
4.RSA加密方法
import com.myc.common.constant.PrePayConstant;
import org.apache.commons.codec.binary.Base64;
import org.apache.poi.util.IOUtils;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
public class RSAUtil {
public static String RSAEncrypt(String data) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(PrePayConstant.PUBLIC_KEY_TEST));
RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
String encode;
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
encode = Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(StandardCharsets.UTF_8), key.getModulus().bitLength()));
} catch (Exception e) {
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
}
return encode;
}
private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas, int keySize) {
int maxBlock = 0;
if (opmode == Cipher.DECRYPT_MODE) {
maxBlock = keySize / 8;
} else {
maxBlock = keySize / 8 - 11;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] buff;
int i = 0;
try {
while (datas.length > offSet) {
if (datas.length - offSet > maxBlock) {
buff = cipher.doFinal(datas, offSet, maxBlock);
} else {
buff = cipher.doFinal(datas, offSet, datas.length - offSet);
}
out.write(buff, 0, buff.length);
i++;
offSet = i maxBlock;
}
} catch(Exception e){
throw new RuntimeException("加解密阀值为[" + maxBlock + "]的数据时发生异 常", e);
}
byte[] resultDatas = out.toByteArray();
IOUtils.closeQuietly(out);
return resultDatas;
}
}
5.开始调用
@Service
public class PrePayServiceImpl implements IPrePayService {
@Autowired
private RestTemplate restTemplate;
@Override
public PrePayResult areaList(String areaId){
String dataParam = MessageAssemblyUtil.messageAssembly(areaId);
PrePayResult<Object> result;
try {
String dataRSA = RSAUtil.RSAEncrypt(dataParam);
String body = MessageAssemblyUtil.messageAssemblyResult(dataRSA);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", " application/json;charset=utf-8");
HttpEntity requestEntity = new HttpEntity<>(body, headers);
result = restTemplate.postForObject(PrePayConstant.URL_TEST + PrePayConstant.AREA_LIST_METHOD, requestEntity, PrePayResult.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
}