我试图将这两个示例Java Spring应用程序结合在一起,以便能够在一台服务器上运行我的Mongo数据库并将JSON提供给另一台服务器上的前端应用程序。如何使用Spring Java Mongo REST应用程序配置CORS?
CORS教程: http://spring.io/guides/gs/rest-service-cors/
的MongoDB与REST http://spring.io/guides/gs/accessing-mongodb-data-rest/
我目前的 “MongoDB中与REST” Spring.io示例应用程序运行,但它目前仍然没有即使添加了SimpleCORSFilter后配套CORS如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的使用,我也读过这个文件,但是无论如何,这个问题似乎也没有解决。
有着同样的问题。我正在使用spring-data和一个没有控制器的MongoRepository。实现了一个过滤器来添加标题,但仍然没有。我不明白这个问题是什么。过滤器正在被加载,但它只是没有做到预期的那样。 – 2014-10-14 17:00:21