嵌入式Jetty重写无法正常工作 [英] Embedded Jetty rewrites not working properly
问题描述
按照Jetty文档示例,我正在尝试在嵌入式Jetty服务器中实现一个简单的重写规则.
I am trying to implement a simple rewrite rule in an embedded Jetty server, following the Jetty documentation examples.
我希望对/admin/
的请求重写为/admin.html.
目前,如果我请求/admin/
,我会收到404错误,找不到/admin.html
.
但是,如果我直接请求/admin.html
,它将起作用!
I want requests to /admin/
to rewrite to /admin.html.
At the moment if I request /admin/
I get a 404 error with /admin.html
not found.
But if I request /admin.html
directly, it works!
stackoverflow上还有2个其他类似的帖子,但问题没有答案!
There are 2 other similar posts on stackoverflow but no answers to the question!
代码如下:
WebAppContext _ctx = new WebAppContext();
_ctx.setContextPath("/");
_ctx.setDefaultsDescriptor(JETTY_DEFAULTS_DESCRIPTOR);
_ctx.setParentLoaderPriority(true);
_ctx.setWar(getShadedWarUrl());
_ctx.setResourceBase(getShadedWarUrl());
RewriteHandler rewriter = new RewriteHandler();
rewriter.setRewritePathInfo(true);
rewriter.setRewriteRequestURI(true);
rewriter.setOriginalPathAttribute("requestedPath");
RewritePatternRule admin = new RewritePatternRule();
admin.setPattern("/admin/");
admin.setReplacement("/admin.html");
admin.setTerminating(true); // this will stop Jetty from chaining the rewrites
rewriter.addRule(admin);
_ctx.setHandler(rewriter);
HandlerCollection _handlerCollection = new HandlerCollection();
_handlerCollection.setHandlers(new Handler[] {_ctx});
server.setHandlers(_result);
推荐答案
替换两行...
_ctx.setHandler(rewriter);
_handlerCollection.setHandlers(new Handler[] {_ctx});
使用
rewriter.setHandler(_ctx);
_handlerCollection.setHandlers(new Handler[] {rewriter});
这将使重写程序规则在 正常上下文处理之前生效.
That will make the rewriter rules kick in before the normal context handling.
将上下文处理视为一棵树.在您的示例代码中,您有....
Think of the context handling as a tree. In your example code you have ....
server
+-- HandlerCollection
[0]-- WebAppContext
+-- Your servlets and filters in web.xml
+-- DefaultServlet
+-- RewriteHandler
这意味着如果WebAppContext
无法处理请求,则执行RewriteHandler
以查看其是否可以处理请求.这将永远不会发生,因为如果没有其他匹配项,则将WebAppContext
设置为使用DefaultServlet
.
That means if the WebAppContext
can't handle the request, then the RewriteHandler
is executed to see if it can handle the request. That will never happen, as WebAppContext
is setup to use DefaultServlet
if nothing else matches.
建议进行简单的更改即可将树更改为如下形式...
The simple change suggested changes the tree to look like this ...
server
+-- HandlerCollection
[0]-- RewriteHandler
+-- WebAppContext
+-- Your servlets and filters in web.xml
+-- DefaultServlet
这将允许RewriteHandler
在甚至要求WebAppContext
之前执行其操作.
This will allow the RewriteHandler
to do its thing before the WebAppContext
is even asked.
注意:在这种情况下,您也可以让代码更适当地利用HandlerCollection
.
Note: you can also have your code utilize the HandlerCollection
a bit more properly for this scenario as well.
// _ctx.setHandler(rewriter);
// rewriter.setHandler(_ctx);
_handlerCollection.setHandlers(new Handler[] { rewriter, _ctx });
这将导致以下树
server
+-- HandlerCollection
[0]-- RewriteHandler
[1]-- WebAppContext
+-- Your servlets and filters in web.xml
+-- DefaultServlet
这篇关于嵌入式Jetty重写无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!