简体   繁体   English

使用Java代码使用Maximo RESTful服务

[英]Consume Maximo RESTful Service using Java code

I am using Maximo 7.5 REST API and want to write REST client in java to consume this RESTful service. 我正在使用Maximo 7.5 REST API,并希望在java中编写REST客户端以使用此RESTful服务。 I have enabled Maximo security in Maximo 7.5 side to keep the Maximo users to access its own REST services. 我在Maximo 7.5端启用了Maximo安全性,以保持Maximo用户访问自己的REST服务。 Below is my web.xml for Maximo RESTful service looks like. 下面是我的Maximo RESTful服务的web.xml。

<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>

I am successfully able to query the Maximo REST service using the Chrome's Postman plugin. 我成功地使用Chrome的Postman插件查询Maximo REST服务。 Below 2 are my Postman's (REST client) Headers. 2以下是我的邮递员(REST客户端)标题。 1. MAXAUTH - bWF4YWRtaW46bWF4YWRtaW4= 2. Accept - Application/xml 1. MAXAUTH - bWF4YWRtaW46bWF4YWRtaW4 = 2.接受 - 应用程序/ xml

Though I have given the authorization (MAXAUTH) in headers, I used to get the pop-up window to enter username and password to query Maximo REST service. 虽然我已在头文件中给出了授权(MAXAUTH),但我常常会弹出窗口输入用户名和密码来查询Maximo REST服务。 Once I give the credentials, I get the response (shown below) 一旦我提供凭据,我得到响应(如下所示) 在此输入图像描述

The below is my Java code to consume above same RESTful service. 以下是我使用相同RESTful服务的Java代码。 I am continuously getting 401 error and though I am giving credentials as property, it is not authorizing it. 我不断收到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();

        }

    }

}

Below is my output: 以下是我的输出:

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)

It is working for different RESTful service (not Maximo RESTful service)and getting the response as expected for which we don't have any security was not enabled. 它适用于不同的RESTful服务(不是Maximo RESTful服务),并且没有启用我们没有任何安全性的预期响应。 Please let me know if i need to do something extra to consume Maximo RESTful service. 如果我需要做一些额外的事情来消费Maximo RESTful服务,请告诉我。

