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

RequestMapping: case insensitive path matching [SPR-13286] #17876

Closed
spring-projects-issues opened this issue Jul 28, 2015 · 5 comments
Closed
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jul 28, 2015

Yi EungJun opened SPR-13286 and commented

I want @RequestMapping annotations to match paths in case-insensitive manner so that @RequestMapping("/path/to/{name}") matches not only /path/to/me but also /path/To/me, /PATH/to/me, and so on.

There is a known workaround to do that as follows:

public class CaseInsensitivePathMatcher extends AntPathMatcher {
    @Override
    protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
        return super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables);
    }
}

But it makes the values of URI template variables lowercase. I want to keep the case information.


Referenced from: commits 291550a

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 28, 2015

Yi EungJun commented

I'm trying to fix this myself. At the first glance, this issue can be solved by making AntPathMatcher matches patterns in case-insensitive manner as follows:

diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
index 3132b1c..5bee271 100644
--- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
+++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
@@ -583,7 +583,7 @@ public class AntPathMatcher implements PathMatcher {
                                end = m.end();
                        }
                        patternBuilder.append(quote(pattern, end, pattern.length()));
-                       this.pattern = Pattern.compile(patternBuilder.toString());
+                       this.pattern = Pattern.compile(patternBuilder.toString(), Pattern.CASE_INSENSITIVE);
                }
 
                private String quote(String s, int start, int end) {

But I am afraid that it causes a lot of side-effects because RequestMapping is not the only client which uses AntPathMatcher. I guess the side effects may be avoided by adding a flag for case-insensitive matching to AntPathmathcer and RequestMapping.

Is there anything more should be considered?

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 29, 2015

Juergen Hoeller commented

I've introduced a public setCaseInsensitive method on AntPathMatcher, allowing you to configure this externally. The default is still false, so there's no backwards compatibility issue. Would this make sense for your purposes?

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 29, 2015

Yi EungJun commented

It sounds great! I think it would make sense to me.

Is it possible to configure that AntPathMatcher matches in case-insensitive manner only for a specific controller? And can I try the your new AntPathMatcher?

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 29, 2015

Juergen Hoeller commented

I've turned this around into a setCaseSensitive method, defaulting to true, which can be switched to false for case-insensitive matching. This will be available in the next 4.2.0.BUILD-SNAPSHOT in about an hour.

I'm afraid the only way to customize the PathMatcher is at the HandlerMapping level, e.g. through the <mvc:annotation-driven path-matcher="..."/> attribute.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 29, 2015

Yi EungJun commented

I tested it and it works very well for me!

Thanks very much, Juergen.

Here is my configuration:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants