1、测试环境:win10,tomcat7.0.72,jdk8
2、实现目标:实现静态文件http访问,api的https访问(本测试只是通过目录来区别的,在spring中,可以是访问路径以api开头的url)
3、配置:
3.1、开放tomcat两个端口,一个供http使用的7080,和一个供https使用的7433端口
tomcat中的server.xml配置如下:
<Connector port="7080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="7433"/>
<!--这里的redirectPort="7433"后面会用到-->
<Connector executor="tomcatThreadPool" port="7433" protocol="org.apache.coyote.http11.Http11Protocol"
scheme="https" secure="true" SSLEnabled="true" clientAuth="false" sslProtocol="TLS"
connectionTimeout="20000" keystoreFile="/cert/tomcat7080.keystore" keystorePass="tomcat7080" />
<!--protocol="org.apache.coyote.http11.Http11Protocol" 是这样的,否则可能会报Connector attribute SSLCertificateFile must be defined when using SSL with APR的问题,无法启动https-->
3.2、创建测试工程
工程结构
test(工程文件夹)
-------api(目录)
--------api.html
--------two(二级目录)
--------api2.html
--------static(目录)
--------1.jpg
--------s2(二级目录)
--------2.jpg
--------WEB-INF(目录)
--------web.xml(主要是控制http跳转到https)
如果没有http跳转到https的控制到上面就可以了,这是就可以通过http://localhost:7080/static/1.jpg或 https://localhost:7443/api/api.html
如果在访问api时要求http转为https,需要在web.xml中设置如下:
<security-constraint>
<web-resource-collection>
<web-resource-name>SSL</web-resource-name>
<url-pattern>/api/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
上面使用路径将http强制转为https,在tomcat的7080端口配置中要增加redirectPort="7433",否则会跳转到没有端口的路径中,无法找到对应页面(https://localhost/api/api.html)
4、结束语
这样的功能当然也可以通过nigix,apache做跳转处理
最近在使用appcan时,https的图片无法显示,最终确定使用http显示图片,遂找了些资料来解决这个问题,可能还有其他的解决方法,请大神们赐教。