KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > contrib > auth > CustomAuthenticationNegotiateExample


1 /*
2  * $Header: $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  *
6  * ====================================================================
7  *
8  * Licensed to the Apache Software Foundation (ASF) under one or more
9  * contributor license agreements. See the NOTICE file distributed with
10  * this work for additional information regarding copyright ownership.
11  * The ASF licenses this file to You under the Apache License, Version 2.0
12  * (the "License"); you may not use this file except in compliance with
13  * the License. You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ====================================================================
23  *
24  * This software consists of voluntary contributions made by many
25  * individuals on behalf of the Apache Software Foundation. For more
26  * information on the Apache Software Foundation, please see
27  * <http://www.apache.org/>.
28  *
29  */

30 package org.apache.commons.httpclient.contrib.auth;
31
32 import java.util.ArrayList JavaDoc;
33
34 import org.apache.commons.httpclient.Credentials;
35 import org.apache.commons.httpclient.HttpClient;
36 import org.apache.commons.httpclient.auth.AuthPolicy;
37 import org.apache.commons.httpclient.auth.AuthScope;
38 import org.apache.commons.httpclient.methods.GetMethod;
39 import org.apache.commons.httpclient.params.DefaultHttpParams;
40 import org.apache.commons.httpclient.params.HttpParams;
41
42 /**
43  * A simple custom AuthScheme example. The included auth scheme is meant
44  * for demonstration purposes only. It does not actually implement a usable
45  * authentication method.
46  *
47  * <pre>
48  Login Configuration file bcsLogin.conf for JAAS.
49  -----------------------------------------------
50    com.sun.security.jgss.initiate {
51      com.sun.security.auth.module.Krb5LoginModule
52          required
53          client=TRUE
54          useTicketCache="true"
55          ticketCache="${user.krb5cc}"
56          debug=true;
57    };
58
59    com.sun.security.jgss.accept {
60      com.sun.security.auth.module.Krb5LoginModule
61          required
62          client=TRUE
63          useTicketCache="true"
64          ticketCache="${user.krb5cc}"
65          debug=true;
66    };
67  -----------------------------------------------
68  
69    java -Djava.security.krb5.realm=REALM \
70       -Djava.security.krb5.kdc=kdc.domain \
71       -Djavax.security.auth.useSubjectCredsOnly=false \
72       -Djava.security.auth.login.config=src/conf/bcsLogin.conf \
73       -Duser.krb5cc="$KRB5CCNAME" \
74       -classpath $CP \
75       CustomAuthenticationNegotiateExample "http://localhost/gsstest/"
76    </pre>
77  */

78 public class CustomAuthenticationNegotiateExample {
79
80     public static void main(String JavaDoc[] args) {
81         
82         // register the auth scheme
83
AuthPolicy.registerAuthScheme("Negotiate", NegotiateScheme.class);
84
85         // include the scheme in the AuthPolicy.AUTH_SCHEME_PRIORITY preference
86
ArrayList JavaDoc schemes = new ArrayList JavaDoc();
87         schemes.add("Negotiate");
88
89         HttpParams params = DefaultHttpParams.getDefaultParams();
90         params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes);
91         
92         // now that our scheme has been registered we can execute methods against
93
// servers that require "Negotiate" authentication...
94
HttpClient client = new HttpClient();
95         
96         // The Negotiate scheme uses JAAS as credential provider but the
97
// httpclient api require us to supply cred anyway.
98
// a work around is to provide an empty set of creds.
99
Credentials use_jaas_creds = new Credentials() {};
100         client.getState().setCredentials(
101             new AuthScope(null, -1, null),
102             use_jaas_creds);
103         GetMethod httpget = new GetMethod(args[0]);
104
105         try {
106             client.executeMethod(httpget);
107             //System.out.println(httpget.getStatusLine());
108
//System.out.println(httpget.getResponseBodyAsString());
109
} catch (Exception JavaDoc e) {
110             e.printStackTrace();
111         } finally {
112             // release any connection resources used by the method
113
httpget.releaseConnection();
114         }
115         
116     }
117 }
118
Popular Tags