Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UrlHandlerFilter should not strip the trailing slash from the contextPath URL #33565

Closed
bdshadow opened this issue Sep 18, 2024 · 1 comment
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@bdshadow
Copy link
Contributor

Although #33444 has been fixed for root URLs, it looks like there is still a problem with the UrlHandlerFilter when server.servlet.contextPath is specified.

Here is a minimal application to reproduce it: https://github.com/bdshadow/SpringTrailingSlashTestProject

server.servlet.contextPath=/myApp

And use the following UrlHandlerFilter:

        UrlHandlerFilter filter = UrlHandlerFilter
            .trailingSlashHandler("/**").wrapRequest()
            .build();

And in this case, both http://localhost:8080/myApp/greeting and http://localhost:8080/myApp/greeting work fine, however,
http://localhost:8080/myApp and http://localhost:8080/myApp/ don't and return 404.

Controller for the root looks like this:

@RestController
@RequestMapping("/")
public class MyController {
    @GetMapping(value = {"/", "greeting"})
    public String greeting() {
        return "Hello world!";
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 18, 2024
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Sep 19, 2024
@rstoyanchev rstoyanchev self-assigned this Sep 19, 2024
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 19, 2024
@rstoyanchev rstoyanchev added this to the 6.2.0-RC2 milestone Sep 19, 2024
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Sep 19, 2024

You can map with "" which handles "/" as well. For example this works:

@RestController
public class MyController {
    @GetMapping(value = {"", "greeting"})
    public String greeting() {
        return "Hello world!";
    }
}

However, if you configure UrlHandlerFilter to redirect, it would send "/myApp/" to "/myApp", which would work for the request mappings, but Tomcat redirects "/myApp" back to "/myApp/". And in any case, I think the UrlHandlerFilter should makes checks on the pathWithinApplication, excluding the contextPath.

@rstoyanchev rstoyanchev changed the title UrlHandlerFilter works wrong with server.servlet.contextPath UrlHandlerFilter should not strip the trailing slash from the contextPath URL Sep 19, 2024
rstoyanchev added a commit that referenced this issue Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants