WildFly 9.0.2 启用 SSL

一、最近做个项目是需要在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

posted @ 2016-08-16 11:12  随风如梦  阅读(2457)  评论(0编辑  收藏  举报