本文整理汇总了Java中org.apache.http.auth.AUTH类的典型用法代码示例。如果您正苦于以下问题:Java AUTH类的具体用法?Java AUTH怎么用?Java AUTH使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AUTH类属于org.apache.http.auth包,在下文中一共展示了AUTH类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: authenticate
import org.apache.http.auth.AUTH; //导入依赖的package包/类
/**
* Returns a Diadoc <tt>Authorization</tt> header value for the given
* {@link DiadocCredentials}.
*
* @param credentials The credentials to encode.
* @return a Diadoc authorization header
*/
public static Header authenticate(final DiadocCredentials credentials) {
if (credentials == null) {
throw new IllegalArgumentException("Credentials may not be null");
}
StringBuilder sb = new StringBuilder();
sb.append("ddauth_api_client_id=");
sb.append(credentials.getApiClientId());
if (credentials.getAuthToken() != null) {
sb.append(",ddauth_token=");
sb.append(credentials.getAuthToken());
}
CharArrayBuffer buffer = new CharArrayBuffer(128);
buffer.append(AUTH.WWW_AUTH_RESP);
buffer.append(": DiadocAuth ");
buffer.append(sb.toString());
return new BufferedHeader(buffer);
}
示例2: testAuthentication
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthentication() throws Exception {
final HttpHost host = new HttpHost("somehost", 80);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm1\", nonce=\"1234\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "whatever realm=\"realm1\", stuff=\"1234\""));
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
Assert.assertTrue(this.httpAuthenticator.handleAuthChallenge(host,
response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.CHALLENGED, this.authState.getState());
final Queue<AuthOption> options = this.authState.getAuthOptions();
Assert.assertNotNull(options);
final AuthOption option1 = options.poll();
Assert.assertNotNull(option1);
Assert.assertEquals("digest", option1.getAuthScheme().getSchemeName());
final AuthOption option2 = options.poll();
Assert.assertNotNull(option2);
Assert.assertEquals("basic", option2.getAuthScheme().getSchemeName());
Assert.assertNull(options.poll());
}
示例3: testAuthenticationFailed
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthenticationFailed() throws Exception {
final HttpHost host = new HttpHost("somehost", 80);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm1\", nonce=\"1234\""));
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.update(this.authScheme, this.credentials);
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(host,
response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.FAILURE, this.authState.getState());
Mockito.verify(this.authCache).remove(host);
}
示例4: testAuthenticationFailedPreviously
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthenticationFailedPreviously() throws Exception {
final HttpHost host = new HttpHost("somehost", 80);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm1\", nonce=\"1234\""));
this.authState.setState(AuthProtocolState.FAILURE);
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(host,
response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.FAILURE, this.authState.getState());
}
示例5: testAuthenticationFailure
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthenticationFailure() throws Exception {
final HttpHost host = new HttpHost("somehost", 80);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm1\", nonce=\"1234\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "whatever realm=\"realm1\", stuff=\"1234\""));
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.update(new BasicScheme(), this.credentials);
Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(host,
response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.FAILURE, this.authState.getState());
Assert.assertNull(this.authState.getCredentials());
}
示例6: testAuthenticationHandshaking
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthenticationHandshaking() throws Exception {
final HttpHost host = new HttpHost("somehost", 80);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm1\", stale=true, nonce=\"1234\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "whatever realm=\"realm1\", stuff=\"1234\""));
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.update(new DigestScheme(), this.credentials);
Assert.assertTrue(this.httpAuthenticator.handleAuthChallenge(host,
response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.HANDSHAKE, this.authState.getState());
}
示例7: testAuthenticationNoMatchingChallenge
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthenticationNoMatchingChallenge() throws Exception {
final HttpHost host = new HttpHost("somehost", 80);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm1\", nonce=\"1234\""));
response.addHeader(new BasicHeader(AUTH.WWW_AUTH, "whatever realm=\"realm1\", stuff=\"1234\""));
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.update(new BasicScheme(), this.credentials);
Assert.assertTrue(this.httpAuthenticator.handleAuthChallenge(host,
response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.CHALLENGED, this.authState.getState());
final Queue<AuthOption> options = this.authState.getAuthOptions();
Assert.assertNotNull(options);
final AuthOption option1 = options.poll();
Assert.assertNotNull(option1);
Assert.assertEquals("digest", option1.getAuthScheme().getSchemeName());
Assert.assertNull(options.poll());
}
示例8: testAuthChallengeStateNoOption
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthChallengeStateNoOption() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.update(this.authScheme, this.credentials);
Mockito.when(this.authScheme.authenticate(
Mockito.any(Credentials.class),
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(AUTH.WWW_AUTH_RESP, "stuff"));
this.httpAuthenticator.generateAuthResponse(request, authState, context);
Assert.assertTrue(request.containsHeader(AUTH.WWW_AUTH_RESP));
Mockito.verify(this.authScheme).authenticate(this.credentials, request, this.context);
}
示例9: testAuthChallengeStateOneOptions
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthChallengeStateOneOptions() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
this.authState.setState(AuthProtocolState.CHALLENGED);
final LinkedList<AuthOption> authOptions = new LinkedList<AuthOption>();
authOptions.add(new AuthOption(this.authScheme, this.credentials));
this.authState.update(authOptions);
Mockito.when(this.authScheme.authenticate(
Mockito.any(Credentials.class),
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(AUTH.WWW_AUTH_RESP, "stuff"));
this.httpAuthenticator.generateAuthResponse(request, authState, context);
Assert.assertSame(this.authScheme, this.authState.getAuthScheme());
Assert.assertSame(this.credentials, this.authState.getCredentials());
Assert.assertNull(this.authState.getAuthOptions());
Assert.assertTrue(request.containsHeader(AUTH.WWW_AUTH_RESP));
Mockito.verify(this.authScheme).authenticate(this.credentials, request, this.context);
}
示例10: testAuthSuccess
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthSuccess() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
this.authState.setState(AuthProtocolState.SUCCESS);
this.authState.update(this.authScheme, this.credentials);
Mockito.when(this.authScheme.isConnectionBased()).thenReturn(Boolean.FALSE);
Mockito.when(this.authScheme.authenticate(
Mockito.any(Credentials.class),
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(AUTH.WWW_AUTH_RESP, "stuff"));
this.httpAuthenticator.generateAuthResponse(request, authState, context);
Assert.assertSame(this.authScheme, this.authState.getAuthScheme());
Assert.assertSame(this.credentials, this.authState.getCredentials());
Assert.assertNull(this.authState.getAuthOptions());
Assert.assertTrue(request.containsHeader(AUTH.WWW_AUTH_RESP));
Mockito.verify(this.authScheme).authenticate(this.credentials, request, this.context);
}
示例11: testAuthSuccessConnectionBased
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testAuthSuccessConnectionBased() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
this.authState.setState(AuthProtocolState.SUCCESS);
this.authState.update(this.authScheme, this.credentials);
Mockito.when(this.authScheme.isConnectionBased()).thenReturn(Boolean.TRUE);
Mockito.when(this.authScheme.authenticate(
Mockito.any(Credentials.class),
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(AUTH.WWW_AUTH_RESP, "stuff"));
this.httpAuthenticator.generateAuthResponse(request, authState, context);
Assert.assertFalse(request.containsHeader(AUTH.WWW_AUTH_RESP));
Mockito.verify(this.authScheme, Mockito.never()).authenticate(
Mockito.any(Credentials.class),
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class));
}
示例12: testProcessChallenge
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testProcessChallenge() throws Exception {
final TestAuthScheme authscheme = new TestAuthScheme();
final Header header = new BasicHeader(
AUTH.WWW_AUTH,
"Test realm=\"realm1\", test, test1 = stuff, test2 = \"stuff, stuff\", test3=\"crap");
authscheme.processChallenge(header);
Assert.assertEquals("test", authscheme.getSchemeName());
Assert.assertEquals("TEST", authscheme.toString());
Assert.assertEquals("realm1", authscheme.getParameter("realm"));
Assert.assertEquals(null, authscheme.getParameter("test"));
Assert.assertEquals("stuff", authscheme.getParameter("test1"));
Assert.assertEquals("stuff, stuff", authscheme.getParameter("test2"));
Assert.assertEquals("crap", authscheme.getParameter("test3"));
Assert.assertEquals(null, authscheme.getParameter(null));
}
示例13: testSerialization
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testSerialization() throws Exception {
final Header challenge = new BasicHeader(AUTH.WWW_AUTH, "test realm=\"test\", blah=blah, yada=\"yada yada\"");
final TestAuthScheme testScheme = new TestAuthScheme(Consts.ISO_8859_1);
testScheme.processChallenge(challenge);
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(testScheme);
out.flush();
final byte[] raw = buffer.toByteArray();
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(raw));
final TestAuthScheme authScheme = (TestAuthScheme) in.readObject();
Assert.assertEquals(Consts.ISO_8859_1, authScheme.getCredentialsCharset());
Assert.assertEquals("test", authScheme.getParameter("realm"));
Assert.assertEquals("blah", authScheme.getParameter("blah"));
Assert.assertEquals("yada yada", authScheme.getParameter("yada"));
}
示例14: testDigestAuthenticationWithDefaultCreds
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testDigestAuthenticationWithDefaultCreds() throws Exception {
final String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
final Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
final HttpRequest request = new BasicHttpRequest("Simple", "/");
final Credentials cred = new UsernamePasswordCredentials("username","password");
final DigestScheme authscheme = new DigestScheme();
final HttpContext context = new BasicHttpContext();
authscheme.processChallenge(authChallenge);
final Header authResponse = authscheme.authenticate(cred, request, context);
Assert.assertTrue(authscheme.isComplete());
Assert.assertFalse(authscheme.isConnectionBased());
final Map<String, String> table = parseAuthResponse(authResponse);
Assert.assertEquals("username", table.get("username"));
Assert.assertEquals("realm1", table.get("realm"));
Assert.assertEquals("/", table.get("uri"));
Assert.assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
Assert.assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response"));
}
示例15: testDigestAuthentication
import org.apache.http.auth.AUTH; //导入依赖的package包/类
@Test
public void testDigestAuthentication() throws Exception {
final String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
final Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
final HttpRequest request = new BasicHttpRequest("Simple", "/");
final Credentials cred = new UsernamePasswordCredentials("username","password");
final DigestScheme authscheme = new DigestScheme();
final HttpContext context = new BasicHttpContext();
authscheme.processChallenge(authChallenge);
final Header authResponse = authscheme.authenticate(cred, request, context);
final Map<String, String> table = parseAuthResponse(authResponse);
Assert.assertEquals("username", table.get("username"));
Assert.assertEquals("realm1", table.get("realm"));
Assert.assertEquals("/", table.get("uri"));
Assert.assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
Assert.assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response"));
}