1 31 32 import java.io.BufferedReader ; 33 import java.io.IOException ; 34 import java.io.InputStreamReader ; 35 36 import org.apache.commons.httpclient.Credentials; 37 import org.apache.commons.httpclient.HttpClient; 38 import org.apache.commons.httpclient.NTCredentials; 39 import org.apache.commons.httpclient.UsernamePasswordCredentials; 40 import org.apache.commons.httpclient.auth.AuthScheme; 41 import org.apache.commons.httpclient.auth.CredentialsProvider; 42 import org.apache.commons.httpclient.auth.CredentialsNotAvailableException; 43 import org.apache.commons.httpclient.auth.NTLMScheme; 44 import org.apache.commons.httpclient.auth.RFC2617Scheme; 45 import org.apache.commons.httpclient.methods.GetMethod; 46 47 53 public class InteractiveAuthenticationExample { 54 55 58 public InteractiveAuthenticationExample() { 59 super(); 60 } 61 62 public static void main(String [] args) throws Exception { 63 64 InteractiveAuthenticationExample demo = new InteractiveAuthenticationExample(); 65 demo.doDemo(); 66 } 67 68 private void doDemo() throws IOException { 69 70 HttpClient client = new HttpClient(); 71 client.getParams().setParameter( 72 CredentialsProvider.PROVIDER, new ConsoleAuthPrompter()); 73 GetMethod httpget = new GetMethod("http://target-host/requires-auth.html"); 74 httpget.setDoAuthentication(true); 75 try { 76 int status = client.executeMethod(httpget); 78 System.out.println(httpget.getStatusLine().toString()); 80 System.out.println(httpget.getResponseBodyAsString()); 81 } finally { 82 httpget.releaseConnection(); 84 } 85 } 86 87 public class ConsoleAuthPrompter implements CredentialsProvider { 88 89 private BufferedReader in = null; 90 public ConsoleAuthPrompter() { 91 super(); 92 this.in = new BufferedReader (new InputStreamReader (System.in)); 93 } 94 95 private String readConsole() throws IOException { 96 return this.in.readLine(); 97 } 98 99 public Credentials getCredentials( 100 final AuthScheme authscheme, 101 final String host, 102 int port, 103 boolean proxy) 104 throws CredentialsNotAvailableException 105 { 106 if (authscheme == null) { 107 return null; 108 } 109 try{ 110 if (authscheme instanceof NTLMScheme) { 111 System.out.println(host + ":" + port + " requires Windows authentication"); 112 System.out.print("Enter domain: "); 113 String domain = readConsole(); 114 System.out.print("Enter username: "); 115 String user = readConsole(); 116 System.out.print("Enter password: "); 117 String password = readConsole(); 118 return new NTCredentials(user, password, host, domain); 119 } else 120 if (authscheme instanceof RFC2617Scheme) { 121 System.out.println(host + ":" + port + " requires authentication with the realm '" 122 + authscheme.getRealm() + "'"); 123 System.out.print("Enter username: "); 124 String user = readConsole(); 125 System.out.print("Enter password: "); 126 String password = readConsole(); 127 return new UsernamePasswordCredentials(user, password); 128 } else { 129 throw new CredentialsNotAvailableException("Unsupported authentication scheme: " + 130 authscheme.getSchemeName()); 131 } 132 } catch (IOException e) { 133 throw new CredentialsNotAvailableException(e.getMessage(), e); 134 } 135 } 136 } 137 } 138 | Popular Tags |