我使用嵌入式碼頭9.3.8.v20160314從斯卡拉(Scala是附帶在這裏,這是一個純粹的碼頭問題),配置是這樣的:如何從嵌入式Jetty URL中刪除尾部斜槓?
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/
(注意斜線):
$ 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("/")
,因爲我讀了answer on Tomcat not adding the trailing slash,但這可能不適用於我。
我必須改變以防止結尾的斜槓?
那並沒有回答OP的問題。他想知道如何解決這個問題。和我一樣,由於各種原因。 – kevr
@kevr答案是不依賴上下文根來完成這個目標。保持你的上下文根在「/」,你的實現可以做你想做的任何事情。 –