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

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


问题描述

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


解决方案:

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

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.HmacUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.net.ssl.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Random;


@Service
@Slf4j
public class AccessService {


     

    public String postUrl(String url , String param){
        log.info("postUrl-url:{},param:{}",url,param);
        
        Client client = null;
        ClientResponse response = null;

        try {
            ClientConfig config = init();
            client = Client.create(config);
            client.setFollowRedirects(true);
            WebResource webResource = client.resource(url);

            response = webResource.header("Authorization",authHeader).type("application/json").post(ClientResponse.class, param);
            String jsonString = response.getEntity(String.class);
            log.warn("http返回码:{},http消息体为:{}" , response.getStatus(),jsonString);
            if (response.getStatus() == 200) {

                return  jsonString;
            } else {
                throw new RuntimeException( "http请求异常,http返回码:" + response.getStatus() + ";http消息体为:" + jsonString);
            }

        }catch (Exception ex){
            log.warn("postUrl:"+ex.getMessage());
        }finally {
            if (response != null) {
                response.close();
            }
            if (client != null) {
                client.destroy();
            }
        }

        return "";
    }
    /**
     * 跳过https
     * */
    public static ClientConfig init() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext context = SSLContext.getInstance("SSL");
        TrustManager[] trustAllCerts = new TrustManager[] {
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                    @Override
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                    @Override
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                }
        };

        context.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

        ClientConfig config = new DefaultClientConfig();
        config.getProperties().put(
                HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
                new HTTPSProperties(new HostnameVerifier() {
                    public boolean verify(String s, SSLSession sslSession) {
                        return true;
                    }
                }, context)
        );
        return config;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值