1

私のシナリオ: 私はクローズド ソースのエンタープライズ ポータルを使用しています。アプリはいくつかのレポートを提供します。テレビでレポートを表示する必要があるため、テレビには表示するための組み込みの Web ブラウザーがあります。 html. ここまで問題はありませんが、ポータルには匿名アクセスを防ぐためのログイン ページがあります。資格情報はありますが、TV にはレポートがランダムに表示されるため、アプリにログインしてサンプル レポートを読み込むメカニズムが必要です。テレビのブラウザに渡します。

私が行ったこと: レポートの URL を取得してからポータルにログインする Web ページを作成しました。その後、Cookie を読み取り、(その URL を介して) レポートへの接続を開き、そのコンテンツをロードして戻ってきます。ブラウザ(TV ブラウザ)にコンテンツを送信します。

マイコード

  @WebServlet("/LoadReport")
    public class LoadReport extends HttpServlet {
    private static final long serialVersionUID = 1L;    
    public LoadReport() {
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        HttpSession session = request.getSession(true);     
        session.setMaxInactiveInterval(5);
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();     

        if (request.getQueryString() != null && request.getQueryString() != "") {

            String tmp = request.getQueryString();
            tmp = tmp.substring(tmp.indexOf('=') + 1);
            sb.append(tmp);
            String postParams = "j_username=djboobo&j_password=djboobo&persistent=on";

            URL url = new URL("http://xyz/xyz/login/auth.jsp");
            HttpURLConnection connection = (HttpURLConnection) getConnection(postParams, url);
            // get sessionid from cookies 
            String cookies = getCookies(connection, postParams);            
            cookies += "logindetails=username:djboobo&persistent:yes";
            // url to report
            url = new URL(tmp);
            connection = (HttpURLConnection) getConnection(postParams, url);
            connection.setRequestProperty("Cookie", cookies);
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(postParams);          
            wr.flush();

            try {

                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = br.readLine()) != null) {                    
                    out.write(line);                    
                }               

            } catch (Exception e) {             
            }
            wr.close();
            connection.disconnect();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
    }   

    private HttpURLConnection getConnection(String postParams, URL url) throws IOException, ProtocolException {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Host", "http://xyz/xyz");
        connection.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.8,fa;q=0.6");
        connection.setRequestProperty("Cache-Control", "max-age=0");
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Content-Length", String.valueOf(postParams.getBytes().length));
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("charset", "utf-8");
        connection
                .setRequestProperty("User-Agent",
                        "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36");

        return connection;
    }

    private String getCookies(HttpURLConnection conn, String postParams) throws IOException {
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(postParams);
        wr.flush();
        wr.close();
        List<String> cookies = conn.getHeaderFields().get("Set-Cookie");
        StringBuilder sb = new StringBuilder();
        for (String cookie : cookies) {
            // System.out.println("Cookies: " + cookie);
            if (cookie.contains("JSESSIONID")) {
                sb.append(cookie.substring(0, cookie.indexOf(";")));
                sb.append("; ");
                break;
            }
        }
        return sb.toString();
    }
    }

主な問題は何ですか クロム開発者ツールを使用して、ポータルの仕組みと、それが要求および応答 Cookie であるとは何かを調べようとしましたが、SessionId (Cookie の 1 つ) をポータルに渡すと、レポートのコンテンツが返されることがわかりましたが、 chrome のツールからコピーした SessionId (つまり、chrome ブラウザからログインしたことを意味します) は、レポートのコンテンツをポータルから読み込みますが、プログラムからログインした SessionId を使用すると、レポートのコンテンツが読み込まれません。

ポータルは、SessionId を返すため、接続はプログラムから来ていると思いますが、ログイン後にセッションを強制終了するため、SessionId で報告する接続が認証されません。

どうしたの ???

4

0 に答える 0