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)
请问,这是什么问题,怎么解决
加上这两个试试,不过现在的密钥要用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();}}}加上这两个试试,不过现在的密钥要用RSA加密,要不然只能在IE上打开,chrome会禁止打开
connector.setScheme("https");
connector.setAttribute("SSLEnabled",true);
我用的jdk7+tomcat7 我的项目部署在服务器上 报这个错改哪啊 上边说的不太懂啊版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。