This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加 CredentialsProviderBuilder fix #17
HeadersPacker 重命名成 HttpRequestHeadersPacker fix #16 feilong-net-httpclient4 要支持 basic author 认证 fix #15
- Loading branch information
1 parent
ff4ab6f
commit 93fc97f
Showing
8 changed files
with
249 additions
and
121 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...client4/src/main/java/com/feilong/net/httpclient4/builder/CredentialsProviderBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (C) 2008 feilong | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.feilong.net.httpclient4.builder; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.auth.AuthScope; | ||
import org.apache.http.auth.UsernamePasswordCredentials; | ||
import org.apache.http.client.CredentialsProvider; | ||
import org.apache.http.impl.client.BasicCredentialsProvider; | ||
|
||
/** | ||
* The Class CredentialsProviderBuilder. | ||
* | ||
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a> | ||
* @since 1.11.0 | ||
*/ | ||
public class CredentialsProviderBuilder{ | ||
|
||
/** | ||
* Builds the credentials provider. | ||
* | ||
* @param targetHost | ||
* the target host | ||
* @param userName | ||
* the user name | ||
* @param password | ||
* the password | ||
* @return the credentials provider | ||
*/ | ||
public static CredentialsProvider build(HttpHost targetHost,String userName,String password){ | ||
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); | ||
return build(authScope, userName, password); | ||
} | ||
|
||
//--------------------------------------------------------------- | ||
|
||
/** | ||
* Builds the. | ||
* | ||
* @param authScope | ||
* the auth scope | ||
* @param userName | ||
* the user name | ||
* @param password | ||
* the password | ||
* @return the credentials provider | ||
*/ | ||
public static CredentialsProvider build(AuthScope authScope,String userName,String password){ | ||
return build(authScope, new UsernamePasswordCredentials(userName, password)); | ||
} | ||
|
||
/** | ||
* Builds the. | ||
* | ||
* @param authScope | ||
* the auth scope | ||
* @param credentials | ||
* the credentials | ||
* @return the credentials provider | ||
*/ | ||
public static CredentialsProvider build(AuthScope authScope,UsernamePasswordCredentials credentials){ | ||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | ||
credentialsProvider.setCredentials(authScope, credentials); | ||
return credentialsProvider; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
feilong-net-httpclient4/src/test/java/org/apache/http/auth/CredentialsAdidasPtsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2008 feilong | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.http.auth; | ||
|
||
import static com.feilong.core.CharsetType.UTF8; | ||
import static com.feilong.core.util.CollectionsUtil.newArrayList; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.NameValuePair; | ||
import org.apache.http.client.AuthCache; | ||
import org.apache.http.client.ClientProtocolException; | ||
import org.apache.http.client.CredentialsProvider; | ||
import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.protocol.HttpClientContext; | ||
import org.apache.http.impl.auth.BasicScheme; | ||
import org.apache.http.impl.client.BasicAuthCache; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.message.BasicNameValuePair; | ||
import org.apache.http.util.EntityUtils; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.feilong.net.httpclient4.builder.CredentialsProviderBuilder; | ||
|
||
public class CredentialsAdidasPtsTest{ | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CredentialsAdidasPtsTest.class); | ||
|
||
@Test | ||
public void testCredentialsTest5() throws ClientProtocolException,IOException{ | ||
CloseableHttpClient httpclient = HttpClients.createDefault(); | ||
|
||
List<NameValuePair> nameValuePairList = newArrayList(); | ||
nameValuePairList.add(new BasicNameValuePair("j_username", "xin.jin")); | ||
nameValuePairList.add(new BasicNameValuePair("j_password", "Aa123456")); | ||
|
||
HttpPost httpPost = new HttpPost("/j_spring_security_check"); | ||
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList)); | ||
|
||
HttpHost targetHost = new HttpHost("pts.adidas.com.cn", 443, "https"); | ||
HttpClientContext httpClientContext = buildHttpClientContext(targetHost); | ||
CloseableHttpResponse closeableHttpResponse = httpclient.execute(targetHost, httpPost, httpClientContext); | ||
|
||
LOGGER.debug(EntityUtils.toString(closeableHttpResponse.getEntity(), UTF8)); | ||
} | ||
|
||
//--------------------------------------------------------------- | ||
|
||
private HttpClientContext buildHttpClientContext(HttpHost targetHost){ | ||
CredentialsProvider credentialsProvider = CredentialsProviderBuilder.build(targetHost, "adidas", "ad20170731"); | ||
// Lookup<AuthSchemeProvider> authRegistry = <...> | ||
// // 创建 AuthCache 对象 | ||
AuthCache authCache = new BasicAuthCache(); | ||
// //创建 BasicScheme,并把它添加到 auth cache中 | ||
BasicScheme basicAuth = new BasicScheme(); | ||
authCache.put(targetHost, basicAuth); | ||
|
||
// 把AutoCache添加到上下文中 | ||
HttpClientContext httpClientContext = HttpClientContext.create(); | ||
httpClientContext.setCredentialsProvider(credentialsProvider); | ||
// context.setAuthSchemeRegistry(authRegistry); | ||
// context.setAuthCache(authCache); | ||
|
||
return httpClientContext; | ||
} | ||
|
||
} |
Oops, something went wrong.