【发布时间】:2014-11-24 09:19:47
【问题描述】:
我正在尝试将这两个示例 Java Spring 应用程序组合在一起,以便能够在一台服务器上运行我的 Mongo 数据库并将 JSON 提供给另一台服务器上的前端应用程序。
CORS 教程: http://spring.io/guides/gs/rest-service-cors/
带有 REST 的 MongoDB http://spring.io/guides/gs/accessing-mongodb-data-rest/
我当前的“带有 REST 的 MongoDB”Spring.io 示例应用程序运行,但它目前仍然不支持 CORS,即使在添加了 SimpleCORSFilter 类后,如 CORS 示例(上图)所示。
我想我可能只需要在某个地方配置这个新的 SimpleCORSFilter 类 - 在 web.xml 的注释等效项中 - 所以我试图在 SimpleCORSFilter 类中使用 @WebFilter("/*")。
根据“MongoDB with REST”教程页面,“@Import(RepositoryRestMvcConfiguration.class) 注解导入 Spring MVC 控制器、JSON 转换器和提供 RESTful 前端所需的其他 bean 的集合。这些组件链接起来到 Spring Data MongoDB 后端。”这可能表明我还应该覆盖 RepositoryRestMvcConfiguration 中的某些内容来配置 SimpleCORSFilter 过滤器。
我尝试在 SimpleCORSFilter 类本身上添加 @WebFilter("/"),但在使用 curl --verbose 的响应中没有看到 Access-Control- 标头。结果,我认为过滤器配置不正确。
有没有人将这两个教程项目(或类似的项目)结合起来?
作为参考,我要配置的 SimpleCORSFilter 类在这里:
package foo.bar;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
@WebFilter("/*")
@Component
public class SimpleCORSFilter extends OncePerRequestFilter implements Filter {
/* Attempt #1 - based on original demo code from spring.io - could not get to work
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
*/
// Attempt #2 - based on https://stackoverflow.com/questions/20583814/angular-and-cors which advised use of addHeader() instead of setHeader() - still no luck
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS");
response.addHeader("Access-Control-Allow-Headers",
"origin, content-type, accept, x-requested-with, sid, mycustom, smuser");
filterChain.doFilter(request, response);
}
// unable to override this final method
//public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
...目前似乎正在执行配置的主要应用程序类在这里:
package foo.bar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
@Configuration
@EnableMongoRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注意:我确实在这里找到了一个有点相似的帖子:CORS with Spring not working,但该解决方案涉及使用 JSONP,我还读到它是一种 hack,无论如何,这个问题似乎没有得到解决要么。
【问题讨论】:
-
有同样的问题。我正在使用没有控制器的 spring-data 和 MongoRepository。实现了一个过滤器来添加标题,但仍然没有。我不明白问题是什么。正在加载过滤器,确保它没有按照预期进行。
标签: java spring mongodb rest cors