【问题标题】:Curl command through Java works in windows and not in linux通过 Java 的 Curl 命令在 Windows 中有效,在 linux 中无效
【发布时间】:2020-03-29 05:34:50
【问题描述】:

我正在尝试使用下面的代码使用 Java 执行 curl 命令

String myUrl= "https://someIp:somePort";
String username = "someusername";
String password = "somepassword";

String command = "curl -k -d \"client_id=someId\" -d \"username="+username+"\" -d \"password="+password+"\"   -d \"grant_type=password\"   -d \"client_secret=\" \""+myUrl+"/myauth/openid-connect/token\"";

Process process = Runtime.getRuntime().exec(command);

ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = process.getInputStream().read(buffer)) != -1) {
        result.write(buffer, 0, length);
}
String response = result.toString(StandardCharsets.UTF_8.name());

这适用于 windows 机器,但不适用于 linux。在使用 exec 方法执行 curl 命令的方式上,linux 和 windows 有什么区别吗? 两次执行都是使用相同的 JRE 完成的。在 Windows 上,我成功获得了令牌,但在 Linux 中,我得到了以下响应: 响应 = {"error":"invalid_request","error_description":"缺少表单参数:grant_type"}

谢谢

【问题讨论】:

  • 在linux下怎么不行?
  • 你在 linux 中的错误信息是什么?
  • 您是否检查过您的 linux 机器中是否添加了 client_certificate。请执行 curl 请求,如果未正确执行,请添加 curl 的堆栈跟踪。我认为这可能是证书问题。 . 谢谢
  • 使用System.out.println打印command变量并直接在linux终端上执行打印的命令并检查它是否有效
  • 你好,如果我打印命令并在终端中手动尝试它可以工作。唯一的区别是我在我的 linux 机器中配置了一个代理,以便能够连接到端点。所以问题只是当这个 curl 命令从 exec 命令执行时。我怀疑java代码忽略了代理设置......这可能是问题吗?

标签: java linux curl keycloak


【解决方案1】:

经过调查,似乎有人在 linux 环境中执行此 java 代码时,curl 命令构造不正确。 我使用了以下代码,一切正常:

String cUrlToKeyCloak = "curl -k -d \"client_id=someId\" -d \"username="+username+"\" -d \"password="+password+"\" -d \"grant_type=password\" "+keyCloakUrl+"/auth/realms/master/protocol/openid-connect/token";

    ProcessBuilder processBuilder = new ProcessBuilder();
    if(!System.getProperty("os.name").contains("Windows"))
        processBuilder.command("bash", "-c", cUrlToKeyCloak );
    else
        processBuilder.command("cmd.exe", "/c", cUrlToKeyCloak );

    String cKeyResponse = "";

    try {

        Process process = processBuilder.start();
        StringBuilder output = new StringBuilder();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }

        int exitVal = process.waitFor();
        if (exitVal == 0) {
            LOGGER.info("Curl command to keyCloak requested ...");
            LOGGER.info("cKey response = "+output);
            cKeyResponse = output.toString();
        } else {
            LOGGER.error("Curl command to keyCloak executed with error ...");
            LOGGER.info("cKey response = "+output);
            return false;
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

【讨论】:

    猜你喜欢
    • 2015-10-09
    • 1970-01-01
    • 2017-03-26
    • 2016-08-10
    • 1970-01-01
    • 2017-04-05
    • 2013-08-21
    • 2020-03-24
    相关资源
    最近更新 更多