我正在开发一个基于 Wildfly Swarm 的项目。我目前遇到的问题是,RESTeasy 隐藏了我的 index.html(和其他 html 文件),这些文件位于/webapp 下方,因为 RESTeasy 正在根级别监听。
我的主要应用程序:
@ApplicationPath("/")
public class XYZnlineApplication extends Application {
}
我的资源之一:
@Path("protected/api/admin")
public class AdminResource {
@GET
@Path("public/api/offer/reduced")
@Produces("application/json")
public List<XYZ> getXYZ() {
...
}
@GET
@Path("protected/api/offer/full")
@Produces("application/json")
public List<XYZ> getAllXYZ() {
...
}
}
事情是这样的。如果我启动我的 Wildfly swarm 应用程序并访问上面的休息点之一,则一切正常(例如 http://localhost:8080/app/public/api/offer/reduced )
但是,如果我想访问位于/webapp 正下方的 html(例如,login.html)文件之一,尽管该文件已正确捆绑(例如,在尝试访问 http://localhost:8080/app/login.html 时),我会收到 404 错误。所以在我看来,发生的情况是 RESTeasy 隐藏了这个 html 文件,因为它监听根 (/)。
由于我的 url 的第一部分是上下文(由代理注入(inject)),因此我无法将除 root (/) 之外的任何内容设置为 XYZApplication 中的 ApplicationPath。
您知道如何解决这个问题吗?
预先非常感谢您的帮助。
最佳答案
您需要将 ApplicationPath 更改为“/services”或“/svc”或任何适合您的内容。最终,您需要在静态资源和服务之间划分 URL 命名空间。指定 ApplicationPath 时无需担心上下文。
主要编辑:
你的评论确实解释了发生了什么。我不确定您到底使用什么类型的安全性,但最终您可能需要在服务前面设置某种过滤器。我想要这样的东西:
@Provider
@Priority(Priorities.AUTHENTICATION)
@PreMatching
public class AuthFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest httpServletRequest;
@Context
private HttpServletResponse httpServletResponse;
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
if( containerRequestContext.getUriInfo().getPath().contains("/public/") )
return; // user is in public area - doesn't matter if they are authenticated
// guess at how to check if user is authenticated
if( httpServletRequest.getSession().get("user_is_ok") )
return;
httpServletResponse.sendRedirect("/login");
// or maybe
httpServletResponse.sendError(SC_UNAUTHORIZED);
}
}
同样,这只是猜测,但这是应对挑战的一种非常常见的方式。
关于java - Wildfly Swarm RESTeasy 隐藏 webapp/index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45196739/