jax rs@cookieparam离子客户端不发送cookie(电容器)

szqfcxe2  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(352)

我正在发送一个cookie作为带有注解@cookieparam的头中的参数。
第一步: 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时,我可以在响应头上读取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$3(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$2(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$1(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$4(SynchronousDispatcher.java:252)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(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$000(VertxRequestHandler.java:36)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.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` . 我有一个 `backend service` 定义如下:

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

post(url, body, options?): Observable {
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:@保罗
在我的启动请求屏幕上,服务器用一个包含我的cookie的集cookie(我已经设置了一个随机数生成器)来响应,但是我的cookie在这里。
![](https://i.stack.imgur.com/MpZcK.png)
amrnrhlw

amrnrhlw1#

问题不是jax-rs,而是阻止cookies的离子电容器。通过将此行添加到 capacitor.config.json :

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

cookie在android中没有被拦截,它是在ios中被拦截的,所以在 capacitor.config.json 只有在ios构建文件中解决了我的问题。
非常感谢保罗·萨索塔,他为我指明了解决问题的途径。

相关问题