If user:password is your username and password then encode64 "user:password" (such as with this tool: https://www.base64encode.org/ ). 如果user:password是您的用户名和密码,则编码64“user:password”(例如使用此工具: https ://www.base64encode.org/)。 Then if "imimastrangestring" is the output of encode64 set that as the request property "MAXAUTH": connection.setRequestProperty("MAXAUTH", "iamastrangestring"); 然后如果“imimastrangestring”是encode64的输出设置为请求属性“MAXAUTH”:connection.setRequestProperty(“MAXAUTH”,“iamastrangestring”);

服务期望用户名和密码或maxauth无效,因此在浏览器的情况下,它会提示您明确输入,但如果是maximo,则获得401.请尝试在请求中提供用户名和密码标头。

You need to add the username and password parameters using basic http: 您需要使用基本http添加用户名和密码参数:

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

I think you can only use AUTH headers when using LDAP. 我认为您在使用LDAP时只能使用AUTH标头。

Dredging up an old question here, but I was directed to this answer from Google when "I" had the same problem, so maybe this will help someone else. 在这里挖掘了一个老问题,但当“我”遇到同样的问题时,我被谷歌的答案引导到了这个答案,所以也许这会帮助其他人。

I was looking at this IBM article: https://www.ibm.com/support/knowledgecenter/en/SSLKT6_7.6.0/com.ibm.mif.doc/gp_intfrmwk/rest_api/c_rest_security.html In it, it lists two different authentication options. 我正在看这篇IBM文章: https//www.ibm.com/support/knowledgecenter/en/SSLKT6_7.6.0/com.ibm.mif.doc/gp_intfrmwk/rest_api/c_rest_security.html其中列出了两个不同的文章验证选项。 It seems rather obvious to me now, but the block of XML the first section of the article suggests to change (and that you show as changed in your post) opens up HTTP Basic Authentication . 现在对我来说似乎相当明显,但本文第一部分建议更改(并且在帖子中显示为已更改)的XML块会打开HTTP基本身份验证 Without those settings you need to use native authentication. 没有这些设置,您需要使用本机身份验证。 This took me bit to realize, and also appears to be the source of your confusion. 这让我有点意识到,也似乎是你困惑的根源。

That means, with the standard Maximo setup, it is configured to use native authentication. 这意味着,使用标准Maximo设置,它被配置为使用本机身份验证。 You need to supply the MAXAUTH header property with a value of " <username>:<password> " base64 encoded. 您需要提供MAXAUTH标头属性,其值为“ <username>:<password> ”base64 encoded。 Maximo will decode it and get the username and password and compare them to its internal database. Maximo将对其进行解码并获取用户名和密码,并将其与内部数据库进行比较。 If they match, the request is considered authenticated and the service will return its results. 如果它们匹配,则认为该请求已通过身份验证,并且该服务将返回其结果。 If they don't match, you are considered unauthenticated and you get the 401 error. 如果它们不匹配,您将被视为未经身份验证,并且您收到401错误。 This means Maximo is doing the authentication against its database. 这意味着Maximo正在对其数据库进行身份验证。

If you change the settings described in the linked article, and as you have done and shown in your question, then you are saying you don't want to use that form authentication (I assume it actually turns off that form of authentication). 如果您更改了链接文章中描述的设置,并且正如您已经完成并在问题中显示的那样,那么您说您不想使用该表单身份验证(我认为它实际上关闭了这种身份验证形式)。 You are then saying that you want the application server to do the authentication against the app server's "database" instead of Maximo's. 然后,您说您希望应用程序服务器针对应用服务器的 “数据库” 而不是 Maximo进行身份验证。 This is what you want if you want to be validated against LDAP (Active Directory) or something similar instead. 如果您想要针对LDAP(Active Directory)或类似的东西进行验证,这就是您想要的。

In this case, the username and password need to be sent using the HTTP Basic Authentication standard instead. 在这种情况下,需要使用HTTP基本身份验证标准发送用户名和密码。 See: https://en.wikipedia.org/wiki/Basic_access_authentication That means you need to supply a header called "Authorization" instead of "MAXAUTH" and the value of that header must be the string "Basic " (note the capitalization and the space) plus the base64 encoded string of " <username>:<password> ". 请参阅: https ://en.wikipedia.org/wiki/Basic_access_authentication这意味着您需要提供名为“Authorization”而不是“MAXAUTH”的标头,并且该标头的值必须是字符串“Basic”(请注意大小写和空格)加上base64编码的字符串“ <username>:<password> ”。 For example, for the user "maxadmin" with the password "maxadmin", the "Authorization" header's value would be "Basic bWF4YWRtaW46bWF4YWRtaW4=". 例如,对于密码为“maxadmin”的用户“maxadmin”,“授权”标题的值为“Basic bWF4YWRtaW46bWF4YWRtaW4 =”。 The application server gets that and decodes the username and password. 应用程序服务器获取该信息并解码用户名和密码。 It then checks those against what it is configured to authorize users against (a database of its own, LDAP, something else, some combination of that). 然后,它会检查那些配置为授权用户的内容(自己的数据库,LDAP,其他内容,某些组合)。 If they don't match (which they won't if you aren't even supplying this information), then the request is considered unauthenticated and you get the 401 response back. 如果它们不匹配(如果您甚至不提供此信息,则不会匹配),则该请求将被视为未经身份验证,您将收到401响应。 If they do match, the request/user is considered authenticated and then the app server will map this request/user to a role (or many roles), depending on your app server settings for the deployed application (that goes into a different LDAP configuration discussion I don't want to tangent into here; this is already too long as-is). 如果它们匹配,则认为请求/用户已经过身份验证,然后应用服务器会将此请求/用户映射到角色(或多个角色),具体取决于已部署应用程序的应用服务器设置(进入不同的LDAP配置)讨论我不想在这里切入;这已经太久了。) That role(s) is then passed to Maximo for the request. 然后将该角色传递给Maximo以获取请求。 Maximo takes that role(s) and checks to see if it matches what is configured in its XML configuration (that you quoted in your question and that is shown in the linked IBM article earlier). Maximo接受该角色并检查它是否与其XML配置中配置的内容匹配(您在问题中引用的内容以及之前链接的IBM文章中显示的内容)。 If they don't match, I think you are considered unauthorized and an error is sent back. 如果它们不匹配,我认为您被认为是未经授权的,并且会发回错误。 If they do match, the request is considered authorized, is allowed through and the service returns its response. 如果它们匹配,则认为该请求被授权,允许通过并且服务返回其响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM