JAX-RS @CookieParam Ionic 客户端不发送 cookie(电容器)

JAX-RS @CookieParam Ionic client not sending cookies (Capacitor)

我在 headers 中发送一个带有注释 @CookieParam 的 cookie 作为参数。

第一步:register/start

    @GET
    @Path("/start")
    @Produces(MediaType.APPLICATION_JSON)
    @NoCache
    public Response start() {
        try {
            String myIdentifier = "abcdefghijkl123456789";

            LOGGER.info(String.format("My identifier: %s ", myIdentifier)); 

            // the identifier is send through the cookies in headers
            NewCookie cookie = new NewCookie("myIdentifierCookie", myIdentifier, null, null, null,
                    1200, false, false);
            return Response.ok().cookie(cookie).build();
        }catch (Exception e) {
            return Response.status(500).build();
        }
    }

从现在开始就好了。当我执行 GET 时,我可以在响应 Headers 上读取我的 cookie。 现在我正在做一个 POST 方法来检查收到的电子邮件,其中有 @CookieParam 注释。

    @POST
    @Path("/email")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @NoCache
    public Response emailCheck(@CookieParam("myIdentifierCookie") Cookie identifierCookie,
                                  CheckEmailRequest request) {

        String identifier = "";
        try {
            identifier = identifierCookie.getValue();
        } catch (Exception e) {
            LOGGER.error("exception e ", e);
            LOGGER.error(String.format("Invalid  request identifier: %s ", identifier));
            return Response.status(500).build();
        }
    }

我的 cookie 值为空,我收到:

(executor-thread-62) exception e : java.lang.NullPointerException
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:151)
        at org.jboss.resteasy.core.MethodInjectorImpl.lambda$invoke(MethodInjectorImpl.java:122)
        at java.base/java.util.concurrent.CompletableFuture.uniApplyNow(CompletableFuture.java:680)
        at java.base/java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:658)
        at java.base/java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:2158)
        at java.base/java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:143)
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:122)
        at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:594)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:468)
        at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget(ResourceMethodInvoker.java:421)
        at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:423)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:391)
        at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invoke(ResourceMethodInvoker.java:365)
        at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1183)
        at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2299)
        at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:143)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:365)
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:477)
        at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke(SynchronousDispatcher.java:252)
        at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess[=13=](SynchronousDispatcher.java:153)
        at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
        at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156)
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238)
        at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
        at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:120)
        at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.access[=13=]0(VertxRequestHandler.java:36)
        at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.run(VertxRequestHandler.java:85)
        at io.quarkus.runtime.CleanableExecutor$CleaningRunnable.run(CleanableExecutor.java:224)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
        at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2011)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1535)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1426)
        at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
        at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
        at java.base/java.lang.Thread.run(Thread.java:832)
        at org.jboss.threads.JBossThread.run(JBossThread.java:479)


我的实现有误吗?
非常感谢。

编辑:@Paul Samsotha

我用的是哪个客户端?

我正在使用 Ionic with Angular,我的插件是来自 @angular/common/http 的 angular Http 模块。 我有一个这样定义的 backend service :

  get(url, options?): Observable<any> {
    options = { ...options, withCredentials: true };
    return this.http.get(myURL, options);
  }

  post(url, body, options?): Observable<any> {
    options = { ...options, withCredentials: true };
    return this.http.post(myURL, body, options);
  }

所以在我的组件上,我是这样使用它的:

  public getRegistrationStart() {
    return this.backendService.get('/register/start', {
      observe: 'response',
    });
  }

  public emailStep(email) {
    const headers = new HttpHeaders().set('Content-Type', 'application/json');
    return this.backendService.postPublic('/register/email', email, {
      observe: 'response',
      headers,
    });
  }

我的电子邮件以 JSON 形式发送,例如 {"email":"test@test.test"}

编辑 2:@Paul

这是我的开始请求的屏幕,服务器响应 Set-Cookie 包含我的 cookie(我已经设置了一个随机数生成器),但我的 cookie 在这里。

问题不是 JAX RS,而是离子电容器阻止了 cookie。通过在 capacitor.config.json 中添加此行解决了问题:

"server":{"hostname":"mydomain"}

Cookie 在 Android 中未被拦截,它在 iOS 中被拦截,因此仅在 iOS 构建文件中的 capacitor.config.json 中添加此行解决了我的问题.

非常感谢 Paul Samsotha 向我展示了解决方案。