一、最近做個項目是需要在WildFly中啟用https,但是由於WildFly的中文文檔比較少所以google了一下,先是通過JBOSS的官方文檔了解了一下,但是官方文檔這塊的配置介紹有些不全面。所以,又通過其他網站找了一些相關資料,終於對其配置有所了解。
二、配置過程
1、生成密鑰和密鑰庫:keytool -genkey -alias envkey -keyalg RSA -keystore env.keystore -validity 365,這里的執行路徑一般在配置文件的目錄下。
此命令的第一個問題就是 your first and last name,這里應該輸入的你服務的域名,如果本地可以輸入localhost,其他可以不填,最后一步輸入 y 即可。
2、找到Wildfly的配置文件路徑 wildfly-9.0.2s\standalone\configuration\standalone.xml,在應用域加入相關 keystore 的信息,如下
<security-realm name="ApplicationRealm"> <!--使得應用支持https--> <server-identities> <ssl> <keystore path="env.keystore" relative-to="jboss.server.config.dir" keystore-password="123456" alias="envkey" key-password="123456"/> </ssl> </server-identities> <authentication> <local default-user="$local" allowed-users="*" skip-group-loading="true"/> <properties path="application-users.properties" relative-to="jboss.server.config.dir"/> </authentication> <authorization> <properties path="application-roles.properties" relative-to="jboss.server.config.dir"/> </authorization> </security-realm>
3、在 undertow subsystem 下添加 http-listener 的監聽,<https-listener name="httpsServer" socket-binding="https" security-realm="ApplicationRealm"/>
配置如下:
<subsystem xmlns="urn:jboss:domain:undertow:2.0"> <buffer-cache name="default"/> <server name="default-server"> <http-listener name="default" socket-binding="http" redirect-socket="https"/> <https-listener name="httpsServer" socket-binding="https" security-realm="ApplicationRealm"/> <host name="default-host" alias="localhost"> <location name="/" handler="welcome-content"/> <filter-ref name="server-header"/> <filter-ref name="x-powered-by-header"/> </host> </server> <servlet-container name="default"> <jsp-config/> <websockets/> </servlet-container> <handlers> <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/> </handlers> <filters> <response-header name="server-header" header-name="Server" header-value="WildFly/9"/> <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/> </filters> </subsystem>
4、https的具體端口可以在如下位置配置,默認是8443:<socket-binding name="https" port="${jboss.https.port:8443}"/>
5、此時即可通過https訪問系統,此時在瀏覽器的地址欄中即可看到一把鎖的圖標,但是在圖標上面會有一個X號,那是因為該服務器證書沒有在CA進行認證。此時通過http還可以訪問服務地址,如果想只允許通過https來訪問,可以做如下配置,這樣每次的http訪問都會直接轉到https,找到自己應用的web.xml,在web-app標簽中,添加如下配置:
<security-constraint> <web-resource-collection> <web-resource-name>WEB_APPLICATION_NAME</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
其中 WEB_APPLICATION_NAME 為你的應用名稱,一般就是war的包名。
6、如果需要訪問jboss管理頁面也需要https,這里就不做詳細介紹,和應用的配置差不多,可以參考一下網址:http://www.mastertheboss.com/jboss-server/jboss-security/securing-access-to-jboss-wildfly-management-console
三、參考網址
Wildfly官網說明文檔:https://docs.jboss.org/author/display/WFLY9/Admin+Guide#AdminGuide-EnableSSL
介紹比較全面的網址:http://stackoverflow.com/questions/32008182/wildfly-9-http-to-https