嵌入式码头无过滤器名为交叉原素
我正在尝试在嵌入式码头服务器中实现CORS。除了这个例外,我现在感到迷失了。
java.lang.IllegalStateException: No filter named cross-origin
at org.eclipse.jetty.servlet.ServletHandler.updateMappings(ServletHandler.java:1260)
at org.eclipse.jetty.servlet.ServletHandler.doStart(ServletHandler.java:179)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89)
at org.eclipse.jetty.server.handler.ScopedHandler.doStart(ScopedHandler.java:112)
at org.eclipse.jetty.server.session.SessionHandler.doStart(SessionHandler.java:486)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89)
at org.eclipse.jetty.server.handler.ScopedHandler.doStart(ScopedHandler.java:112)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:955)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:388)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:896)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:306)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:121)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171)
at org.eclipse.jetty.server.Server.start(Server.java:469)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:414)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.projectsanatan.restsanatan.RestSanatan.start(RestSanatan.java:38)
at org.projectsanatan.restsanatan.Main.main(Main.java:16)
我的代码是,
private void initServlets() {
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
ResourceConfig resourceCfg = new ResourceConfig();
resourceCfg.packages(BookResource.class.getPackage().getName(), "/*");
ServletContainer servletContainer = new ServletContainer(resourceCfg);
ServletHolder servletHolder = new ServletHolder(servletContainer);
servletContextHandler.addServlet(servletHolder, "/api/*");
// servletContextHandler.addFilter(null, KEY, null)
FilterHolder corsFilter = new FilterHolder();
corsFilter.setInitParameter("allowedOrigins", "https://example.com");
corsFilter.setInitParameter("allowedMethods", "POST,GET,OPTIONS,PUT,DELETE,HEAD");
corsFilter.setInitParameter("allowedHeaders", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
corsFilter.setInitParameter("preflightMaxAge", "728000");
corsFilter.setInitParameter("allowCredentials", "true");
corsFilter.setInitParameter("chainPreflight", "false");
CrossOriginFilter cors = new CrossOriginFilter();
corsFilter.setFilter(cors);
FilterMapping corsFilterMap = new FilterMapping();
corsFilterMap.setFilterName("cross-origin");
corsFilterMap.setPathSpec("/*");
corsFilterMap.setDispatcherTypes(EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
servletContextHandler.getServletHandler().addFilter(corsFilter, corsFilterMap);
this.handlerList.addHandler(servletContextHandler);
this.setHandler(this.handlerList);
}
我对码头和servlet东西的经验不太经验,所以我很困惑,帮助您将不胜感激!
I am trying to implement cors in a embedded jetty server. I feel lost right now with this exception.
java.lang.IllegalStateException: No filter named cross-origin
at org.eclipse.jetty.servlet.ServletHandler.updateMappings(ServletHandler.java:1260)
at org.eclipse.jetty.servlet.ServletHandler.doStart(ServletHandler.java:179)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89)
at org.eclipse.jetty.server.handler.ScopedHandler.doStart(ScopedHandler.java:112)
at org.eclipse.jetty.server.session.SessionHandler.doStart(SessionHandler.java:486)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89)
at org.eclipse.jetty.server.handler.ScopedHandler.doStart(ScopedHandler.java:112)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:955)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:388)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:896)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:306)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:121)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171)
at org.eclipse.jetty.server.Server.start(Server.java:469)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:414)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93)
at org.projectsanatan.restsanatan.RestSanatan.start(RestSanatan.java:38)
at org.projectsanatan.restsanatan.Main.main(Main.java:16)
My code is
private void initServlets() {
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
ResourceConfig resourceCfg = new ResourceConfig();
resourceCfg.packages(BookResource.class.getPackage().getName(), "/*");
ServletContainer servletContainer = new ServletContainer(resourceCfg);
ServletHolder servletHolder = new ServletHolder(servletContainer);
servletContextHandler.addServlet(servletHolder, "/api/*");
// servletContextHandler.addFilter(null, KEY, null)
FilterHolder corsFilter = new FilterHolder();
corsFilter.setInitParameter("allowedOrigins", "https://example.com");
corsFilter.setInitParameter("allowedMethods", "POST,GET,OPTIONS,PUT,DELETE,HEAD");
corsFilter.setInitParameter("allowedHeaders", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
corsFilter.setInitParameter("preflightMaxAge", "728000");
corsFilter.setInitParameter("allowCredentials", "true");
corsFilter.setInitParameter("chainPreflight", "false");
CrossOriginFilter cors = new CrossOriginFilter();
corsFilter.setFilter(cors);
FilterMapping corsFilterMap = new FilterMapping();
corsFilterMap.setFilterName("cross-origin");
corsFilterMap.setPathSpec("/*");
corsFilterMap.setDispatcherTypes(EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
servletContextHandler.getServletHandler().addFilter(corsFilter, corsFilterMap);
this.handlerList.addHandler(servletContextHandler);
this.setHandler(this.handlerList);
}
I am not very much experienced with jetty and servlet stuff so I am confused, help would be really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些问题。
servletContexthandler
完成,而不是servlethandler
。filterholder
与名称映射,则必须命名filterholder
。名称的映射对于嵌入式杰出的使用过于复杂,实际上仅存在于
web-inf/web.xml
基于配置的>>。只需直接使用
filterholder
。servletContexThandler
basereSource声明defaultServlet
在servletContext
defaulthandler
在您的顶级处理程序树上(通常设置为最后一个条目),正确的设置就是这样...
crossoriginfilter
的过滤器映射已指定dispatchertype
of <代码> include 和请求
,这是一个非常奇怪的设置。从
进行CORS include
(或forward
)dispatch非常尴尬,跳过它。但是您没有指定
async
,这实际上是非常普遍的。我会以这种方式更改映射...
要牢记使用
crossoriginfilter
的事情。Origin
请求标头的请求不会触发CrossoriginFilter
。There's a few issues.
ServletContextHandler
, not theServletHandler
.FilterHolder
+ Mapping with name, then you have to name yourFilterHolder
.Mapping with name is overly complicated for embedded-jetty use, it really only exists for
WEB-INF/web.xml
based configuration. Just use theFilterHolder
directly.ServletContextHandler
BaseResource declarationDefaultServlet
on yourServletContext
DefaultHandler
on your top level handler tree (usually set as last entry)A correct setup would be like this ...
The Filter mapping of your
CrossOriginFilter
has specified theDispatcherType
set ofINCLUDE
andREQUEST
, that's a really odd setup.Doing CORS from an
INCLUDE
(orFORWARD
) dispatch is super awkward, skip it.But you didn't specify
ASYNC
, which is actually super common.I would change the mapping this way ...
Things to keep in mind for using
CrossOriginFilter
.Origin
request header will not trigger theCrossOriginFilter
.