我在Google上使用Restlet来开发我的示例应用程序。前端是角2应用程序。
Rest在浏览器中运行得很好。但是,当我试图点击角应用程序的URL时,我得到了下面的问题。
XMLHttpRequest cannot load https://1-dot-jda-saas-training-02.appspot.com/rest/projectsBillingInfo/123. The 'Access-Control-Allow-Origin' header contains multiple values 'http://evil.com/, *', but only one is allowed. Origin 'http://localhost:3000' is therefore not allowed access.因此,我认为我将继续,并在响应中添加CORS头。我使用了CorsFilter,如下所示,但问题仍然存在。当我看到响应的头时,我没有看到任何CORS头。我在这里错过了什么?
@Override
public Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a
// new instance of HelloWorldResource.
Router router = new Router(getContext());
CorsFilter corsFilter = new CorsFilter(getContext(), router);
corsFilter.setAllowedOrigins(new HashSet<String>(Arrays.asList("*")));
corsFilter.setAllowedCredentials(true);
// Defines only one route
router.attachDefault(AddressServerResource.class);
router.attach("/contact/123",ContactServerResource.class);
router.attach("/projectsBillingInfo/123",ProjectBillingResource.class);
return corsFilter;}
编辑
我可以让它发挥作用。可能是我做错了什么。但是,我无法使这与GaeAuthenticator一起工作。当我将GaeAuthenticator和Corsfilter放在一起时,它跳过了认证部分。因此,身份验证或corsfilter可以工作,但不能同时工作。有什么简单的方法可以在restlet中设置/修改HTTP报头。
这是我使用的代码..。
@Override
public Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a
// new instance of HelloWorldResource.
Router router = new Router(getContext());
// Defines only one route
router.attachDefault(AddressServerResource.class);
router.attach("/contact/123",ContactServerResource.class);
router.attach("/projectsBillingInfo/123",ProjectBillingResource.class);
GaeAuthenticator guard = new GaeAuthenticator(getContext());
guard.setNext(router);
CorsFilter corsFilter = new CorsFilter(getContext(), router);
corsFilter.setAllowedOrigins(new HashSet<String>(Arrays.asList("*")));
corsFilter.setAllowedCredentials(true);
return corsFilter;发布于 2016-07-25 12:38:01
首先,我认为您可以使用服务而不是过滤器:
public MyApplication() {
CorsService corsService = new CorsService();
corsService.setAllowedCredentials(true);
corsService.setSkippingResourceForCorsOptions(true);
getServices().add(corsService);
}你介意设置"skippingServerResourceForOptions“吗?
@Override
public Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a
// new instance of HelloWorldResource.
Router router = new Router(getContext());
// Defines only one route
router.attachDefault(AddressServerResource.class);
router.attach("/contact/123",ContactServerResource.class);
router.attach("/projectsBillingInfo/123",ProjectBillingResource.class);
return router;
}向你问好,蒂埃里·博里奥
https://stackoverflow.com/questions/38567232
复制相似问题