基于JAVA表单的身份验证

时间:2019-05-23 11:06:40

标签: java authentication post

我正在尝试访问图像图标的URL。例如,https://sample.sample.com/d/cam/sss/organizations/sam/sample/SampleImages/sample.jpg会被重定向到SSO登录页面。成功进行身份验证后,用户将可以访问该页面并获得有效的响应代码。

给出的代码面临的问题是,每当我通过POST使用基于表单的身份验证时,都会得到以下响应,

       URL url;
try {
    url = new URL("https://sample.sample.com/d/cam/sss/organizations/sam/sample/SampleImages/sample.jpg");
    Map<String,Object> params = new LinkedHashMap<>();
    params.put("username", "sample");
    params.put("password", "sample");
    StringBuilder postData = new StringBuilder();
    for (Map.Entry<String,Object> param : params.entrySet()) {
        if (postData.length() != 0) postData.append('&');
        postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
        postData.append('=');
        postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
    }
    byte[] postDataBytes = postData.toString().getBytes("UTF-8");
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
    conn.setDoOutput(true);
    conn.getOutputStream().write(postDataBytes);
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

    for (int c; (c = in.read()) >= 0;)
        System.out.print((char)c);

<!DOCTYPE html>
<html>
<head>
  <title>Click Provider Link</title>
  <meta charset="UTF-8">
  <link rel="shortcut icon" href="/pa/favicon.ico"/>
  <script type="text/javascript">
    function storeAndNavigate() {

      // using var here for backward-compatibility
      var encodedNonce = 'dafdsfsdfdsf';
      var encodedPostParams = 'dfsdfgfsgfgfg';

      var encodedWebStorage = sessionStorage;
      encodedWebStorage.setItem(encodedNonce, encodedPostParams);

            window.location = 'SSO-Login page URL';
    }
  </script>
</head>
<body onLoad="storeAndNavigate()">
  <noscript>
    <p>
      <strong>Note: </strong>For POST Preservation to operate correctly you must have JavaScript enabled in your browser.
    </p>
    </noscript>
  </body>
</html>

0 个答案:

没有答案