首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Restlet ++ CORS

Restlet ++ CORS
EN

Stack Overflow用户
提问于 2016-07-25 12:01:46
回答 1查看 211关注 0票数 0

我在Google上使用Restlet来开发我的示例应用程序。前端是角2应用程序。

Rest在浏览器中运行得很好。但是,当我试图点击角应用程序的URL时,我得到了下面的问题。

代码语言:javascript
复制
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头。我在这里错过了什么?

代码语言:javascript
复制
@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报头。

这是我使用的代码..。

代码语言:javascript
复制
   @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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-25 12:38:01

首先,我认为您可以使用服务而不是过滤器:

代码语言:javascript
复制
public MyApplication() {
    CorsService corsService = new CorsService();
    corsService.setAllowedCredentials(true);
    corsService.setSkippingResourceForCorsOptions(true);
    getServices().add(corsService);
}

你介意设置"skippingServerResourceForOptions“吗?

代码语言:javascript
复制
@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;
}

向你问好,蒂埃里·博里奥

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38567232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档