发布到Siteminder login.fcc以使用java

时间:2018-04-20 06:57:22

标签: java authentication login siteminder

我的应用程序需要访问一些受SiteMinder保护的URL,因此需要Siteminder代理提供的SMSession cookie。

我想通过将我的登录凭据发布到Siteminder登录表单来获取SMSession cookie。

我尝试了许多不同的方法来访问和发布所需的数据(目标,smauthreason,smagentname,用户名,密码)到login.fcc。 我可以在Postman / Fiddler中这样发布它并检索一个cookie。

try {
        URL requestURL = new URL("HTTPS_URL_TO_THE/login-captcha.fcc");
        HttpsURLConnection conn = (HttpsURLConnection) requestURL.openConnection();
        conn.setDoOutput(true);
        //conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Host", "HOST_FROM_ABOVE");
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Content-Length","" + createBody().getBytes().length);
        conn.setRequestProperty("Cache-Control", "max-age=0");
        conn.setRequestProperty("Origin", "ORIGIN_HOST_FROM_WHERE_POSTED");
        conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
        conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        conn.setRequestProperty("DNT", "1");
        conn.setRequestProperty("Referer", "REFERER_TAKEN_FROM_FIDDLER");
        conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
        conn.setRequestProperty("Accept-Language", "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7");


        OutputStream os = conn.getOutputStream();
        os.write(createBody().getBytes());

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));               
        String in;
        while ((in = br.readLine()) != null){
            System.out.println(in);
        }

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

我将获得连接,并且可以在我之前提供cookie时访问该页面。但是在这一步中我想要检索cookie。

身体数据100%正确。 以上示例中使用的标头并非全部都需要。这是我最后一次绝望的尝试发布与fiddler中显示的相同。但我没有获得登录或302 HTTP重定向。

为什么它不起作用?有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我为我的问题找到了解决方案。实际上我将不得不手动处理重定向。如果我自动遵循重定向,则检索到的cookie将不会与下一个请求一起发送。 在没有cookie的情况下发布到targetURL会自动重定向到登录页面。

URL protectedURL = new URL(protectedURLString);
        HttpsURLConnection conn = (HttpsURLConnection) protectedURL.openConnection();
        conn.setInstanceFollowRedirects(false);

        boolean redirect = false;
        System.out.println("Request URL: " + protectedURL);

        //Check for redirection
        int status = conn.getResponseCode();
        if(status != HttpURLConnection.HTTP_OK) {
            if(status == HttpURLConnection.HTTP_MOVED_TEMP 
                    || status == HttpURLConnection.HTTP_MOVED_PERM
                    || status == HttpURLConnection.HTTP_SEE_OTHER) {
                redirect = true;
            }
        }
        System.out.println("Response Code: " + status);

        //If redirected
        if(redirect) {
            //get new redirect URL from Location header field
            String newURL = conn.getHeaderField("Location");
            URL tempURL = new URL(newURL);
            String query = tempURL.getQuery();
            //Read parameters from query
            String [] params = query.split("&");
            for (String param : params) {
                String [] parts = param.split("=");
                String key = parts[0].toLowerCase();
                String value = "";
                if(parts.length > 1)
                    value = parts[1];
                updateVars(key, value);
            }

        }
        postToLogin(this.URL_LOGIN);

这是我的解决方案中使用的一些代码。 updateVars(key, value)更新类变量。 postToLogin(this.URL_LOGIN)发布到包含使用类变量生成的正文的登录URL。