Digest Auth 摘要認證

yiwanbin發表於2024-08-09

1、該程式碼展示了使用Apache HttpClient庫進行HTTP請求,並處理基於MD5的HTTP Digest認證的過程。 Digests類實現了MD5加密演算法,HttpUtils類處理了GET、POST方法的請求,包括設定請求頭、生成授權資訊和處理響應。

2、請求流程

2.1.傳送一個請求

2.2.伺服器返回401響應頭,要求輸入使用者憑據(建立通訊,發起請求。如果返回401,繼續下一次請求。重新設定響應,並獲取隨機數,透過md5加密返回)

2.3.輸入憑據後再傳送請求

2.4.服務端驗證透過後返回資料

2.4程式碼示例:本案例使用xml請求並返回xml資料響應,json請求同理

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>

package com.ywb.common.utils.http;

import com.ywb.common.constant.Constants;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.*;
import java.io.*;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;


/**
 * 通用http傳送方法
 *
 * @author ywb
 */
public class HttpUtils {
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * 向指定 URL 傳送GET方法的請求
     *
     * @param url   傳送請求的 URL
     * @param param 請求引數,請求引數應該是 name1=value1&name2=value2 的形式。
     * @return 所代表遠端資源的響應結果
     */
    public static String sendGet(String url, String param) {
        return sendGet(url, param, Constants.UTF8);
    }

    /**
     * 向指定 URL 傳送GET方法的請求
     *
     * @param url         傳送請求的 URL
     * @param param       請求引數,請求引數應該是 name1=value1&name2=value2 的形式。
     * @param contentType 編碼型別
     * @return 所代表遠端資源的響應結果
     */
    public static String sendGet(String url, String param, String contentType) {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            log.info("sendGet - {}", urlNameString);
            URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            log.info("recv - {}", result);
        } catch (ConnectException e) {
            log.error("呼叫HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
        } catch (SocketTimeoutException e) {
            log.error("呼叫HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
        } catch (IOException e) {
            log.error("呼叫HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
        } catch (Exception e) {
            log.error("呼叫HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                log.error("呼叫in.close Exception, url=" + url + ",param=" + param, ex);
            }
        }
        return result.toString();
    }

    /**
     * 向指定 URL 傳送POST方法的請求
     *
     * @param url   傳送請求的 URL
     * @param param 請求引數,請求引數應該是 name1=value1&name2=value2 的形式。
     * @return 所代表遠端資源的響應結果
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try {
            String urlNameString = url;
            log.info("sendPost - {}", urlNameString);
            URL realUrl = new URL(urlNameString);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("contentType", "utf-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            log.info("recv - {}", result);
        } catch (ConnectException e) {
            log.error("呼叫HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
        } catch (SocketTimeoutException e) {
            log.error("呼叫HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
        } catch (IOException e) {
            log.error("呼叫HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
        } catch (Exception e) {
            log.error("呼叫HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                log.error("呼叫in.close Exception, url=" + url + ",param=" + param, ex);
            }
        }
        return result.toString();
    }

    public static String sendSSLPost(String url, String param) {
        StringBuilder result = new StringBuilder();
        String urlNameString = url + "?" + param;
        try {
            log.info("sendSSLPost - {}", urlNameString);
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
            URL console = new URL(urlNameString);
            HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("contentType", "utf-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);

            conn.setSSLSocketFactory(sc.getSocketFactory());
            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String ret = "";
            while ((ret = br.readLine()) != null) {
                if (ret != null && !"".equals(ret.trim())) {
                    result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8"));
                }
            }
            log.info("recv - {}", result);
            conn.disconnect();
            br.close();
        } catch (ConnectException e) {
            log.error("呼叫HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
        } catch (SocketTimeoutException e) {
            log.error("呼叫HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e);
        } catch (IOException e) {
            log.error("呼叫HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
        } catch (Exception e) {
            log.error("呼叫HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e);
        }
        return result.toString();
    }

    /**
     * 摘要認證 兩次請求
     *
     * @param url
     * @return 返回結果
     */
    public static String doPostDigest(String url, String uri, String username, String password, String requestXml) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpPost httpPost = null;
        String strResponse = null;
        try {
            httpClient = HttpClients.createDefault();
//            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            // 構造請求頭
            httpPost.setHeader("Content-Type", "application/xml");
            httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
            httpPost.addHeader("Cache-Control", "no-cache"); //設定快取
            httpPost.setHeader("Connection", "Close");

            RequestConfig.Builder builder = RequestConfig.custom();
            builder.setSocketTimeout(5000); //設定請求時間
            builder.setConnectTimeout(8000); //設定超時時間
            builder.setRedirectsEnabled(false);//設定是否跳轉連結(反向代理)
            // 設定 連線 屬性
            httpPost.setConfig(builder.build());
            StringEntity entityss = new StringEntity(requestXml, "utf-8");
            httpPost.setEntity(entityss);
            // 執行請求
            response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            // 檢驗返回碼
            int statusCode = response.getStatusLine().getStatusCode();
            log.debug("第一次傳送摘要認證 Post請求 返回碼:{}" + statusCode);
            if (401 == statusCode) {
                strResponse = EntityUtils.toString(responseEntity, "utf-8");
                log.debug("Post請求401返回結果:{}" + strResponse);

                // 組織引數,發起第二次請求
                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";
                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 nc = "00000001";
                String cnonce = "uniview";
                // 後期變成可配置
                String a1 = username + ":" + realm + ":" + password;
                String a2 = method + ":" + uri;
                String response1 = null;
                // 獲取 Digest 這個字串
                String backString = response.getFirstHeader("WWW-Authenticate").getValue();
                try {
                    response1 = DigestUtils.md5Hex((DigestUtils.md5Hex(a1.getBytes("UTF-8")) + ":" + nonce + ":" + nc
                            + ":" + "uniview" + ":" + qop + ":" + DigestUtils.md5Hex(a2.getBytes("UTF-8"))).getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    log.error("MD5異常:{}" + e.getLocalizedMessage(), e);
                }
                httpPost.addHeader("Authorization", backString + ",username=\"" + username + "\"" + ",realm=\"" + realm + "\""
                        + ",nonce=\"" + nonce + "\"" + ",uri=\"" + uri + "\"" + ",qop=\"" + qop + "\"" + ",nc=\"" + nc + "\""
                        + ",cnonce=\"" + cnonce + "\"" + ",response=\"" + response1 + "\"" + ",opaque=\"" + opaque);

                // 傳送第二次請求
                response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                int statusCode1 = response.getStatusLine().getStatusCode();
                log.debug("第二次傳送摘要認證 Post請求 返回碼:{}" + statusCode1);
                if (HttpStatus.SC_OK == statusCode1) {
                    strResponse = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                    log.debug("第二次傳送strResponse" + strResponse);
                    return strResponse;
                } else {
                    strResponse = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                    log.debug("第二次鑑權認證請求非 200 返回結果:{}" + strResponse);
                    return strResponse;
                }
            } else {
                strResponse = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
                log.error("第一次鑑權認證請求非401 返回結果:{}" + strResponse);
            }
        } catch (Exception e) {
            log.error("摘要認證 傳送請求失敗" + e.getLocalizedMessage(), e);
        } finally {
            if (null != httpPost) {
                httpPost.releaseConnection();
            }
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    log.error("httpResponse流關閉異常:", e);
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    log.error("httpClient 流關閉異常:", e);
                }
            }
        }
        return strResponse;
    }


