我有一个关于 Spring MVC/Rest 的问题。
假设我有一项服务,您可以在其中通过输入以下 url 来检索用户的用户详细信息 (JSON) http://localhost:8080/project/api/get/user/1
。
我的前端应用程序运行在:http://localhost:9000/
并且我能够从 API 获取数据。但是,我可以在任何域上执行此操作。我的目标是只提供一个(或列表)域/ips。
所以我在我的 API 中添加了以下过滤器,以便它只接受来自 localhost:9000 的调用
@Component
public class CORSFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain)
throws ServletException, IOException {
res.addHeader("Access-Control-Allow-Origin","http://localhost:9000");
/* other code*/
现在它确实阻止了来自其他域的调用。但是,如果我使用 Postman 之类的工具,我仍然可以获取数据!我错过了什么吗?我在这里做的事情是安全和正确的方式吗?如果我使用 Postman,为什么我仍然能够获取甚至发布数据?