我試圖將這兩個示例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