2 Replies Latest reply: Jan 26, 2010 12:34 AM by 843810 RSS

    Bypass sharepoint portal browser login and open Welcome page

    843810
      We have share point portal site (which is test) which takes logon credentials when i access this site from the browser. I am trying to acces the same site from my java application using http client and passing all the credentials. But it always fails with "401" error. Can you please suggest me how to reach to Welcome page by passing domain, user name, password. I am also pasting the code which I tried to solve the issue.
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      
      import org.apache.commons.httpclient.Credentials;
      import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
      import org.apache.commons.httpclient.HttpClient;
      import org.apache.commons.httpclient.NTCredentials;
      import org.apache.commons.httpclient.UsernamePasswordCredentials;
      import org.apache.commons.httpclient.auth.AuthScheme;
      import org.apache.commons.httpclient.auth.AuthScope;
      import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
      import org.apache.commons.httpclient.auth.CredentialsProvider;
      import org.apache.commons.httpclient.auth.NTLMScheme;
      import org.apache.commons.httpclient.auth.RFC2617Scheme;
      import org.apache.commons.httpclient.methods.GetMethod;
      import org.apache.commons.httpclient.params.HttpMethodParams;
      
      
      public class InteractiveAuthenticationExample {
      
           public InteractiveAuthenticationExample() {
                super();
           }
      
           public static void main(String[] args) throws Exception {
      
                InteractiveAuthenticationExample demo = 
                     new InteractiveAuthenticationExample();
      
                demo.doDemo();
           }
      
           private void doDemo() throws IOException {
      
      
                HttpClient client = new HttpClient();
                
                client.getState().setCredentials(new AuthScope("www.test.com", 80,
                          AuthScope.ANY_HOST), new NTCredentials("test@test.com", "test1", "www.test.com", "test.com"));
      
                GetMethod httpget = new GetMethod("http://www.test.com/Page/Welcome.aspx");
      
                httpget.setDoAuthentication(true);
      
      
      
                try {
      
                     httpget.setFollowRedirects(true);
      
                     httpget.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));  
                     // execute the GET
                     int status = client.executeMethod(httpget);
      
      
      
                     System.out.println("status::::" + status);
                     // print the status and response
                     System.out.println(httpget.getStatusLine().toString());
                     System.out.println(httpget.getResponseBodyAsString());
      
                } finally {
      
                     // release any connection resources used by the method
                     httpget.releaseConnection();
                }
           }
      
           public class ConsoleAuthPrompter implements CredentialsProvider {
      
                private BufferedReader in = null;
                public ConsoleAuthPrompter() {
                     super();
                     this.in = new BufferedReader(
                               new InputStreamReader(System.in));
                }
      
                private String readConsole() throws IOException {
                     return this.in.readLine();
                }
      
                public Credentials getCredentials(
                          final AuthScheme authscheme,
                          final String host,
                          int port,
                          boolean proxy)
                throws CredentialsNotAvailableException {
      
                     if (authscheme == null) {
                          return null;
                     }
      
                     try{
      
                          if (authscheme instanceof NTLMScheme) {
      
                               System.out.println(host + ":" + port + 
                                         " requires Windows authentication");
      
                               System.out.print("Enter domain: ");
                               String domain = readConsole();
                               System.out.print("Enter username: ");
                               String user = readConsole();
                               System.out.print("Enter password: ");
                               String password = readConsole();
      
                               return new NTCredentials(user, password, host, domain);
      
                          } else
      
                               if (authscheme instanceof RFC2617Scheme) {
      
                                    System.out.println(host + ":" + port + 
                                              " requires authentication with the realm '"
                                              + authscheme.getRealm() + "'");
      
                                    System.out.print("Enter username: ");
                                    String user = readConsole();
                                    System.out.print("Enter password: ");
                                    String password = readConsole();
      
                                    return new UsernamePasswordCredentials(
                                              user, password);
      
                               } else {
      
                                    throw new CredentialsNotAvailableException(
                                              "Unsupported authentication scheme: " +
                                              authscheme.getSchemeName());
                               }
      
                     } catch (IOException e) {
                          throw new CredentialsNotAvailableException(
                                    e.getMessage(), e);
                     }
                }
           }
      }  
        
        • 1. Re: Bypass sharepoint portal browser login and open Welcome page
          843810
          SharePoint ist protected through Kerberos/SPNEGO. You can google for a sun example how to user the regular httpconnection for. It will work transparently. This is what I do at work.
          • 2. Re: Bypass sharepoint portal browser login and open Welcome page
            843810
            Here are a couple of links you might find helpful:

            http://java.sun.com/javase/6/docs/technotes/guides/net/http-auth.html
            and
            http://spnego.sourceforge.net/api/net/sourceforge/spnego/SpnegoHttpURLConnection.html

            The sourceforge project is meant to be used on the server but it does have a stand alone client library.

            From the java docs, your code would end up looking something like this...
            public static void main(final String[] args) throws Exception {
                System.setProperty("java.security.krb5.conf", "krb5.conf");
                System.setProperty("sun.security.krb5.debug", "true");
                System.setProperty("java.security.auth.login.config", "login.conf");
                
                SpnegoHttpURLConnection spnego = null;
                
                try {
                    spnego = new SpnegoHttpURLConnection("spnego-client", "dfelix", "myp@s5");
                    spnego.connect(new URL("http://medusa:8080/index.jsp"));
                    
                    System.out.println(spnego.getResponseCode());
                
                } finally {
                    if (null != spnego) {
                        spnego.disconnect();
                    }
                }
            }