首页 > 解决方案 > 重定向到angular6中的页面

问题描述

我有这种情况:-

我必须将支付网关集成到网站中。我们在前端使用 angular 6,在后端使用 springboot。支付网关要求以 formurl 编码发布数据,因此我以 formurl 编码发布数据。现在它给了我跨域访问控制错误。所以我现在所做的是我创建了一个 api,它采用 formurl 编码的字符串并在他们的 api 上发布。现在他们的响应代码是 302,它是重定向的。现在我从标题中获取 url 和 cookie。但是,如果我在前端发送此 url 和 cokkie,我将无法打开具有所需输出的页面,即无法在另一个域上以角度设置 cookie。

现在我正在获取 html,但我无法使用该 html 以角度动态创建页面。

如果有的话,请用角度或javascript告诉我它的解决方案。

我的代码如下: -

-> 服务.ts

makePayment(data: MakePaymentModel) {
        let body = new URLSearchParams();
        let headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
        for (let i in data) {
            if (data[i] != undefined) {
                body.append(i, data[i]);
            }
        }
        return this.http.post(this.resourceUrl2, body.toString(), { responseType: 'text', observe: 'response', withCredentials: false });
    }

它是在 api 上发布表单 urlencoded 字符串。

资源.java

@PostMapping(value = "/make-payment")
    public ResponseEntity<String> makePayment(@RequestBody String request) throws IOException {
        byte[] postData = request.getBytes(StandardCharsets.UTF_8);
        int postDataLength = postData.length;
        URL url = new URL(this.payementGatewayUrl+this.challanUrl);
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setInstanceFollowRedirects(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("charset", "utf-8");
        conn.setRequestProperty("Content-Length", Integer.toString(postDataLength ));
        conn.setUseCaches(false);
        try(DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
            wr.write(postData);
        }
        boolean redirect = false;

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


        if (redirect) {
            // get redirect url from "location" header field
            String newUrl = conn.getHeaderField("Location");
            // get the cookie if need, for login
            String cookies = conn.getHeaderField("Set-Cookie");
            // open the new connnection again
            conn = (HttpURLConnection) new URL(this.payementGatewayUrl+newUrl).openConnection();
            conn.setRequestProperty("Cookie", cookies);
            conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
            conn.addRequestProperty("Referer", "google.com");
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer html = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            html.append(inputLine);
        }
        in.close();
        System.out.println("URL Content... \n" + html.toString());
        return ResponseEntity.ok("OK");
    }

它是在他们的 api 上发布数据并取回重定向

标签: javahtmlangularredirecturl-redirection

解决方案


推荐阅读