01: /*
02: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/AlternateAuthenticationExample.java,v 1.3 2004/06/12 22:47:23 olegk Exp $
03: * $Revision: 480424 $
04: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
05: * ====================================================================
06: *
07: * Licensed to the Apache Software Foundation (ASF) under one or more
08: * contributor license agreements. See the NOTICE file distributed with
09: * this work for additional information regarding copyright ownership.
10: * The ASF licenses this file to You under the Apache License, Version 2.0
11: * (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software
17: * distributed under the License is distributed on an "AS IS" BASIS,
18: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19: * See the License for the specific language governing permissions and
20: * limitations under the License.
21: * ====================================================================
22: *
23: * This software consists of voluntary contributions made by many
24: * individuals on behalf of the Apache Software Foundation. For more
25: * information on the Apache Software Foundation, please see
26: * <http://www.apache.org/>.
27: *
28: * [Additional notices, if required by prior licensing conditions]
29: *
30: */
31:
32: import java.util.ArrayList;
33: import java.util.List;
34:
35: import org.apache.commons.httpclient.HttpClient;
36: import org.apache.commons.httpclient.UsernamePasswordCredentials;
37: import org.apache.commons.httpclient.auth.AuthPolicy;
38: import org.apache.commons.httpclient.auth.AuthScope;
39: import org.apache.commons.httpclient.methods.GetMethod;
40:
41: /**
42: * <p>A simple example that uses alternate authentication scheme selection
43: * if several authentication challenges are returned.
44: * </p>
45: *
46: * <p>Per default HttpClient picks the authentication challenge in the following
47: * order of preference: NTLM, Digest, Basic. In certain cases it may be desirable to
48: * force the use of a weaker authentication scheme.
49: * </p>
50: *
51: * @author Oleg Kalnichevski
52: */
53: public class AlternateAuthenticationExample {
54:
55: /**
56: * Constructor for BasicAuthenticatonExample.
57: */
58: public AlternateAuthenticationExample() {
59: super ();
60: }
61:
62: public static void main(String[] args) throws Exception {
63: HttpClient client = new HttpClient();
64: client.getState()
65: .setCredentials(
66: new AuthScope("myhost", 80, "myrealm"),
67: new UsernamePasswordCredentials("username",
68: "password"));
69: // Suppose the site supports several authetication schemes: NTLM and Basic
70: // Basic authetication is considered inherently insecure. Hence, NTLM authentication
71: // is used per default
72:
73: // This is to make HttpClient pick the Basic authentication scheme over NTLM & Digest
74: List authPrefs = new ArrayList(3);
75: authPrefs.add(AuthPolicy.BASIC);
76: authPrefs.add(AuthPolicy.NTLM);
77: authPrefs.add(AuthPolicy.DIGEST);
78: client.getParams().setParameter(
79: AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
80:
81: GetMethod httpget = new GetMethod(
82: "http://myhost/protected/auth-required.html");
83:
84: try {
85: int status = client.executeMethod(httpget);
86: // print the status and response
87: System.out.println(httpget.getStatusLine());
88: System.out.println(httpget.getResponseBodyAsString());
89: } finally {
90: // release any connection resources used by the method
91: httpget.releaseConnection();
92: }
93: }
94: }
|