HttpClient完成Basic Authentication认证
来源:互联网 发布:贵阳公安数据铁笼 编辑:程序博客网 时间:2024/05/18 03:13
有的平台登陆验证是Http Basic Authentication认证,如下输入用户名、密码完成验证。
如何通过java实现呢,这里仅贴出核心代码。
private CloseableHttpClient createCloseableHttpClientWithBasicAuth(CookieStore cookieStore){ if (httpClientBuilder == null) { // 创建HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); } // 设置BasicAuth CredentialsProvider provider = new BasicCredentialsProvider(); // Create the authentication scope AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); // Create credential pair,在此处填写用户名和密码 logger.info(configBean.getKpmUserName()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(configBean.getKpmUserName(), configBean.getKpmPassWord()); // Inject the credentials provider.setCredentials(scope, credentials); // Set the default credentials provider httpClientBuilder.setDefaultCredentialsProvider(provider); // Set the default cookieStore httpClientBuilder.setDefaultCookieStore(cookieStore); // HttpClient return httpClientBuilder.build(); }
所依赖的三方库如下:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.8</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency>
实际请求过程中,添加CookieStore时,运行提示异常。
// 设置CookieStore CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", jsessionId); // version4.5 bug fix,should set domain and attribute,otherwise return java.lang.NullPointerException // cookie.setDomain(".my.domain.com"); // cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true"); cookieStore.addCookie(cookie); CloseableHttpClient closeableHttpClient = createCloseableHttpClientWithBasicAuth(cookieStore);
异常信息:
2017-12-23 19:33:14,993 ERROR DtsTaskProcessor-173993-com.amap.chexian.yunxiao.schedulerx.JobDemo-7263912075-2017-12-23 19:33:00-0 -[SimpleTaskProcessor]: process error, instanceId:7263912075 com.alibaba.dts.client.executor.simple.processor.SimpleTaskProcessor? java.lang.NullPointerException: null at org.apache.http.impl.cookie.PublicSuffixDomainFilter.match(PublicSuffixDomainFilter.java:93) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.impl.cookie.CookieSpecBase.match(CookieSpecBase.java:136) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.impl.cookie.DefaultCookieSpec.match(DefaultCookieSpec.java:178) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:166) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:133) ~[httpcore-4.4.8.jar:4.4.8] at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:183) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[httpclient-4.5.3.jar:4.5.3] at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) ~[httpclient-4.5.3.jar:4.5.3] at com.amap.chexian.yunxiao.service.impl.HttpClientServiceImpl.doGetMethod(HttpClientServiceImpl.java:124) ~[classes/:na] at com.amap.chexian.yunxiao.service.impl.HttpClientServiceImpl.downloadFromKpm(HttpClientServiceImpl.java:54) ~[classes/:na] at com.amap.chexian.yunxiao.schedulerx.JobDemo.process(JobDemo.java:57) ~[classes/:na] at com.alibaba.dts.client.executor.simple.processor.SimpleTaskProcessor.executeTask(SimpleTaskProcessor.java:133) ~[schedulerx-client-2.0.15.jar:0.0.2-SNAPSHOT] at com.alibaba.dts.client.executor.simple.processor.SimpleTaskProcessor.run(SimpleTaskProcessor.java:90) ~[schedulerx-client-2.0.15.jar:0.0.2-SNAPSHOT]
经过查阅,是由于需要设置cookie的Domain及Attribute属性,这个问题是httpclient v4.5里面需要注意的。参考文末的链接。
cookie.setDomain(".my.domain.com");cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
https://stackoverflow.com/questions/29970409/cookies-getting-ignored-in-apache-httpclient-4-4
参考链接:
1、http响应状态码
2、PublicSuffixDomainFilter.java空指针异常说明
阅读全文
0 0
- HttpClient完成Basic Authentication认证
- HttpClient Basic Authentication
- HTTP Basic Authentication认证
- HTTP Basic Authentication认证
- HTTP Basic Authentication认证
- HTTP Basic 认证(Authentication)
- HTTP Basic Authentication认证
- httpclient basic认证
- http 基础认证(Basic Authentication)
- HTTP基础认证Basic Authentication
- HTTP基本认证(Basic Authentication)
- java添加HTTP基本认证(Basic Authentication)
- Go实战--通过basic认证的http(basic authentication)
- HttpClient学习之三实现Basic认证
- HttpClient BA认证 basic auth 实例
- Basic and Digest Access Authentication (rfc2617) 及HttpClient实现
- Basic and Digest Access Authentication (rfc2617) 及HttpClient实现
- 关于Basic,Digest and NTLM Authentication认证的问题
- 在Eclipse中使用Git
- 深度学习优化算法(sgd/momentum/Nesterov/adagrad/adadelta)
- 使用git将项目上传到github(最简单方法)
- 浅谈IT行业风投
- can't call void android.view.View.setElevation(float) on null obj in call to CallVoidMethodV
- HttpClient完成Basic Authentication认证
- 源码剖析之wait queue
- yii2 展示静态页面
- oracle bfilename <Value Error>
- 【Mysql】mysql 事务未提交导致死锁 Lock wait timeout exceeded; try restarting transaction 解决办法
- SQL Server 中WITH (NOLOCK)浅析
- mysql5.6深入解决编码乱码问题
- POJ:3041-Asteroids(匈牙利算法模板)
- 关于微信公众号获取token的学习总结