1 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 33 import org.apache.commons.httpclient.Credentials; 34 import org.apache.commons.httpclient.HttpMethod; 35 import org.apache.commons.httpclient.auth.AuthPolicy; 36 import org.apache.commons.httpclient.auth.AuthScheme; 37 import org.apache.commons.httpclient.auth.AuthenticationException; 38 import org.apache.commons.httpclient.auth.MalformedChallengeException; 39 import org.apache.commons.httpclient.params.DefaultHttpParams; 40 import org.apache.commons.httpclient.params.HttpParams; 41 42 47 public class CustomAuthenticationExample { 48 49 public static void main(String [] args) { 50 51 AuthPolicy.registerAuthScheme(SecretAuthScheme.NAME, SecretAuthScheme.class); 53 54 HttpParams params = DefaultHttpParams.getDefaultParams(); 58 ArrayList schemes = new ArrayList (); 59 schemes.add(SecretAuthScheme.NAME); 60 schemes.addAll((Collection ) params.getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY)); 61 params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes); 62 63 } 66 67 71 private class SecretAuthScheme implements AuthScheme { 72 73 public static final String NAME = "Secret"; 74 75 public SecretAuthScheme() { 76 } 78 public String authenticate(Credentials credentials, HttpMethod method) 79 throws AuthenticationException { 80 return "Open Sesame"; 81 } 82 public String authenticate(Credentials credentials, String method, 83 String uri) throws AuthenticationException { 84 return "Open Sesame"; 85 } 86 public String getID() { 87 return NAME; 88 } 89 public String getParameter(String name) { 90 return null; 92 } 93 public String getRealm() { 94 return null; 96 } 97 public String getSchemeName() { 98 return NAME; 99 } 100 public boolean isConnectionBased() { 101 return false; 102 } 103 public void processChallenge(String challenge) 104 throws MalformedChallengeException { 105 } 108 public boolean isComplete() { 109 return true; 111 } 112 } 113 } 114 | Popular Tags |