简体   繁体   English

如何从应用程序级别设置 tomcat 的轻松查询字符

[英]How to set relaxedQueryChars of tomcat from the application level

Allowing special characters in request URL is possible in tomcat_folder/conf/server.xml as far as I know, but I am somewhat required to find another way to set these special characters from the application side (if it is possible), I mean using web.xml or any other way.据我所知,在 tomcat_folder/conf/server.xml 中允许请求 URL 中的特殊字符是可能的,但我有点需要找到另一种从应用程序端设置这些特殊字符的方法(如果可能的话),我的意思是使用web.xml 或任何其他方式。 Here, I have enabled "[" (opening square bracket) and "]" (closing square bracket) in the query:在这里,我在查询中启用了“[”(左方括号)和“]”(右方括号):

    <Connector port="8080" protocol="HTTP/1.1" relaxedQueryChars='|[]'
           connectionTimeout="20000"
           redirectPort="8443"
           URIEncoding="UTF-8"/>

This works pretty fine and I would appreciate it if anyone can help me to set without touching server.xml.这工作得很好,如果有人可以帮助我在不接触 server.xml 的情况下进行设置,我将不胜感激。

Info about the application environment: Tomcat version: 7.0.96, Spring: 4.1.7.RELEASE应用环境信息: Tomcat 版本:7.0.96,Spring:4.1.7.RELEASE

I agree with @M.Deinum .我同意@M.Deinum

It is not recommended to touch server.xml of the tomcat using java code as it can stop other application from working properly.不建议使用 java 代码触摸server.xml的 server.xml 代码,因为它可以阻止其他应用程序正常工作。

But still you want to do, you can take the risk.但是你仍然想做,你可以冒险。

Sample Java code:样品 Java 代码:

import java.io.File;
import java.util.Arrays;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class RelaxedQueryCharsExample {

    public static void main(String[] args)
            throws ParserConfigurationException, SAXException, TransformerException {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        File file = new File("D:/tomcat/conf/server.xml");
        Document document = documentBuilder.parse(file);
        NodeList list = document.getElementsByTagName("Connector");
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            NamedNodeMap map = node.getAttributes();
            if (map.getNamedItem("port").getNodeValue().equalsIgnoreCase("8080") 
      && map.getNamedItem("protocol").getNodeValue().equalsIgnoreCase("HTTP/1.1")) {
                map.getNamedItem("relaxedQueryChars").setNodeValue("|[]");
            }

        }

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        document.setXmlStandalone(true);
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(file);
        transformer.transform(source,result);
    }

}

Note: Run this code before running tomcat server.注意:在运行 tomcat 服务器之前运行此代码。 In between running this may crash the server.在运行之间,这可能会使服务器崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM