最近开发浏览器人脸采集功能,需要使用H5调用摄像头,发现最新版的谷歌和火狐浏览器安全机制不允许通过http协议打开摄像头,所以自己使用jdk自带的工具申请了证书配置到tomcat,然后访问https协议访问tomcat发布的服务
1.使用jdk自带的keytool生成证书keystore
1.1打开cmd窗口后 cd到jdk目录bin目录
1.2输入 keytool -genkey -alias test01 -keyalg RSA -keystore F:\test01.keystore
1.3 按照提示一步步操作就行,第一步输入证书口令
1.4 到了这里确认是否正确,正确输入y,否则请输入n
输入y后输入秘钥口令,我都是设置和秘钥库一样的口令
1.5完成后查看指定的keystore目录已经生成了test01.keystore
2.把证书配置到tomcat
2.1 打开tomcat的server.xml文件
找到配置tomcat http端口所在的位置
在下面增加以下代码段
<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true" URIEncoding="utf-8"
maxThreads="1500" scheme="https" secure="true" useBodyEncodingForURI="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="D:\xxxx\test01.keystore" keystorePass="123456" />
其中配置http端口的地方 redirectPort需要修改为和https所配置的端口,这样当http协议访问时才能自动重定向到https协议的端口
443为https协议的默认端口,这样https访问时可以不输入端口号访问
2.2 配置tomcat web.xml 文件 结合http重定向端口完成 http访问自动跳转到https
打开web.xml拉倒最底部 该位置下增加代码,配置tomcat的认证类型,配置所有请求都经过SSL证书认证
<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
3.启动tomcat 使用https协议访问发布的服务
https://ip/项目名
由于证书自己申请,未经机构认证,浏览器会提示危险,点击高级继续访问即可
使用http协议访问http://ip:8085/项目名 发现会自动跳转到https的地址
来源:CSDN
作者:菜鸟盟
链接:https://blog.csdn.net/qq445829096/article/details/102874638