java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3911
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use custom auth scheme E-mail
User Rating: / 0
PoorBest 

A simple custom AuthScheme example. The included auth scheme is meant for demonstration purposes only. It does not actually implement a usable authentication method.

/*
 * $Header: 
 * $Revision$
 * $Date$
 *
 * ====================================================================
 *
 *  Copyright 2002-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.AuthenticationException;
import org.apache.commons.httpclient.auth.MalformedChallengeException;
import org.apache.commons.httpclient.params.DefaultHttpParams;
import org.apache.commons.httpclient.params.HttpParams;

/**
 * A simple custom AuthScheme example.  The included auth scheme is meant
 * for demonstration purposes only.  It does not actually implement a usable
 * authentication method.
 */
public class CustomAuthenticationExample {
    
    public static void main(String[] args) {
        
        // register the auth scheme
        AuthPolicy.registerAuthScheme(SecretAuthScheme.NAME, 
                SecretAuthScheme.class);
        
        // include the scheme in the AuthPolicy.AUTH_SCHEME_PRIORITY 
        // preference, this can be done on a per-client or per-method basis 
        // but we'll do it globally for this example
        HttpParams params = DefaultHttpParams.getDefaultParams();
        ArrayList schemes = new ArrayList();
        schemes.add(SecretAuthScheme.NAME);
        schemes.addAll((Collectionparams.getParameter(
                AuthPolicy.AUTH_SCHEME_PRIORITY));
        
        params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes);
        
        // now that our scheme has been registered we can execute 
        // methods against servers that require "Secret" authentication...
        
    }
    
    /**
     * A custom auth scheme that just uses "Open Sesame" as the 
     * authentication string.
     */
    private class SecretAuthScheme implements AuthScheme {
        
        public static final String NAME = "Secret";
        
        public SecretAuthScheme() {
            // All auth schemes must have a no arg constructor.
        }
        
        public String authenticate(Credentials credentials, 
                HttpMethod methodthrows AuthenticationException {
            
            return "Open Sesame";
            
        }
        public String authenticate(Credentials credentials, String method,
                String urithrows AuthenticationException {
            
            return "Open Sesame";
            
        }
        
        public String getID() {
            
            return NAME;
            
        }
        
        public String getParameter(String name) {
            
            // this scheme does not use parameters, 
            // see RFC2617Scheme for an example
            return null;
            
        }
        
        public String getRealm() {
            
            // this scheme does not use realms
            return null;
            
        }
        public String getSchemeName() {
            
            return NAME;
            
        }
        public boolean isConnectionBased() {
            
            return false;
            
        }
        
        public void processChallenge(String challenge)        
            throws MalformedChallengeException {
            
            // Nothing to do here, this is not a challenge based
            // auth scheme.  See NTLMScheme for a good example.
            
        }
        
        public boolean isComplete() {
            
            // again we're not a challenge based scheme so 
            // this is always true
            return true;
            
        }
    }
}

Source: Apache HttpClient


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.