在对接的时候用到了Auth2.0,对方用的C#写的项目并给我发了如下的demo:
官网:https://getkong.org/plugins/oauth2-authentication/
会linux的应该能看懂,直接在linux里执行却不通,后来在¥ curl 后加 "-k"之后就访问通了,原来是因为他用的HTTPS,需要认证,但是没有证书,所以要加上忽略
服务器认证证书,在linux中也就是-K 忽略证书。加上之后linux果然访问通了,但是我的项目是用java写的呀,他也没有java的demo,最后终于让我搞出一个
代码见下:
运行这个方法会返回code 和 token,当然此方法调用了封装的类,
上一个方法调用的封装类如下
public class OKHttpUtilForSelfSSL {
private static final String GET = "get";
private static final String POST = "post";
private static final String PUT = "put";
private static final String DELETE = "delete";
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final OKHttpUtilForSelfSSL okhttpUtil = new OKHttpUtilForSelfSSL();
private static OkHttpClient client;
private OKHttpUtilForSelfSSL() {
final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
X509Certificate[] x509Certificates = new X509Certificate[0];
return x509Certificates;
}
} };
SSLContext sslContext;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
client = new OkHttpClient();
client.setSslSocketFactory(sslContext.getSocketFactory());
client.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static final OKHttpUtilForSelfSSL Builder() {
return okhttpUtil;
}
public Object get(String url) throws Exception {
return get(url, null);
}
public Object get(String url, Map<String, String> headers) throws Exception {
return responseBody(url, GET, null, headers);
}
public String getAndReturnJson(String url) throws Exception {
return getAndReturnJson(url, new HashMap<String, String>());
}
public String getAndReturnJson(String url, Map<String, String> headers) throws Exception {
Object obj = responseBody(url, GET, null, headers);
return obj == null ? null : JacksonUtil.Builder().obj2Json(obj);
}
public Object post(String url, String json) throws Exception {
return post(url, json, new HashMap<String, String>());
}
public Object post(String url, String json, Map<String, String> headers) throws Exception {
return responseBody(url, POST, json, headers);
}
public String postAndReturnJson(String url, String json) throws Exception {
return postAndReturnJson(url, json, new HashMap<String, String>());
}
public String postAndReturnJson(String url, String json, Map<String, String> headers) throws Exception {
Object obj = responseBody(url, POST, json, headers);
return obj == null ? null : JacksonUtil.Builder().obj2Json(obj);
}
private Object responseBody(String url, String type, String json, Map<String, String> headers) throws Exception {
Builder builder = new Request.Builder().url(url);
Set<Entry<String, String>> set = headers.entrySet();
Iterator<Entry<String, String>> iter = set.iterator();
Entry<String, String> entry = null;
while (iter.hasNext()) {
entry = iter.next();
builder.addHeader(entry.getKey(), entry.getValue());
System.out.println(builder);
}
RequestBody body = null;
Request request = null;
Response response = null;
Object value = null;
if (json != null) {
body = RequestBody.create(JSON, json);
}
switch (type) {
case GET:
request = builder.build();
break;
case POST:
request = builder.post(body).build();
break;
case PUT:
request = builder.put(body).build();
break;
case DELETE:
if (body == null) {
request = builder.delete().build();
} else {
request = builder.delete(body).build();
}
break;
default:
break;
}
response = client.newCall(request).execute();
if (response.isSuccessful()) {
value = response.body().string();
} else {
throw new RuntimeException("状态码:" + response.code() + ", message:" + response.message());
}
return value;
}
}