Apache Camel Rest DSL CORS 选项

Mat*_*son 3 rest apache-camel

我正在尝试根据休息调用的路径实现自定义选项响应。

例如我有:

rest("/employee/login").id("employee-login-options")
                .verb("options").route()
                .setHeader("Access-Control-Allow-Origin", constant("https://example.com"))
                .setHeader("Access-Control-Allow-Credentials", constant(true))
                .setHeader("Access-Control-Allow-Methods", constant("GET, HEAD, POST, PUT, DELETE, OPTIONS"))
                .setHeader("Access-Control-Allow-Headers", constant("Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization"))
                .setHeader("Allow", constant("GET, HEAD, POST, PUT, DELETE, OPTIONS"));

rest("/login").id("login-options")
        .verb("options").route()
        .setHeader("Access-Control-Allow-Origin", constant("*"))
        .setHeader("Access-Control-Allow-Methods", constant("GET, HEAD, POST, PUT, DELETE, OPTIONS"))
        .setHeader("Access-Control-Allow-Headers", constant("Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"))
        .setHeader("Allow", constant("GET, HEAD, POST, PUT, DELETE, OPTIONS"));
Run Code Online (Sandbox Code Playgroud)

因此,一条路由允许凭据并具有特定域,另一条路由则使用通配符进行响应。

我遇到的问题是他们都没有被调用。我可以看到客户端发出 OPTIONS 请求,但它没有命中我的路线。

我已经配置了我的其余配置,如下所示:

restConfiguration()
                .component("jetty")
                .scheme(JETTY_SCHEME)
                .port(JETTY_PORT);
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

restConfiguration()
        .component("jetty")
        .scheme(JETTY_SCHEME)
        .port(JETTY_PORT)
        .enableCORS(true)
        .corsAllowCredentials(true)
        .corsHeaderProperty("Access-Control-Allow-Origin","https://hello.3en.codes")
        .corsHeaderProperty("Access-Control-Allow-Headers","Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization");
Run Code Online (Sandbox Code Playgroud)

但是,这仅适用于一条路线,并且我无法在每个路线的基础上覆盖标头

我使用的是骆驼版本 2.20.0

Mat*_*son 7

通过此检查,HTTP 选项请求将绕过任何路由:https: //github.com/apache/camel/blob/8d434546520f2434d4278575a732a5632acca664/camel-core/src/main/java/org/apache/camel/processor/RestBindingAdvice。 java#L142

但经过对代码的更多研究后,我设法找到了解决我的问题的方法:

如果我保持我的restConfiguration像这样:

restConfiguration()
        .component("jetty")
        .scheme(JETTY_SCHEME)
        .port(JETTY_PORT)
        .enableCORS(true) // <-- Important
        .corsAllowCredentials(true) // <-- Important
        .corsHeaderProperty("Access-Control-Allow-Origin","*")
        .corsHeaderProperty("Access-Control-Allow-Headers","Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization");
Run Code Online (Sandbox Code Playgroud)

我将我的员工登录路线更改为:

rest("/example").post("/employee/login").consumes("application/json")
  .route().to("direct:validateUserCredentials")
  .setHeader("Origin",constant("http://localhost:4100"));
Run Code Online (Sandbox Code Playgroud)

通过在路由末尾设置“Origin”标头,camel 然后使用它来填充 Access-Control-Allow-Origin 标头,这意味着我可以根据每个路由定制标头。仅当在 restConfiguration 中将 corsAllowCredentials 设置为 true 时才有效