我需要设置" Access-Control-Allow-Origin"到" *"按代码。
阅读this我尝试使用org.eclipse.jetty.servlets.CrossOriginFilter
设置setInitParameter
:
Server server = new Server(activity.WEB_SERVER_PORT);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setWelcomeFiles(new String[] { "index.html" });
context.setContextPath("/");
context.setInitParameter("org.eclipse.jetty.servlets.CrossOriginFilter", "/*");
TheServlet sv = new TheServlet();
context.addServlet(new ServletHolder(sv),"/servleturl");
server.setHandler(context);
try {
server.start();
} catch (Exception e) { }
但它不起作用。我怎么解决?
答案 0 :(得分:2)
尝试将addFilter方法与filterHolder一起使用。这段代码可能会有所帮助
FilterHolder filterHolder = new FilterHolder(CrossOriginFilter.class);
filterHolder.setInitParameter("allowedOrigins", "*");
filterHolder.setInitParameter("allowedMethods", "GET, POST");
ServletContextHandler servletContextHandler;
servletContextHandler = new ServletContextHandler(webServer, "/", ServletContextHandler.SESSIONS);
servletContextHandler.addServlet(atmosphereServletHolder, "/atmosphere/*");
servletContextHandler.addServlet(jerseyServletHolder, "/jersey/*");
servletContextHandler.addFilter(filterHolder, "/*", null);