Embed Tomcat SSL报错:Invalid character found in method name. HTTP method names must be tokens

西夏一品堂 发布于 2016/09/18 21:20
阅读 21K+
收藏 0

JDK1.8

embed tomcat版本:8.5.4

spring.keystore 使用的是JDK1.8的keytool生成的(keytool -genkeypair -keystore spring.keystore)

代码如下:


package com.pp.ws.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().print("hello tomcat");
	}
}
package com.pp.ws.server;

import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.Tomcat.FixContextListener;
import org.apache.coyote.http11.Http11NioProtocol;

public class App2 {
	
	public static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";
	static final int port = 9090;
	
	public static void main(String[] args) throws Exception {
		Tomcat tomcat = new Tomcat();
        tomcat.setPort(port);
        tomcat.setBaseDir("e:/tmp");
        
        Connector connector = new Connector(DEFAULT_PROTOCOL);
        connector.setPort(port);
        
        Http11NioProtocol protocol = (Http11NioProtocol)connector.getProtocolHandler();
        protocol.setKeystorePass("123456");
        protocol.setKeystoreFile("e:/tmp/spring.keystore");
        protocol.setKeyAlias("mykey");
        
        tomcat.getService().addConnector(connector);
        tomcat.setConnector(connector);
        tomcat.getHost().setAutoDeploy(false);
        
        StandardContext context = new StandardContext();
        context.setName("/book");
        context.setPath("/book");
        context.setDocBase("e:/tmp/work");
        context.addLifecycleListener(new FixContextListener());
        tomcat.getHost().addChild(context);
        
        tomcat.addServlet("/book", "/home", new HomeServlet());
        context.addServletMapping("/home", "/home");
        tomcat.start(); 
        tomcat.getServer().await();
	}
}
使用http访问 http://127.0.0.1:9090/book/home  没有问题,正常


使用https访问 https://127.0.0.1:9090/book/home 报错


Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
	at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:462)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:994)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

请问,这是什么问题,怎么解决




加载中
0
zhonghai
zhonghai

加上这两个试试,不过现在的密钥要用RSA加密,要不然只能在IE上打开,chrome会禁止打开

connector.setScheme("https");

connector.setAttribute("SSLEnabled", true);

0
littleant
littleant

下面是一个完整是示例代码,我去年写的,你可以参考,都是可以运行的

package little.ant.platform.run;

import java.io.File;

import javax.servlet.ServletException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;

/**
 * 内嵌式tomcat
 * @author 董华健  dongcb678@163.com
 */
public class TomcatEmbed7 {

	private static String PROJECT_PATH = System.getProperty("user.dir");

	/**
	 * 项目路径,标准的java web项目结构
	 */
	private static final String projectPath = "D:/DevelopmentTool/eclipse-4.5-jee/git/JfinalUIB/JFinalUIBV2/WebContent";

	/**
	 * ssl配置需要的证书路径
	 */
	private static final String sslStorePath = "D:/DevelopmentTool/eclipse-4.5-jee/workspace/TomcatEmbed7/build-lib/ssl";

    public static void main(String[] args) {
    	// 普通http
		//http();		
		
		// 启用ssl,单向证书加密
		//httpsSingle();
		
		// 启用ssl,双向证书加密
		httpsDouble();
    }

