001: /*
002: * $Header: $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030: package org.apache.commons.httpclient.contrib.auth;
031:
032: import java.util.ArrayList;
033:
034: import org.apache.commons.httpclient.Credentials;
035: import org.apache.commons.httpclient.HttpClient;
036: import org.apache.commons.httpclient.auth.AuthPolicy;
037: import org.apache.commons.httpclient.auth.AuthScope;
038: import org.apache.commons.httpclient.methods.GetMethod;
039: import org.apache.commons.httpclient.params.DefaultHttpParams;
040: import org.apache.commons.httpclient.params.HttpParams;
041:
042: /**
043: * A simple custom AuthScheme example. The included auth scheme is meant
044: * for demonstration purposes only. It does not actually implement a usable
045: * authentication method.
046: *
047: * <pre>
048: Login Configuration file bcsLogin.conf for JAAS.
049: -----------------------------------------------
050: com.sun.security.jgss.initiate {
051: com.sun.security.auth.module.Krb5LoginModule
052: required
053: client=TRUE
054: useTicketCache="true"
055: ticketCache="${user.krb5cc}"
056: debug=true;
057: };
058:
059: com.sun.security.jgss.accept {
060: com.sun.security.auth.module.Krb5LoginModule
061: required
062: client=TRUE
063: useTicketCache="true"
064: ticketCache="${user.krb5cc}"
065: debug=true;
066: };
067: -----------------------------------------------
068:
069: java -Djava.security.krb5.realm=REALM \
070: -Djava.security.krb5.kdc=kdc.domain \
071: -Djavax.security.auth.useSubjectCredsOnly=false \
072: -Djava.security.auth.login.config=src/conf/bcsLogin.conf \
073: -Duser.krb5cc="$KRB5CCNAME" \
074: -classpath $CP \
075: CustomAuthenticationNegotiateExample "http://localhost/gsstest/"
076: </pre>
077: */
078: public class CustomAuthenticationNegotiateExample {
079:
080: public static void main(String[] args) {
081:
082: // register the auth scheme
083: AuthPolicy.registerAuthScheme("Negotiate",
084: NegotiateScheme.class);
085:
086: // include the scheme in the AuthPolicy.AUTH_SCHEME_PRIORITY preference
087: ArrayList schemes = new ArrayList();
088: schemes.add("Negotiate");
089:
090: HttpParams params = DefaultHttpParams.getDefaultParams();
091: params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes);
092:
093: // now that our scheme has been registered we can execute methods against
094: // servers that require "Negotiate" authentication...
095: HttpClient client = new HttpClient();
096:
097: // The Negotiate scheme uses JAAS as credential provider but the
098: // httpclient api require us to supply cred anyway.
099: // a work around is to provide an empty set of creds.
100: Credentials use_jaas_creds = new Credentials() {
101: };
102: client.getState().setCredentials(new AuthScope(null, -1, null),
103: use_jaas_creds);
104: GetMethod httpget = new GetMethod(args[0]);
105:
106: try {
107: client.executeMethod(httpget);
108: //System.out.println(httpget.getStatusLine());
109: //System.out.println(httpget.getResponseBodyAsString());
110: } catch (Exception e) {
111: e.printStackTrace();
112: } finally {
113: // release any connection resources used by the method
114: httpget.releaseConnection();
115: }
116:
117: }
118: }
|