如何在2.2.0 Java中实现CORS过滤器?

时间:2014-02-15 11:09:03

标签: java playframework cors

我到处寻找,但我创建的所有解决方案对我都没有用,我认为问题在于我的游戏版本,但无论如何,有人可以帮助我实施一个CORS过滤器Java中的Java 2.2.0+。对我的英语很抱歉,我不太清楚它是否清楚。

我实现了这段代码:

public class CorsComposition {

    /**
     * Wraps the annotated action in an <code>CorsAction</code>.
     */
    @With(CorsAction.class)
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Cors {
        String value() default "*";
    }

    public static class CorsAction extends Action<Cors> {

        @Override
        public Promise<SimpleResult> call(Context context) throws Throwable {
            Response response = context.response();
            response.setHeader("Access-Control-Allow-Origin", "*");

            //Handle preflight requests
            if(context.request().method().equals("POST")) {
                response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
                response.setHeader("Access-Control-Max-Age", "3600");
                response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Auth-Token");
                response.setHeader("Access-Control-Allow-Credentials", "true");

                return delegate.call(context);
            }

            response.setHeader("Access-Control-Allow-Headers","X-Requested-With, Content-Type, X-Auth-Token");
            return delegate.call(context);
        }


    }
}

在我的控制器中,我把注释@Cors。这对于请求是可以的,但是当我使用POST时,它不起作用。我的Chrome控制台上显示CROSS ORIGIN NOT ALLOWED错误。

2 个答案:

答案 0 :(得分:0)

问题解决了,实际上,问题是返回的方法没有返回,因为Binding错误会停止方法。所以这段代码适用于CORS问题,所以任何人都可以使用它。

答案 1 :(得分:0)

我是java和Play的新手。这段代码很多@ renatomattos2912!

我刚才有一些正确的包含问题。所以我希望它是可以的,当我发布最终文件版本时:

path: app/actions/CorsComposition.java

文件:

package actions;

import play.libs.F;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.SimpleResult;
import play.mvc.With;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class CorsComposition {

    /**
     * Wraps the annotated action in an <code>CorsAction</code>.
     */
    @With(CorsAction.class)
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Cors {
        String value() default "*";
    }

    public static class CorsAction extends Action<Cors> {

        @Override
        public F.Promise<SimpleResult> call(Http.Context context) throws Throwable {
            Http.Response response = context.response();
            response.setHeader("Access-Control-Allow-Origin", "*");

            //Handle preflight requests
            if(context.request().method().equals("POST")) {
                response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
                response.setHeader("Access-Control-Max-Age", "3600");
                response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Auth-Token");
                response.setHeader("Access-Control-Allow-Credentials", "true");

                return delegate.call(context);
            }

            response.setHeader("Access-Control-Allow-Headers","X-Requested-With, Content-Type, X-Auth-Token");
            return delegate.call(context);
        }
    }
}

<强>更新 将这些添加到您的所有控制器:

@CorsComposition.Cors

上面的代码对我来说是GET请求。但对于帖子等,我得到了同样的错误。 为了解决这个问题,我在路线配置中添加了这些路线

OPTIONS        /*path                          controllers.Application.preflight(path: String)

在我的应用程序控制器中:

public static Result preflight(String path) {

    return ok("");

}