我编写了一个提供休息服务的无状态 EJB。我正在使用 Wildfly 10 应用程序服务器并使用 Netbeans IDE 进行开发。
我试图通过在服务方法的 http-response 中添加 max-age 标头来缓存几个方法的返回值几个小时。请考虑这是我的 bean 的简化版本:
@Stateless
@DeclareRoles({"role1","role2"})
@RolesAllowed({"role1","role2"})
@Path("api-dummy")
public class DummyApiREST {
@EJB
private StuffFacade stuffFacade;
@GET
@Path("get-stuff")
@Produces({MediaType.APPLICATION_JSON})
public Response findStuff() {
Stuff stuff = stuffFacade.getStuff();
Response.ResponseBuilder builder = Response.status(Response.Status.OK).entity(stuff);
Utils.setCacheHeader(maxAgeSeconds, builder);
return builder.build();
}
}
和 setCacheHeader 方法:
private static Response.ResponseBuilder setCacheHeader(int maxAgeSeconds, Response.ResponseBuilder builder) {
CacheControl cc = new CacheControl();
cc.setNoCache(false);
cc.setNoStore(false);
cc.setMaxAge(maxAgeSeconds);
cc.setPrivate(true);
builder.cacheControl(cc);
return builder;
}
但是“get-stuff”返回的响应总是包含Cache-Control 标头的副本;重复的标头包含 no-cache 指令(这也是一个 Pragma 标头):
HTTP/1.1 200 OK
Expires: 0
Cache-Control: no-cache, no-store, must-revalidate
Cache-Control: no-transform, max-age=60, private
X-Powered-By: Undertow/1
Server: WildFly/10
Pragma: no-cache
Date: Thu, 13 Apr 2017 15:11:17 GMT
Connection: keep-alive
Content-Type: application/json
我想问题是由 EJB 中 JAX-RS 服务的默认行为(或过滤器)引起的。我的问题是:
注意:也许它不相关,我已经配置了 jdbc 安全域,并且用户身份验证和主体运行良好。
问候
我找到了解决办法。默认情况下,Wildfly(我认为JBoss的太)增加了无缓存指令到所有的私有资源(资源需要身份验证)。
您必须通过在server-container标记中添加属性disable-caching -*来更改standalone.xml文件中的配置:
<servlet-container name="default" disable-caching-for-secured-pages="false">
<jsp-config/>
<websockets/>
</servlet-container>
通过这种方式,不会再将 Pragma和nocache指令添加到响应和我在问题中发布的代码中,它只是按预期工作。
编辑:提醒,当在同一 URL 上请求私有资源时,必须将不同的内容返回给不同的用户,您必须:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句