2 Replies Latest reply: Jun 13, 2012 12:28 PM by 322268 RSS

    OpenSSO identity/authenticate returns 401

    322268
      Hello, I have an Eclipse RCP application that needs to access a REST API that secured behind OpenSSO. When I attempt to authenticate against OpenSSO I get a 401. Code included below.

      import java.io.BufferedReader;
      import java.io.DataOutputStream;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.net.URLConnection;
      import java.net.URLEncoder;

      import javax.net.ssl.HttpsURLConnection;


      public class TestIdentityService {
           
           /**
           * @param args
           */
           public static void main(String[] args) {
                String token = null;
                
                try {
                     URL iurl = new URL("https://localhost:9443/opensso/identity/authenticate");
                     URLConnection connection = iurl.openConnection();
                     connection.setDoInput(true);
                     connection.setDoOutput(true);
                     connection.setUseCaches(false);
                     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                     
                     ((HttpsURLConnection)connection).setRequestMethod("POST");
                     
                     DataOutputStream printout = new DataOutputStream(connection.getOutputStream());
                     String content = "username=" + URLEncoder.encode("user1", "UTF-8") + "&password=" + URLEncoder.encode("myPassword", "UTF-8");
                     printout.writeBytes(content);
                     printout.flush();
                     printout.close();
                     
                     BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()));
                     System.out.println("Successful Authentication using REST");
                     String line = null;
                     while((line = reader.readLine()) != null) {
                          System.out.println(line + "\n");
                          int index = line.indexOf("token");
                          if (index != -1) {
                               token = line.substring(9);
                          }
                     }
                } catch (Exception e) {
                     e.printStackTrace();
                }
                
                System.out.println("token:" + token);
           }

      }

      Results in:

      java.io.IOException: Server returned HTTP response code: 401 for URL: https:/localhost:9443/opensso/identity/authenticate
           at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
           at java.net.URLConnection.getContent(URLConnection.java:688)
           at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:406)
           at TestIdentityService.main(TestIdentityService.java:37)
      token:null

      Any help would be greatly appreciated.