当我尝试在Lagom HTTP RestCall的Header中发送“access_token”参数时遇到问题:
@Override
public HeaderServiceCall<NotUsed, GetUserInfoWrapperResponse> getUserInfo() {
return (request, requestBody) -> {
UUID userId;
try {
userId = UUID.fromString(request.getHeader("access_token").get());
System.out.println("user id = " + userId);
return userEntityRef(userId).ask(new GetUserInfoCommand()).thenApply(reply -> Pair.create(ResponseHeader.OK, reply.userInfo.get()));
} catch (Exception e) {
throw new NotFound("User with access_token ");
}
};
}
我总是得到NotFoundException,因为 Headers 中不存在“access_token” .
但是,当我通过“访问令牌”更改“access_token”时,它可以工作 .
当我做一些研究时,我知道我需要将CrosFilter播放到我的application.conf:
play.http.filters = "com.test.user.impl.AccessFilter"
play.filters.cors {
// review the values of all these settings to fulfill your needs. These values are not meant for production.
pathPrefixes = ["/cms"]
allowedOrigins = null
allowedHttpMethods = null
allowedHttpHeaders = ["Origin", "X-Requested-With", "Content-Type", "Accept", "Referer", "User-Agent", "access_token", "cache-control"]
# The exposed headers
exposedHeaders = ["Origin", "X-Requested-With", "Content-Type", "Accept", "Referer", "User-Agent", "access_token", "cache-control"]
supportsCredentials = false
preflightMaxAge = 6 hour
}
但即便如此,“access_token”标头参数始终不存在 .
有帮助吗?请!
1 回答
这是因为你在名称中使用了下划线;它不是禁止但不常见的,而某些服务器如
Nginx
会丢弃它们,除非you explicitly define them in its config file.你也可以看看这个问题:Why underscores are forbidden in HTTP header names .