KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AlternateAuthenticationExample


1 /*
2  * $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 $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  * ====================================================================
6  *
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * 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 JavaDoc;
33 import java.util.List JavaDoc;
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 JavaDoc[] args) throws Exception JavaDoc {
63         HttpClient client = new HttpClient();
64         client.getState().setCredentials(
65             new AuthScope("myhost", 80, "myrealm"),
66             new UsernamePasswordCredentials("username", "password"));
67         // Suppose the site supports several authetication schemes: NTLM and Basic
68
// Basic authetication is considered inherently insecure. Hence, NTLM authentication
69
// is used per default
70

71         // This is to make HttpClient pick the Basic authentication scheme over NTLM & Digest
72
List JavaDoc authPrefs = new ArrayList JavaDoc(3);
73         authPrefs.add(AuthPolicy.BASIC);
74         authPrefs.add(AuthPolicy.NTLM);
75         authPrefs.add(AuthPolicy.DIGEST);
76         client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
77
78         GetMethod httpget = new GetMethod("http://myhost/protected/auth-required.html");
79
80         try {
81             int status = client.executeMethod(httpget);
82             // print the status and response
83
System.out.println(httpget.getStatusLine());
84             System.out.println(httpget.getResponseBodyAsString());
85         } finally {
86             // release any connection resources used by the method
87
httpget.releaseConnection();
88         }
89     }
90 }
91
Popular Tags