【发布时间】:2016-02-06 23:57:48
【问题描述】:
我有一个关于 Spring MVC/Rest 的问题。
假设我有一项服务,您可以通过输入以下 url http://localhost:8080/project/api/get/user/1 来检索用户的用户详细信息 (JSON)。
我的前端应用程序在: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,为什么我仍然能够获取甚至发布数据?
【问题讨论】:
标签: spring rest spring-mvc spring-security spring-rest