Apache-HTTPClient 用户验证实现
2016-12-01 11:05
351 查看
问题:在微服务框架之外的系统中,我们经常会遇到使用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; } }
相关文章推荐
- JSP开发中Apache-HTTPClient 用户验证的实例详解
- Apache服务器实现用户验证
- Apache服务器实现的用户验证
- HttpModule实现用户身份验证
- 使用HttpContext的User属性来实现用户验证-.NET教程
- Linux 下实现 Apache服务器用户验证
- 使用HttpContext的User属性来实现用户身份验证
- apache用户验证板块,实施HTTP安全性
- HttpMoudle实现用户身份验证
- 使用HttpContext的User属性来实现用户身份验证
- Apache httpclient-4.1.1 实现 Session保持的解决方法
- 使用HttpContext的User属性来实现用户验证
- 使用HttpContext的User属性来实现用户验证
- 使用HttpContext中的User属性来实现用户身份验证之用户验证票篇
- Apache服务器实现用户验证
- 使用HttpContext的User属性来实现用户验证
- 使用HttpContext的User属性来实现用户验证
- 使用HttpContext中的User属性来实现用户身份验证之用户验证票篇
- 使用HttpContext中的User属性来实现用户身份验证之用户验证票篇[转载]
- httpclient 实现自动登录NTLM 域验证(sso)