2014-01-06 23 views
3

我正在使用Maximo 7.5 REST API並且想要在Java中編寫REST客戶端以使用此RESTful服務。我在Maximo 7.5端啓用了Maximo安全性,以讓Maximo用戶訪問其自己的REST服務。以下是Maximo RESTful服務的web.xml。使用Java代碼使用Maximo RESTful服務

<security-constraint> 
     <web-resource-collection> 
      <web-resource-name>REST Servlet for Web App</web-resource-name> 
      <description>Object Structure Service Servlet (HTTP POST) accessible by authorized users</description> 
      <url-pattern>/rest/*</url-pattern> 
      <http-method>GET</http-method> 
      <http-method>POST</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <description>Roles that have access to Object Structure Service Servlet (HTTP POST)</description> 
      <role-name>maximouser</role-name> 
     </auth-constraint> 
     <user-data-constraint> 
      <description>data transmission gaurantee</description> 
      <transport-guarantee>NONE</transport-guarantee> 
     </user-data-constraint> 
    </security-constraint> 


    <login-config> 
     <auth-method>BASIC</auth-method> 
     <realm-name>REST Web Application Realm</realm-name>   
    </login-config> 

我成功能夠使用Chrome的Postman插件查詢Maximo REST服務。 2以下是我的郵差(REST客戶端)頭。 1. MAXAUTH - bWF4YWRtaW46bWF4YWRtaW4 = 2.接受 - 應用程序/ XML

雖然我已在報頭的授權(MAXAUTH),我用得到的彈出窗口中輸入用戶名和密碼查詢馬克西莫REST服務。一旦我給了憑據,我得到了迴應(如下所示) enter image description here

以下是我的Java代碼使用上述相同的RESTful服務。我不斷收到401錯誤,儘管我將憑據作爲財產,但並未授權。

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

public class RESTConsume { 

    // http://localhost:8080/RESTfulExample/json/product/get 
    public static void main(String[] args) { 

     try { 

      URL url = new URL("HOSTNAME/maxrest/rest/os/mxperson?personid=maxadmin"); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.setRequestProperty("Accept", "Application/xml"); 
      connection.setRequestProperty("MAXAUTH", "bWF4YWRtaW46bWF4YWRtaW4="); 
      System.out.println("Output from Server ....1 \n"); 

      /* 
      * if (conn.getResponseCode() != 200) { 
      * System.out.println("Output from Server ....2 \n"); 
      * 
      * throw new RuntimeException("Failed : HTTP error code : "+ 
      * conn.getResponseCode()); } 
      */ 
      System.out.println("Output from Server ....3 \n"); 

      BufferedReader br = new BufferedReader(new InputStreamReader(
        (connection.getInputStream()))); 
      System.out.println("Output from Server ....4 \n"); 

      String output; 
      System.out.println("Output from Server .... \n"); 
      while ((output = br.readLine()) != null) { 
       System.out.println(output); 
      } 

     } catch (MalformedURLException e) { 

      e.printStackTrace(); 

     } catch (IOException e) { 

      e.printStackTrace(); 

     } 

    } 

} 

下面是我的輸出:

Output from Server ....1 

Output from Server ....3 

java.io.IOException: Server returned HTTP response code: 401 for URL: http://vhost0043.dc1.co.us.compute.ihost.com/maxrest/rest/os/mxperson?personid=maxadmin 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
    at RESTConsume.main(RESTConsume.java:35) 

它正在爲不同的RESTful服務(不馬克西莫RESTful服務),並得到預期的迴應對此我們沒有任何安全性未啓用。請讓我知道是否需要額外使用Maximo RESTful服務。

回答

0

要麼服務期待用戶名和密碼或maxauth是無效的,因此在瀏覽器的情況下,它會提示你明確輸入,但在maximo的情況下,你會得到401.請嘗試提供用戶名和密碼頭也在請求。

+0

由於我啓用了Maximo安全性,我希望服務期望用戶名和密碼,但這是我們按照IBM文檔給予的屬性(MAXAUTH ==> bWF4YWRtaW46bWF4YWRtaW4 =)屬性。請參閱此鏈接http://pic.dhe.ibm.com/infocenter/tivihelp/v50r1/index.jsp?topic=%2Fcom.ibm.mif.doc%2Fgp_intfrmwk%2Frest_api%2Fc_rest_security.html – tasmohan

0

您需要添加使用基本的HTTP用戶名和密碼參數:

.../os/mxperson?_lid=username&_lpwd=password&_format=xml&personid=MAXADMIN 

我認爲使用LDAP時,你只能使用AUTH頭。

1

如果用戶:密碼是您的用戶名和密碼,則編碼爲64「user:password」(例如使用此工具:https://www.base64encode.org/)。然後,如果「imimastrangestring」是encode64的輸出,那麼請求屬性「MAXAUTH」:connection.setRequestProperty(「MAXAUTH」,「iamastrangestring」);