	/**
	 * 普通http,端口8080
	 * 访问路径:http://127.0.0.1:8080
	 */
	public static void http(){
		try {
            Tomcat tomcat = new Tomcat();
            tomcat.setPort(8080);
            
            // TomcatEmbed7 目录
            tomcat.setBaseDir(PROJECT_PATH + File.separator + "build-lib" + File.separator +  "TomcatEmbed7");
            
            StandardServer server = (StandardServer) tomcat.getServer();
            AprLifecycleListener listener = new AprLifecycleListener();
            server.addLifecycleListener(listener);
            
            /**
             * 添加一个根路径应用,这种方式适合把TomcatEmbed在独立项目中运行
             */
            tomcat.addWebapp("", projectPath); 
            
            /**
             * 添加一个根路径应用,这种方式适合把TomcatEmbed文件整合到你的项目中
             */
            // tomcat.addWebapp("", PROJECT_PATH + File.separator +  "WebRoot" ); 
            
            tomcat.start();
            server.await();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (LifecycleException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * https单向加密,端口8443
	 * 访问路径:https://127.0.0.1:8443
	 */
	public static void httpsSingle(){
        try {
            Tomcat tomcat = new Tomcat();
            
            // 设置Connector属性
    		Connector connector = tomcat.getConnector();
    	    connector.setPort(8443);
    	    connector.setSecure(true);
    	    connector.setScheme("https");
    	    
    	    connector.setAttribute("SSLEnabled", true);
            connector.setAttribute("sslProtocol", "TLS");
            connector.setAttribute("protocol", "org.apache.coyote.http11.Http11Protocol");

            // 单向加密证书配置
            connector.setAttribute("clientAuth", false);
            connector.setAttribute("keystoreFile", sslStorePath + "/oneway/cas.keystore");
            connector.setAttribute("keystorePass", "678789");
            
            // TomcatEmbed7 目录
            tomcat.setBaseDir(PROJECT_PATH + File.separator + "build-lib" + File.separator +  "TomcatEmbed7");
            
            StandardServer server = (StandardServer) tomcat.getServer();
            AprLifecycleListener listener = new AprLifecycleListener();
            server.addLifecycleListener(listener);
            
            /**
             * 添加一个根路径应用,这种方式适合把TomcatEmbed在独立项目中运行
             */
            tomcat.addWebapp("", projectPath); 
            
            /**
             * 添加一个根路径应用,这种方式适合把TomcatEmbed文件整合到你的项目中
             */
            // tomcat.addWebapp("", PROJECT_PATH + File.separator +  "WebRoot" ); 
            
            tomcat.start();
            server.await();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (LifecycleException e) {
			e.printStackTrace();
		}
	}

	/**
	 * https双向加密,端口8443
	 * 访问路径:https://127.0.0.1:8443
	 */
	public static void httpsDouble(){
        try {
            Tomcat tomcat = new Tomcat();
            
            // 设置Connector属性
    		Connector connector = tomcat.getConnector();
    	    connector.setPort(8443);
    	    connector.setSecure(true);
    	    connector.setScheme("https");
    	    
    	    connector.setAttribute("SSLEnabled", true);
            connector.setAttribute("sslProtocol", "TLS");
            connector.setAttribute("protocol", "org.apache.coyote.http11.Http11Protocol");
            
            // 双向加密证书配置
            connector.setAttribute("clientAuth", true);
            connector.setAttribute("keystoreFile", sslStorePath + "/bothway/server.p12");
            connector.setAttribute("keystorePass", "678789");
            connector.setAttribute("keystoreType", "PKCS12");
            connector.setAttribute("truststoreFile", sslStorePath + "/bothway/ca.P12");
            connector.setAttribute("truststorePass", "678789");
            connector.setAttribute("truststoreType", "PKCS12");
            
            // TomcatEmbed7 目录
            tomcat.setBaseDir(PROJECT_PATH + File.separator + "build-lib" + File.separator +  "TomcatEmbed7");
            
            StandardServer server = (StandardServer) tomcat.getServer();
            AprLifecycleListener listener = new AprLifecycleListener();
            server.addLifecycleListener(listener);
            
            /**
             * 添加一个根路径应用,这种方式适合把TomcatEmbed在独立项目中运行
             */
            tomcat.addWebapp("", projectPath); 
            
            /**
             * 添加一个根路径应用,这种方式适合把TomcatEmbed文件整合到你的项目中
             */
            // tomcat.addWebapp("", PROJECT_PATH + File.separator +  "WebRoot" ); 
            
            tomcat.start();
            server.await();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (LifecycleException e) {
			e.printStackTrace();
		}
	}
	
}



西夏一品堂
西夏一品堂
connector.setAttribute("SSLEnabled", true); 有这一句就可以了,谢谢
0
西夏一品堂
西夏一品堂

引用来自“zhonghai”的评论

加上这两个试试,不过现在的密钥要用RSA加密,要不然只能在IE上打开,chrome会禁止打开

connector.setScheme("https");

connector.setAttribute("SSLEnabled", true);

谢谢,加这一句就OK了:connector.setAttribute("SSLEnabled", true);
0
螺丝丁
螺丝丁
我用的jdk7+tomcat7      我的项目部署在服务器上      报这个错改哪啊    上边说的不太懂啊
返回顶部
顶部