first commit

main
Pavel Belyaev 2 years ago
commit ead50742d5

@ -0,0 +1,15 @@
package org.pavlik.helpers;
public class Main {
public static void main(String[] args) {
RestHelper Rest = new RestHelper("client-keystore3.jks","JKS","123321","2","truststore.jks","JKS","123321");
String cyrillicString = "какашка";
// try {
// cyrillicString = URLEncoder.encode(cyrillicString, "UTF-8");
// } catch (UnsupportedEncodingException e) {
// throw new RuntimeException(e);
// }
Rest.queryPostRaw("https://192.168.200.10/index.php?koko=1",cyrillicString);
}
}

@ -0,0 +1,120 @@
package org.pavlik.helpers;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.PrivateKeyStrategy;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.security.*;
public class RestHelper {
public String codepage="UTF-8";
protected HttpClient httpClient = null;
public RestHelper (String keypath,String keytype,String keypass, String keyAlias, String trustpath, String trusttype, String trustpass) {
boolean withTrust = false;
KeyStore truststore_material = null;
KeyStore keystore_material = (keypath !=null) ? this.readKeyStore(keypath, keytype, keypass) : null;
PrivateKeyStrategy privateKeyStrategy = keyAlias == null ? null : (aliases, socket) -> keyAlias;
if (trustpath !=null) {
truststore_material = this.readKeyStore(trustpath, trusttype, trustpass);
withTrust = true;
}
SSLContext sslContext = (withTrust) ?
genSSLContext(privateKeyStrategy,keystore_material,truststore_material,keypass) :
genSSLContextAllTrust(privateKeyStrategy,keystore_material,keypass);
this.httpClient = HttpClients.custom().setSSLContext(sslContext).build();
}
/**
* Подгружает файл JKS или PKCS12
* @param path - путь до jks/p12
* @param type - JKS или PKCS12
* @param pwd - пароль к хранилищу
* @return - хранилище для SSLContext
*/
protected KeyStore readKeyStore(String path, String type, String pwd) {
try {
FileInputStream KeyStoreFile = new FileInputStream(new File(path));
KeyStore keyStore = KeyStore.getInstance(type);
keyStore.load(KeyStoreFile, pwd.toCharArray());
return keyStore;
} catch (Exception e){
throw new RuntimeException(e);
}
}
/**
* mtls - взаимная аутентификация
* @return
*/
SSLContext genSSLContext (PrivateKeyStrategy privateKeyStrategy,KeyStore keyStore, KeyStore trustStore, String keypass) {
try {
return SSLContexts.custom()
.loadTrustMaterial(trustStore,null)
.loadKeyMaterial(keyStore, keypass.toCharArray(),privateKeyStrategy)
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Аутентификация только клиента, серверный сертификат не проверяется
* @return
*/
SSLContext genSSLContextAllTrust (PrivateKeyStrategy privateKeyStrategy,KeyStore keyStore, String keypass) {
try {
return SSLContexts.custom()
.loadTrustMaterial(null, (x509CertChain, authType) -> true) //вариант принимающий всё
.loadKeyMaterial(keyStore, keypass.toCharArray(),privateKeyStrategy) // use null as second param if you don't have a separate key password
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void queryPostRaw(String queryUrl, String queryBody) {
HttpResponse response = null;
String responseBody = null;
HttpPost query = new HttpPost(queryUrl);
query.setHeader("р1","yes"); //заголовок
query.setHeader("CONTENT-TYPE","text/plain; charset=UTF-8"); //заголовок
try {
query.setEntity(new StringEntity(queryBody,this.codepage)); //тело
response = this.httpClient.execute(query);
responseBody = EntityUtils.toString(response.getEntity(), this.codepage);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println("Response body: " + responseBody);
}
}
Loading…
Cancel
Save