apache oltu https ssl

对接https接口中遇到的证书问题解决,无须安装

    使用apache-oltu client demo对接



package cn.com.xxxxx.common.utils;


import java.net.Socket;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Map;


import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;


import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
import org.apache.oltu.oauth2.client.response.OAuthResourceResponse;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.message.types.GrantType;


public class AccessTokenUtil {

    
protected static void init() {
  try {
   SSLContext sc = SSLContext.getInstance("SSL");
   HostnameVerifier hv = new HostnameVerifier() {
    public boolean verify(String urlHostName, SSLSession session) {
     return true;
    }
   };
   TrustManager[] trustAllCerts = new TrustManager[] {
   new X509ExtendedTrustManager() {
           @Override
           public void checkClientTrusted(X509Certificate[] 
   x509Certificates, String s) { }


           @Override
           public void checkServerTrusted(X509Certificate[] 
   x509Certificates, String s) { }


           @Override
           public X509Certificate[] getAcceptedIssuers() {
               return null;
           }


           @Override
           public void checkClientTrusted(X509Certificate[] 
               x509Certificates, String s, Socket socket) {
           }


           @Override
           public void checkServerTrusted(X509Certificate[] 
               x509Certificates, String s, Socket socket) {
           }


           @Override
           public void checkClientTrusted(X509Certificate[] 
                x509Certificates, String s, SSLEngine sslEngine) {
           }


           @Override
           public void checkServerTrusted(X509Certificate[] 
              x509Certificates, String s, SSLEngine sslEngine) {
           }
       } };
   sc.init(null, trustAllCerts, new SecureRandom());


   SSLContext.setDefault(sc);
   HttpsURLConnection.setDefaultHostnameVerifier(hv);
  } catch (Exception e) {
   e.printStackTrace();
  }
}

public static String getResultByToken(Map<String,String> paramMap,String requestUrl,String requestBody){
init();
try {
OAuthClientRequest  request = OAuthClientRequest.tokenLocation(paramMap.get("access_token_url")).setClientId(paramMap.get("client_id"))
.setGrantType(GrantType.PASSWORD).setUsername(paramMap.get("username")).setPassword(paramMap.get("password")).setClientSecret(paramMap.get("client_secret")).buildQueryMessage();
    System.out.println(request.getLocationUri());
    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
    //OAuthAccessTokenResponse resourceResponse = oAuthClient.accessToken(request);
//OAuthResourceResponse resourceResponse = oAuthClient.resource(request, OAuth.HttpMethod.POST, OAuthResourceResponse.class);
//System.out.println(resourceResponse.getBody());
    //Map requestBodyMap = JSON.parseObject(resourceResponse.getBody(), Map.class);
    oAuthClient.accessToken(request);
       OAuthJSONAccessTokenResponse tokenResponse = (OAuthJSONAccessTokenResponse) oAuthClient
         .accessToken(request, OAuthJSONAccessTokenResponse.class);
       
    OAuthClientRequest bearerClientRequest = new OAuthBearerClientRequest(requestUrl)
        .setAccessToken(tokenResponse.getAccessToken())
    .buildQueryMessage();
bearerClientRequest.setHeader(OAuth.HeaderType.CONTENT_TYPE, "application/json;charset=utf8");
//bearerClientRequest.setHeader(OAuth.HeaderType.AUTHORIZATION, "Bearer "+tokenResponse.getAccessToken());
bearerClientRequest.setBody(requestBody);
OAuthResourceResponse resourceResponse = oAuthClient.resource(bearerClientRequest, OAuth.HttpMethod.POST, OAuthResourceResponse.class);
return resourceResponse.getBody();
   
    } catch (Exception e) {
e.printStackTrace();
}
return "error";
}
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuanjin1325887562/article/details/79947589
文章标签: java https ssl api
个人分类: java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