java模拟Digest.认证
生活随笔
收集整理的這篇文章主要介紹了
java模拟Digest.认证
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public static int sendPost(String url, String parameters,String username,String password ,String id) {String result = "";BufferedReader in = null;// 讀取響應(yīng)輸入流PrintWriter out = null;StringBuffer sb = new StringBuffer();// 處理請(qǐng)求參數(shù)String params = "";// 編碼之后的參數(shù)String ip= "";String port= "";int resCode = 404;try { CredentialsProvider credsProvider = new BasicCredentialsProvider();credsProvider.setCredentials(new AuthScope(ip, port), // 請(qǐng)求地址 + 端口號(hào)new UsernamePasswordCredentials(username, password));// 用戶名 + 密碼 (用于驗(yàn)證)HttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();try {SSLContext sslContext = SSLContextBuilder.create().useProtocol(SSLConnectionSocketFactory.SSL).loadTrustMaterial((x, y) -> true).build();RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).setSSLContext(sslContext).setSSLHostnameVerifier((x, y) -> true).build();} catch (Exception e) {e.printStackTrace();}HttpPost postMethod = new HttpPost(url);// 請(qǐng)求詳細(xì)地址(如:http://192.168.1.105:9000/MotorVehicles)//根據(jù)不通要求自己添加頭postMethod.addHeader("User-Identify", id);postMethod.addHeader("Content-Type", "application/json");StringEntity s = new StringEntity(parameters);// 向后臺(tái)傳的json數(shù)據(jù)s.setContentEncoding("utf-8");// 編碼postMethod.setEntity(s);HttpResponse response = httpclient.execute(postMethod); // 執(zhí)行POST方法resCode = response.getStatusLine().getStatusCode();System.out.println("resCode = " + resCode); // 獲取響應(yīng)碼
// System.out.println("result = " + EntityUtils.toString(response.getEntity(), "utf-8")); // 獲取響應(yīng)內(nèi)容String resultfirst = EntityUtils.toString(response.getEntity(), "utf-8");logger.info("result = " + resultfirst); // 獲取響應(yīng)內(nèi)容 if(resCode == 401) {String a = resultfirst;// 組織參數(shù),發(fā)起第二次請(qǐng)求Header[] headers = response.getHeaders("WWW-Authenticate");HeaderElement[] elements = headers[0].getElements();String realm = null;String qop = null;String nonce = null;String opaque = null;String method = "POST";String uri = url;for (HeaderElement element : elements) {if (element.getName().equals("Digest realm")) {realm = element.getValue();} else if (element.getName().equals("qop")) {qop = element.getValue();} else if (element.getName().equals("nonce")) {nonce = element.getValue();} else if (element.getName().equals("opaque")) {opaque = element.getValue();}}String a1 = username + ":" + realm + ":" + password;String a2 = method + ":" + uri;String response1 = null;String nc = "00000001";String cnonce = "uniview";// 獲取 Digest 這個(gè)字符串String backString = response.getFirstHeader("WWW-Authenticate").getValue();try {response1 = DigestUtils.md5DigestAsHex((DigestUtils.md5DigestAsHex(a1.getBytes("UTF-8")) + ":" + nonce + ":" + nc+ ":" + "uniview" + ":" + qop + ":" + DigestUtils.md5DigestAsHex(a2.getBytes("UTF-8"))).getBytes("UTF-8"));} catch (UnsupportedEncodingException e) {System.out.println("md5加密異常"+e);}postMethod.addHeader("Authorization", backString + ",username=\"" + username + "\"" + ",realm=\"" + realm + "\""+ ",nonce=\"" + nonce + "\"" + ",uri=\"" + uri + "\"" + ",qop=\"" + qop + "\"" + ",nc=\"" + nc + "\""+ ",cnonce=\"" + cnonce + "\"" + ",response=\"" + response1 + "\"" + ",opaque=\"" + opaque);// 發(fā)送第二次請(qǐng)求response = httpclient.execute(postMethod);resCode = response.getStatusLine().getStatusCode();//獲取返回響應(yīng)碼System.out.println(resCode);if (HttpStatus.SC_OK == resCode) {logger.info("result = " + EntityUtils.toString(response.getEntity(), "utf-8")); // 獲取響應(yīng)內(nèi)容 return resCode;} else {}} else {}} catch (Exception e) {System.out.println(e);}return resCode;}
``
對(duì)應(yīng) 的xml<dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.10</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>6.3.1</version><exclusions><exclusion><artifactId>elasticsearch-rest-client</artifactId><groupId>org.elasticsearch.client</groupId></exclusion><exclusion><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></exclusion></exclusions></dependency>
<dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version>
</dependency><dependency><artifactId>elasticsearch-rest-client</artifactId><groupId>org.elasticsearch.client</groupId><version>6.3.1</version></dependency><dependency><artifactId>elasticsearch</artifactId><groupId>org.elasticsearch</groupId><version>6.3.1</version></dependency>
總結(jié)
以上是生活随笔為你收集整理的java模拟Digest.认证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven 项目报错Context in
- 下一篇: 解决在freemarker中报空问题