    private static class TrustAnyTrustManager implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }

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

    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    @Test
    public void testHttpPostRaw() {
        final String url = "http://18.18.167.120/ISAPI/Traffic/ContentMgmt/Statistic/operator";
        final String uri = "/ISAPI/Traffic/ContentMgmt/Statistic/operator";
        final String encode = "utf-8";
        String requestXml = "<?xml version='1.0' encoding='utf-8'?>" +
                "<StatisticOperator>" +
                "<operationType>search</operationType>" +
                "<searchCond>" +
                "<searchID>CAD8E0D6-1480-0001-C0B7-1E50E290140D</searchID>" +
                "<timeSpanList><timeSpan>" +
                "<startTime>2024-06-21T15:25:10Z</startTime>" +
                "<endTime>2024-06-21T15:27:59Z</endTime>" +
                "</timeSpan></timeSpanList>" +
                "<maxResults>20</maxResults>" +
                "<searchResultPosition>0</searchResultPosition>" +
                "</searchCond>" +
                "</StatisticOperator>";
        final HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        headers.put("X-Requested-With", "XMLHttpRequest");

        System.out.println(doPostDigest(url, uri, "admin", "123456", requestXml));
<?xml version="1.0" encoding="UTF-8"?>

-<StatisticOperatorResult xmlns="http://www.isapi.org/ver20/XMLSchema" version="2.0">

<statusCode>1</statusCode>

<description>OK</description>

<searchID>CAD8E0D6-1480-0001-C0B7-1E50E290140D</searchID>

<numOfMatches>2</numOfMatches>

<totalMatches>2</totalMatches>


-<StatisticList>


-<Statistic>

<ID max="65535" min="1">1</ID>

<direction>0</direction>

<laneNo>18</laneNo>

<vehicleType>0</vehicleType>

<startTime>2024-06-21T15:26:03</startTime>

<endTime>2024-06-21T15:27:03</endTime>

<timePoint>2024-06-21T15:26:03</timePoint>

<channel>1</channel>

<carFlux>2</carFlux>

<smallCarFlux>0</smallCarFlux>

<bigCarFlux>1</bigCarFlux>

<passerbyFlux>0</passerbyFlux>

<averOccpancy>26.2</averOccpancy>

<averSpeed>0</averSpeed>

<averCarDis>0</averCarDis>

<midCarFlux>0</midCarFlux>

<averTimeOccupancy>50</averTimeOccupancy>

<averQueueLength>35</averQueueLength>

<averTimeHeadway>102</averTimeHeadway>

<nonmotorFlux>1</nonmotorFlux>

</Statistic>


-<Statistic>

<ID max="65535" min="1">2</ID>

<direction>0</direction>

<laneNo>19</laneNo>

<vehicleType>0</vehicleType>

<startTime>2024-06-21T15:26:03</startTime>

<endTime>2024-06-21T15:27:03</endTime>

<timePoint>2024-06-21T15:26:03</timePoint>

<channel>1</channel>

<carFlux>0</carFlux>

<smallCarFlux>0</smallCarFlux>

<bigCarFlux>0</bigCarFlux>

<passerbyFlux>0</passerbyFlux>

<averOccpancy>0.0</averOccpancy>

<averSpeed>0</averSpeed>

<averCarDis>0</averCarDis>

<midCarFlux>0</midCarFlux>

<averTimeOccupancy>0</averTimeOccupancy>

<averQueueLength>0</averQueueLength>

<averTimeHeadway>0</averTimeHeadway>

<nonmotorFlux>0</nonmotorFlux>

</Statistic>


-<Statistic>

<ID max="65535" min="1">3</ID>

<direction>0</direction>

<laneNo>3</laneNo>

<vehicleType>0</vehicleType>

<startTime>2024-06-21T15:26:03</startTime>

<endTime>2024-06-21T15:27:03</endTime>

<timePoint>2024-06-21T15:26:03</timePoint>

<channel>1</channel>

<carFlux>0</carFlux>

<smallCarFlux>0</smallCarFlux>

<bigCarFlux>0</bigCarFlux>

<passerbyFlux>0</passerbyFlux>

<averOccpancy>0.0</averOccpancy>

<averSpeed>0</averSpeed>

<averCarDis>0</averCarDis>

<midCarFlux>0</midCarFlux>

<averTimeOccupancy>0</averTimeOccupancy>

<averQueueLength>0</averQueueLength>

<averTimeHeadway>0</averTimeHeadway>

<nonmotorFlux>0</nonmotorFlux>

</Statistic>


-<Statistic>

<ID max="65535" min="1">4</ID>

<direction>0</direction>

<laneNo>18</laneNo>

<vehicleType>0</vehicleType>

<startTime>2024-06-21T15:27:03</startTime>

<endTime>2024-06-21T15:28:03</endTime>

<timePoint>2024-06-21T15:27:03</timePoint>

<channel>1</channel>

<carFlux>1</carFlux>

<smallCarFlux>0</smallCarFlux>

<bigCarFlux>1</bigCarFlux>

<passerbyFlux>0</passerbyFlux>

<averOccpancy>27.0</averOccpancy>

<averSpeed>0</averSpeed>

<averCarDis>0</averCarDis>

<midCarFlux>0</midCarFlux>

<averTimeOccupancy>98</averTimeOccupancy>

<averQueueLength>35</averQueueLength>

<averTimeHeadway>54</averTimeHeadway>

<nonmotorFlux>0</nonmotorFlux>

</Statistic>


-<Statistic>

<ID max="65535" min="1">5</ID>

<direction>0</direction>

<laneNo>19</laneNo>

<vehicleType>0</vehicleType>

<startTime>2024-06-21T15:27:03</startTime>

<endTime>2024-06-21T15:28:03</endTime>

<timePoint>2024-06-21T15:27:03</timePoint>

<channel>1</channel>

<carFlux>0</carFlux>

<smallCarFlux>0</smallCarFlux>

<bigCarFlux>0</bigCarFlux>

<passerbyFlux>0</passerbyFlux>

<averOccpancy>0.0</averOccpancy>

<averSpeed>0</averSpeed>

<averCarDis>0</averCarDis>

<midCarFlux>0</midCarFlux>

<averTimeOccupancy>0</averTimeOccupancy>

<averQueueLength>0</averQueueLength>

<averTimeHeadway>0</averTimeHeadway>

<nonmotorFlux>0</nonmotorFlux>

</Statistic>


-<Statistic>

<ID max="65535" min="1">6</ID>

<direction>0</direction>

<laneNo>3</laneNo>

<vehicleType>0</vehicleType>

<startTime>2024-06-21T15:27:03</startTime>

<endTime>2024-06-21T15:28:03</endTime>

<timePoint>2024-06-21T15:27:03</timePoint>

<channel>1</channel>

<carFlux>0</carFlux>

<smallCarFlux>0</smallCarFlux>

<bigCarFlux>0</bigCarFlux>

<passerbyFlux>0</passerbyFlux>

<averOccpancy>0.0</averOccpancy>

<averSpeed>0</averSpeed>

<averCarDis>0</averCarDis>

<midCarFlux>0</midCarFlux>

<averTimeOccupancy>0</averTimeOccupancy>

<averQueueLength>0</averQueueLength>

<averTimeHeadway>0</averTimeHeadway>

<nonmotorFlux>0</nonmotorFlux>

</Statistic>

</StatisticList>

</StatisticOperatorResult>

相關文章