当前位置:   article > 正文

jersey的post方式访问HTTPS跳过SSL安全认证_jerseyclientt isdefaultsslcontext false

jerseyclientt isdefaultsslcontext false

项目场景:用过jersey的post访问HTTPS


问题描述

用过jersey的post访问HTTPS是报cert异常


解决方案:

提示:这里填写该问题的具体解决方案:

  1. import com.sun.jersey.api.client.Client;
  2. import com.sun.jersey.api.client.ClientResponse;
  3. import com.sun.jersey.api.client.WebResource;
  4. import com.sun.jersey.api.client.config.ClientConfig;
  5. import com.sun.jersey.api.client.config.DefaultClientConfig;
  6. import com.sun.jersey.client.urlconnection.HTTPSProperties;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.apache.commons.codec.binary.Base64;
  9. import org.apache.commons.codec.digest.HmacUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import javax.net.ssl.*;
  13. import java.io.UnsupportedEncodingException;
  14. import java.net.URLEncoder;
  15. import java.security.KeyManagementException;
  16. import java.security.NoSuchAlgorithmException;
  17. import java.security.SecureRandom;
  18. import java.security.cert.X509Certificate;
  19. import java.util.Date;
  20. import java.util.Random;
  21. @Service
  22. @Slf4j
  23. public class AccessService {
  24. public String postUrl(String url , String param){
  25. log.info("postUrl-url:{},param:{}",url,param);
  26. Client client = null;
  27. ClientResponse response = null;
  28. try {
  29. ClientConfig config = init();
  30. client = Client.create(config);
  31. client.setFollowRedirects(true);
  32. WebResource webResource = client.resource(url);
  33. response = webResource.header("Authorization",authHeader).type("application/json").post(ClientResponse.class, param);
  34. String jsonString = response.getEntity(String.class);
  35. log.warn("http返回码:{},http消息体为:{}" , response.getStatus(),jsonString);
  36. if (response.getStatus() == 200) {
  37. return jsonString;
  38. } else {
  39. throw new RuntimeException( "http请求异常,http返回码:" + response.getStatus() + ";http消息体为:" + jsonString);
  40. }
  41. }catch (Exception ex){
  42. log.warn("postUrl:"+ex.getMessage());
  43. }finally {
  44. if (response != null) {
  45. response.close();
  46. }
  47. if (client != null) {
  48. client.destroy();
  49. }
  50. }
  51. return "";
  52. }
  53. /**
  54. * 跳过https
  55. * */
  56. public static ClientConfig init() throws NoSuchAlgorithmException, KeyManagementException {
  57. SSLContext context = SSLContext.getInstance("SSL");
  58. TrustManager[] trustAllCerts = new TrustManager[] {
  59. new X509TrustManager() {
  60. public X509Certificate[] getAcceptedIssuers() {
  61. return null;
  62. }
  63. @Override
  64. public void checkClientTrusted(X509Certificate[] certs, String authType) {}
  65. @Override
  66. public void checkServerTrusted(X509Certificate[] certs, String authType) {}
  67. }
  68. };
  69. context.init(null, trustAllCerts, new SecureRandom());
  70. HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
  71. ClientConfig config = new DefaultClientConfig();
  72. config.getProperties().put(
  73. HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
  74. new HTTPSProperties(new HostnameVerifier() {
  75. public boolean verify(String s, SSLSession sslSession) {
  76. return true;
  77. }
  78. }, context)
  79. );
  80. return config;
  81. }
  82. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号