我创建了一个 Spring Boot 应用程序,我在其中从外部 API 获取数据,将它们存储在数据库中,然后使用 Thymeleaf 在前端以 HTML 形式显示它们。除了一件事,一切都工作得很好。
db 中的列之一,名称为“HEADLINE”,映射为 String 变量并包含新闻标题。我将此列值用作我的 Spring Boot 应用程序中的文本超链接,并结合存储在同一数据库(“news_link”列)中的 URL,以重定向到它们的来源。下面我展示了我的前端代码。
前端页面
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:c="http://www.w3.org/1999/XSL/Transform">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Spring Boot Thymeleaf Hello World Example</title>
<link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}"/>
<link rel="stylesheet" th:href="@{/styles/db.css}"/>
<script type="text/javascript" th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
</head>
<body>
<ul>
<li><a href="http://localhost:8080/">Home</a></li>
<li><a href="https://github.com/Andreas-Kreouzos">Github</a></li>
</ul>
<main role="main" class="container">
<div class="starter-template">
<h1>IoT Application</h1>
</div>
</main>
<!--Display DB contents on HTML and Thymeleaf-->
<div class="container text-center" id="tasksDiv">
<h3>Database Contents</h3>
<hr>
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>news_id</th>
<th>news_provider_name</th>
<th>HEADLINE</th>
<th>news_link</th>
<th>related_image</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
<tr th:each="rate: ${rates}">
<td th:text="${rate.news_id}"> </td>
<td th:text="${rate.news_provider_name}"> </td>
<td th:text="${rate.HEADLINE}"> </td>
<td> <a th:href="${rate.news_link}"> <p th:text="${rate.HEADLINE}"> </a> </td>
<td> <img th:src="${rate.related_image}" alt="error displaying image"> </td>
<td th:text="${rate.timestamp}"> </td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
当我运行我的应用程序时,一切都显示得很好,但只要我按下文本超链接,我就会被重定向到主页,而不是我存储在我的数据库中的给定 URL。下面,我向您展示了我的打印屏幕,在我按下它们之前,当我在浏览器中将光标悬停在它们上方时,您肯定可以看到 URL 已被正确获取和分配。但是一旦他们确实受到压力,我就会得到重定向 here ,而不是 here .
关于我做错了什么有什么想法吗?
更新
我试图将 th:href="${rate.news_link}
更改为 th:href="@{${rate.news_link}}
但仍然没有任何作用。例如,如果我的初始 URL 是
https://www.investing.com/news/cryptocurrency-news/tether-liquidates-celsius-position-with-no-losses-to-stablecoin-issuer-2845626
被切断到 https://www.investing.com/
更新 2
我创建了以下 Controller ,但我得到的是空 URL -> http://localhost:8080/null
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method(
@RequestParam(name = "news_link", required = false) String news_link) {
return new ModelAndView("redirect:" + news_link);
}
最佳答案
我用两种方法创建了一个 Controller :
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello World!");
//I have set below manually just for demo, but in real code, you can fetch this details from database & then set.
model.addAttribute("news_linkvalue", "https://www.investing.com/news/cryptocurrency-news/tether-liquidates-celsius-position-with-no-losses-to-stablecoin-issuer-2845626");
return "helloworld";
}
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method(@RequestParam(name = "news_link", required = false) String news_link) {
return new ModelAndView("redirect:" + news_link);
}
然后在 helloworld.html
中我使用了下面的 anchor 标记:
<p><a th:href="@{/redirect(news_link=${news_linkvalue})}">Click here</a></p>
当我单击 http://localhost:8080/hello
选项时使用 Click here
启动 spring boot 后,它将转到 https://www.investing.com/ 的主页并在我单击链接时给出 HTTP 301
响应。
此行为可通过单击 network
-> 单击第一条记录 -> Headers
部分查看。
这主要意味着,网站正在阻止任何跨源不安全请求(即来自具有 http
协议(protocol)的域的请求)到其主页以外的特定页面。
如果您使用任何在线 javascript 编辑器或 stackoverflow 编辑器,对特定页面使用相同的 anchor 标记,它将加载,因为请求来自安全来源 (https),示例如下:
<!DOCTYPE html>
<html>
<body>
<p>Checking Cross origin requests </p>
<p><a href="https://www.investing.com/news/cryptocurrency-news/tether-liquidates-celsius-position-with-no-losses-to-stablecoin-issuer-2845626">Click</a></p>
</body>
</html>
这主要意味着,只有在访问特定页面时协议(protocol)安全级别相同(即 HTTPS)时,本网站的特定页面(主页除外)才能访问。
本地 spring boot 使用默认的 http
协议(protocol),这就是当我们访问该网站的特定页面时,我们总是重定向到主页的原因。
如何修复:
检查您是否可以使用
https
运行本地 spring 引导代码(即通过使用单向身份验证或相互身份验证启用 ssl)。您可以在网上找到一些使用带有安全 http 的 spring boot 的示例。您可以根据可用性将您的应用程序部署到任何安全的应用程序服务器或任何云环境,并检查在非本地环境上运行应用程序是否可以解决此问题。您的应用程序 url 必须可以通过此处的
https
访问。
关于java - Thymeleaf HTML 页面转发问题到 Spring Boot App 中的外部网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72928247/