Embedded Tomcat
首先到tomcat抓取embedded版本
檔案下載,解壓縮後直接import進java project即可
HTTPS :
使用java keytool 產生憑證
keytool 參數
-alias 別名
-keystore 憑證名稱及儲存位置
-keyalg 憑證的algorithm
-validity 憑證有效期
-storepass 憑證密碼
-keypass 別名密碼
-dname 憑證壅有者資料 CN=名字與姓氏,OU=組織單位名稱,O=組織名稱,L=城市或區域名稱,ST=州或省份名稱,C=單位的兩字母國家代碼
keytool -genkey -alias name -keyalg RSA -storetype JKS -validity 3650 -keypass password -keystore ./test.jks -storepass password -dname "CN=, OU=, O=, L=Hsinchu, C=TW";
單獨執行的話:
import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
public class TomcatServer{
public static void main(String[] args) throws Exception {
//appBase是放置web app的地方
String appBase = "D:\\webContent";
//Tomcat的執行位置會建立相關目錄
String catalinaBase = System.getProperty("user.dir") + File.separatorChar + "Embedded" + File.separatorChar;
//bind port在8081
Integer port = Integer.valueOf(8081);
Tomcat tomcat = new Tomcat();
tomcat.setPort(port);
tomcat.setBaseDir(appBase);
tomcat.getHost().setAppBase(catalinaBase);
//rcp mode add
//init catalina load server.xml
Catalina catalina = new Catalina();
catalina.load();
StandardServer server = (StandardServer)tomcat.getServer();
//rcp mode add
server.setCatalina(catalina);
//add https connector
Connector httpsConnector = new Connector();
httpsConnector.setPort(8443);
httpsConnector.setSecure(true);
httpsConnector.setScheme("https");
httpsConnector.setAttribute("keystorePass", "password");
httpsConnector.setAttribute("keystoreFile", 憑證位置);
httpsConnector.setAttribute("clientAuth", "false");
httpsConnector.setAttribute("sslProtocol", "TLS");
httpsConnector.setAttribute("maxThreads", "200");
httpsConnector.setAttribute("SSLEnabled", true);
// 避免firefox 顯示 ssl_error_weak_server_ephemeral_dh_key
httpsConnector.setProperty("ciphers","TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA");
tomcat.getService().addConnector(httpsConnector)
加入webapp的方法
1.webapp是folder的狀態
String contextPath = "/axis2";
Context ctx = tomcat.addWebapp(contextPath, appBase);
//webapp的web.xml絕對路徑
File contextFile = new File(appBase +"\\WEB-INF\\web.xml");
ctx.setConfigFile(contextFile.toURI().toURL());
2.webapp是war檔
//tomcat啟動會解壓縮war檔
File webApp = new File(appBase, "axis2.war");
StandardHost stdHost = (StandardHost) tomcat.getHost();
stdHost.setUnpackWARs(true);
stdHost.setAutoDeploy(true);
stdHost.setDeployOnStartup(true);
tomcat.setHost(stdHost);
String appPath = webApp.getAbsolutePath();
tomcat.addWebapp(tomcat.getHost(), "/axis2" ,
appPath);
//啟動tomcat
tomcat.setHostname("localhost");
tomcat.start();
//持續執行
tomcat.getServer().await();
}
檔案下載,解壓縮後直接import進java project即可
HTTPS :
使用java keytool 產生憑證
keytool 參數
-alias 別名
-keystore 憑證名稱及儲存位置
-keyalg 憑證的algorithm
-validity 憑證有效期
-storepass 憑證密碼
-keypass 別名密碼
-dname 憑證壅有者資料 CN=名字與姓氏,OU=組織單位名稱,O=組織名稱,L=城市或區域名稱,ST=州或省份名稱,C=單位的兩字母國家代碼
單獨執行的話:
import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
public class TomcatServer{
public static void main(String[] args) throws Exception {
//appBase是放置web app的地方
String appBase = "D:\\webContent";
//Tomcat的執行位置會建立相關目錄
String catalinaBase = System.getProperty("user.dir") + File.separatorChar + "Embedded" + File.separatorChar;
//bind port在8081
Integer port = Integer.valueOf(8081);
Tomcat tomcat = new Tomcat();
tomcat.setPort(port);
tomcat.setBaseDir(appBase);
tomcat.getHost().setAppBase(catalinaBase);
//rcp mode add
//init catalina load server.xml
Catalina catalina = new Catalina();
catalina.load();
StandardServer server = (StandardServer)tomcat.getServer();
//rcp mode add
server.setCatalina(catalina);
//add https connector
Connector httpsConnector = new Connector();
httpsConnector.setPort(8443);
httpsConnector.setSecure(true);
httpsConnector.setScheme("https");
httpsConnector.setAttribute("keystorePass", "password");
httpsConnector.setAttribute("keystoreFile", 憑證位置);
httpsConnector.setAttribute("clientAuth", "false");
httpsConnector.setAttribute("sslProtocol", "TLS");
httpsConnector.setAttribute("maxThreads", "200");
httpsConnector.setAttribute("SSLEnabled", true);
// 避免firefox 顯示 ssl_error_weak_server_ephemeral_dh_key
httpsConnector.setProperty("ciphers","TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA");
tomcat.getService().addConnector(httpsConnector)
加入webapp的方法
1.webapp是folder的狀態
String contextPath = "/axis2";
Context ctx = tomcat.addWebapp(contextPath, appBase);
//webapp的web.xml絕對路徑
File contextFile = new File(appBase +"\\WEB-INF\\web.xml");
ctx.setConfigFile(contextFile.toURI().toURL());
2.webapp是war檔
//tomcat啟動會解壓縮war檔
File webApp = new File(appBase, "axis2.war");
StandardHost stdHost = (StandardHost) tomcat.getHost();
stdHost.setUnpackWARs(true);
stdHost.setAutoDeploy(true);
stdHost.setDeployOnStartup(true);
tomcat.setHost(stdHost);
String appPath = webApp.getAbsolutePath();
tomcat.addWebapp(tomcat.getHost(), "/axis2" ,
appPath);
//啟動tomcat
tomcat.setHostname("localhost");
tomcat.start();
//持續執行
tomcat.getServer().await();
}
留言
張貼留言