开发者社区> 问答> 正文

Embed Tomcat SSL报错:Invalid character fou?报错

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)

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




展开
收起
爱吃鱼的程序员 2020-06-09 10:30:15 761 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

    connector.setScheme("https");

    connector.setAttribute("SSLEnabled",true);

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

    packagelittle.ant.platform.run;importjava.io.File;importjavax.servlet.ServletException;importorg.apache.catalina.LifecycleException;importorg.apache.catalina.connector.Connector;importorg.apache.catalina.core.AprLifecycleListener;importorg.apache.catalina.core.StandardServer;importorg.apache.catalina.startup.Tomcat;/***内嵌式tomcat*@author董华健dongcb678@163.com*/publicclassTomcatEmbed7{privatestaticStringPROJECT_PATH=System.getProperty("user.dir");/***项目路径,标准的javaweb项目结构*/privatestaticfinalStringprojectPath="D:/DevelopmentTool/eclipse-4.5-jee/git/JfinalUIB/JFinalUIBV2/WebContent";/***ssl配置需要的证书路径*/privatestaticfinalStringsslStorePath="D:/DevelopmentTool/eclipse-4.5-jee/workspace/TomcatEmbed7/build-lib/ssl";publicstaticvoidmain(String[]args){//普通http//http();//启用ssl,单向证书加密//httpsSingle();//启用ssl,双向证书加密httpsDouble();}/***普通http,端口8080*访问路径:http://127.0.0.1:8080*/publicstaticvoidhttp(){try{Tomcattomcat=newTomcat();tomcat.setPort(8080);//TomcatEmbed7目录tomcat.setBaseDir(PROJECT_PATH+File.separator+"build-lib"+File.separator+"TomcatEmbed7");StandardServerserver=(StandardServer)tomcat.getServer();AprLifecycleListenerlistener=newAprLifecycleListener();server.addLifecycleListener(listener);/***添加一个根路径应用,这种方式适合把TomcatEmbed在独立项目中运行*/tomcat.addWebapp("",projectPath);/***添加一个根路径应用,这种方式适合把TomcatEmbed文件整合到你的项目中*///tomcat.addWebapp("",PROJECT_PATH+File.separator+"WebRoot");tomcat.start();server.await();}catch(ServletExceptione){e.printStackTrace();}catch(LifecycleExceptione){e.printStackTrace();}}/***https单向加密,端口8443*访问路径:https://127.0.0.1:8443*/publicstaticvoidhttpsSingle(){try{Tomcattomcat=newTomcat();//设置Connector属性Connectorconnector=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");StandardServerserver=(StandardServer)tomcat.getServer();AprLifecycleListenerlistener=newAprLifecycleListener();server.addLifecycleListener(listener);/***添加一个根路径应用,这种方式适合把TomcatEmbed在独立项目中运行*/tomcat.addWebapp("",projectPath);/***添加一个根路径应用,这种方式适合把TomcatEmbed文件整合到你的项目中*///tomcat.addWebapp("",PROJECT_PATH+File.separator+"WebRoot");tomcat.start();server.await();}catch(ServletExceptione){e.printStackTrace();}catch(LifecycleExceptione){e.printStackTrace();}}/***https双向加密,端口8443*访问路径:https://127.0.0.1:8443*/publicstaticvoidhttpsDouble(){try{Tomcattomcat=newTomcat();//设置Connector属性Connectorconnector=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");StandardServerserver=(StandardServer)tomcat.getServer();AprLifecycleListenerlistener=newAprLifecycleListener();server.addLifecycleListener(listener);/***添加一个根路径应用,这种方式适合把TomcatEmbed在独立项目中运行*/tomcat.addWebapp("",projectPath);/***添加一个根路径应用,这种方式适合把TomcatEmbed文件整合到你的项目中*///tomcat.addWebapp("",PROJECT_PATH+File.separator+"WebRoot");tomcat.start();server.await();}catch(ServletExceptione){e.printStackTrace();}catch(LifecycleExceptione){e.printStackTrace();}}}



    connector.setAttribute("SSLEnabled",true);有这一句就可以了,谢谢

    引用来自“zhonghai”的评论

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

    connector.setScheme("https");

    connector.setAttribute("SSLEnabled",true);

    我用的jdk7+tomcat7   我的项目部署在服务器上   报这个错改哪啊  上边说的不太懂啊
    2020-06-09 10:30:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Apache Tomcat 的云原生演进 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载