java - 重定向到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 上发布数据并取回重定向
解决方案
推荐阅读
- c++ - 使用 LLVM IR Builder CreateAlloca 创建分配时出错
- c++ - 如何通过mingw-w64在windows中运行vulkan示例代码?
- go - 为什么我从应用程序收到 dns 错误,而不是从我的浏览器
- python - 如何在 tkinter GUI 中创建交互式动画
- email - 防止将邮件发送到 gmail 中的“促销”
- python - 需要了解 SubwordTextEncoder 分词器背后的概念
- google-cloud-platform - GCP 云构建查看原始日志链接
- r - 如何获取在指定环境中评估调用对象时使用的所有参数的值
- python - 创建将整个图像数据集转换为注释文件(.xml 文件)的自动方法
- css - 使用 nextjs 时 Css 未在生产模式下加载