gpt4 book ai didi

rest - ContainerRequestContext abortWith() 不会中止

转载 作者:行者123 更新时间:2023-12-01 13:32:08 25 4
gpt4 key购买 nike

这是我的身份验证过滤器:

@Secured
@Provider
@Priority(Priorities.AUTHENTICATION)
public class SecuredFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext requestContext) {

String authorizationHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}

String token = authorizationHeader.substring("Bearer".length()).trim();
// [...]
}
}

为什么在缺少 header 时它不会中止并发回响应?
现在我在 authorizationHeader.substring 得到一个明显的 NPE ...

documentation说:

void abortWith(Response response)

Abort the filter chain with a response. This method breaks the filter chain processing and returns the provided response back to the client. The provided response goes through the chain of applicable response filters. Parameters: response - response to be sent back to the client.



我错过了什么?

最佳答案

我错过了“返回”关键字:

if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
return;
}

我找到了解决方案 here - 我猜文档中的句子有误导性:

This method breaks the filter chain processing and returns the provided response back to the client



我认为包括“返回”。

关于rest - ContainerRequestContext abortWith() 不会中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45457652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com