如何从嵌入式Jetty URL中删除结尾的斜杠? [英] How to remove trailing slash from embedded Jetty URLs?

查看:166
本文介绍了如何从嵌入式Jetty URL中删除结尾的斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用来自Scala的嵌入式Jetty 9.3.8.v20160314(Scala偶然出现在这里,这是一个纯粹的Jetty问题),配置如下:

I use embedded Jetty 9.3.8.v20160314 from Scala (Scala is incidental here, this is a pure Jetty question), configured like this:

val loginService = new RepoLoginService(usersRepository, signInsRepo)

val server = new Server(8080)
server.addBean(loginService)

val security = new ConstraintSecurityHandler()
server.setHandler(security)

val constraint = new Constraint()
constraint.setName("auth")
constraint.setAuthenticate(true)
constraint.setRoles(Array[String]("user", "admin"))

val mapping = new ConstraintMapping()
mapping.setPathSpec("/*")
mapping.setConstraint(constraint)

security.setConstraintMappings(Collections.singletonList(mapping))
security.setAuthenticator(new BasicAuthenticator())
security.setLoginService(loginService)

val mapper = prepareJacksonObjectMapper

val listAccountsController = new ContextHandler("/api/accounts")
listAccountsController.setHandler(new ListAccountsController(mapper, accountsRepository))

val importBankAccountsController = new ContextHandler("/api/bank-account-transactions/import")
importBankAccountsController.setHandler(new ImportBankAccountTransactionsController(mapper, bankAccountTransactionsRepo))

val contexts = new ContextHandlerCollection()
contexts.setHandlers(Array(listAccountsController, importBankAccountsController))

security.setHandler(contexts)

server.start()
server.join()

请注意 listAccountsController 的上下文: / api / accounts 。当我卷曲 / api / accounts 时,我会收到一个重定向到 / api / accounts / 的路径(请注意末尾的斜线) :

Notice the context for the listAccountsController: /api/accounts. When I curl /api/accounts, I receive a redirect to /api/accounts/ (note the trailing slash):

$ curl --silent --verbose --user francois:francois http://localhost:8080/api/accounts
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'francois'
> GET /api/accounts HTTP/1.1
> Host: localhost:8080
> Authorization: Basic ZnJhbmNvaXM6ZnJhbmNvaXM=
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 302 Found
< Date: Sun, 22 May 2016 03:54:44 GMT
< Location: http://localhost:8080/api/accounts/
< Content-Length: 0
< Server: Jetty(9.3.8.v20160314)
<
* Connection #0 to host localhost left intact

我尝试将呼叫添加到 clearAliasChecks(),我不确定这是否与此相关。我还尝试将 mapping.setPathSpec( / *)更改为 mapping.setPathSpec( /)我在Tomcat上读了一个答案,没有添加斜杠,但这可能仍然不适用于我。

I tried adding a call to clearAliasChecks(), which I'm not sure is even related to this. I also tried changing mapping.setPathSpec("/*") to mapping.setPathSpec("/"), because I read an answer on Tomcat not adding the trailing slash, but this probably doesn't apply to me anyway.

为防止斜杠,我必须更改什么?

What must I change to prevent the trailing slash?

推荐答案

您的方式

A ContextHandler 是一组资源的根 ,并且该根目录始终定义为资源目录(在结构上)。

A ContextHandler is a "root" to a set of resources, and that root is defined to always be a resource directory (in structure).

自定义 ContextHandler( / api / accounts) 并访问 / api / accounts 实现,通过制作 / api / accounts / 正确的上下文根。

Since you defined ContextHandler("/api/accounts") and accessed /api/accounts the implementation "helps" you by making /api/accounts/ the correct Context root.

想象一下...


  • 您有 val b = ContextHandler( / a / b)

  • 您有 val a = ContextHandler( / a)-实现(内部)支持子资源 / b

  • 如果您请求 / a / b ,您处于哪个环境中? -根据规范,您位于上下文 a

  • 如果您请求 / a / b / ,那么您处于上下文 b

  • You have val b = ContextHandler("/a/b")
  • You have val a = ContextHandler("/a") - which implements (internally) support for sub-resource /b
  • If you request /a/b, which context are you in? - Per spec, you are in context a
  • If you request /a/b/, then you are in context b

这篇关于如何从嵌入式Jetty URL中删除结尾的斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