国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP開發中Apache-HTTPClient 用戶驗證的實例詳解

2024-09-05 00:23:17
字體:
來源:轉載
供稿:網友

JSP開發中Apache-HTTPClient 用戶驗證的實例詳解

前言:

在微服務框架之外的系統中,我們經常會遇到使用httpClient進行接口調用的問題,除了進行白名單的設置,很多時候我們需要在接口調用的時候需要身份認證。翻了一下官方文檔,解決方法很多,但是都不太符合實際業務場景,這里提供一種簡單粗暴的解決方法。

解決方法:利用請求頭,將驗證信息保存起來。

實現代碼:

public class HttpClientUtils {  protected static final Logger LOG = LoggerFactory.getLogger(HttpClientUtils.class);  private static final String AUTHENKEY = "Authorization";  private static final String BASICKEY = "Basic ";  public static String getConnect(String url,String username,String password) {    CloseableHttpResponse response = null;    CloseableHttpClient client = HttpClients.createDefault();    HttpGet httpGet = new HttpGet(url);    Base64 token = new Base64();    String authenticationEncoding = token.encodeAsString(new String(username + ":" + password).getBytes());    httpGet.setHeader(AUTHENKEY, BASICKEY + authenticationEncoding);    String responseContent = "";    try {      response = client.execute(httpGet);      HttpEntity entity = response.getEntity();      responseContent = EntityUtils.toString(entity, "UTF-8");    } catch (IOException e) {      LOG.error(e.toString());    } finally {      if (response != null) {        try {          response.close();        } catch (IOException e) {          LOG.error(e.toString());        }      }      if (client != null) {        try {          client.close();        } catch (IOException e) {          LOG.error(e.toString());        }      }    }    return responseContent;  }}

以上就是Apache-HTTPClient 用戶驗證的實例,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到JSP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新干县| 丹寨县| 乌海市| 泰来县| 磐石市| 安达市| 长兴县| 富川| 宜宾市| 灵宝市| 彭阳县| 文安县| 深州市| 柳林县| 旌德县| 永年县| 怀宁县| 旅游| 西和县| 安宁市| 嘉祥县| 中牟县| 临西县| 通海县| 万安县| 蒲江县| 五大连池市| 荔浦县| 广昌县| 辉县市| 玛纳斯县| 武夷山市| 宜阳县| 安徽省| 章丘市| 秀山| 寿光市| 四会市| 长子县| 新密市| 洛扎县|