如何为Spring Boot嵌入式Tomcat设置relaxedQueryChars
?
connector属性描述为here,但是Spring Boot documentation没有列出此类参数。
一般如何设置Tomcat的Connector属性?
答案 0 :(得分:7)
我不确定是否可以使用属性文件来执行此操作。我相信这应该可行
@Component
public class MyTomcatWebServerCustomizer
implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.setAttribute("relaxedQueryChars", "yourvaluehere");
}
});
}
}
答案 1 :(得分:3)
我这样做是为了我的工作解决方案:
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer(){
return new MyCustomizer();
}
private static class MyCustomizer implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer factory) {
if(factory instanceof TomcatEmbeddedServletContainerFactory) {
customizeTomcat((TomcatEmbeddedServletContainerFactory) factory);
}
}
void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
connector.setAttribute("relaxedPathChars", "<>[\\]^`{|}");
connector.setAttribute("relaxedQueryChars", "<>[\\]^`{|}");
});
}
}
答案 2 :(得分:2)
如果您使用的是Spring Boot 2.x,则需要使用WebSeerverFactoryCustomizer,如下所示。
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory>
containerCustomizer(){
return new EmbeddedTomcatCustomizer();
}
private static class EmbeddedTomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
connector.setAttribute("relaxedPathChars", "<>[\\]^`{|}");
connector.setAttribute("relaxedQueryChars", "<>[\\]^`{|}");
});
}
}
答案 3 :(得分:1)
最简单的方法是配置服务器(在application.properties中添加一行)。 您可以添加如下内容:
server.tomcat.relaxed-path-chars=<,>,etc