• Tomcat配置SSL证书


    本地配置ssl证书

    为了更好的再服务器上配置ssl证书,先在本上上熟悉流程。本地不需要类似阿里云的证书,借助java的keytool帮助生成离线的证书。

    keytool -genkey -alias ceshi -storetype PKCS12 -keyalg RSA -keystore D:\Java\apache-tomcat-9.0.55-windows-x64\apache-tomcat-9.0.55\conf\https.keystore
    
    • 1

    该命令会在指向的地址位置生成一个名为https.keystore的证书

    在这里插入图片描述

    进入该步骤后需要注意的密钥需要记住,之后还要用的,名字与姓氏要填域名即localhost其他的随便填即可。

    如下在指定的目录下生成了证书:
    在这里插入图片描述
    配置证书加密访问,对于http协议来说,80是默认端口,若项目在其他端口上则需要带上端口号http://localhost:8080,对于https来说,443是默认端口号,若项目在其他端口上则也要带上端口号https://localhost:8443

    接下来配置本地的https协议访问,已经生成了离线的证书,需要配置证书及监听的端口号,在server.xml文件中进行如下配置:

    
    
    
    <Server port="8005" shutdown="SHUTDOWN">
      <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
      
      
      <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
      
      <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
      <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
      <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
    
      
      <GlobalNamingResources>
        
        <Resource name="UserDatabase" auth="Container"
                  type="org.apache.catalina.UserDatabase"
                  description="User database that can be updated and saved"
                  factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                  pathname="conf/tomcat-users.xml" />
      GlobalNamingResources>
    
      
      <Service name="Catalina">
    
        
        
    
    
        
        <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" URIEncoding="UTF-8"/>
    
    
         
         
          
        <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
                  maxThreads="150" schema="https" secure="true" SSLEnabled="true">
          <SSLHostConfig>
            <Certificate
              certificateKeystoreFile="conf/https.keystore" 
              certificateKeystorePassword="Xwh190823" type="RSA"
            />
          SSLHostConfig>
        Connector>
    
    
        
        
        
        
        
        
    
        
        
    
        
    
        
        <Engine name="Catalina" defaultHost="localhost">
    
          
          
    
          
          <Realm className="org.apache.catalina.realm.LockOutRealm">
            
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                   resourceName="UserDatabase"/>
          Realm>
    
          <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
    
            
            
    
            
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t "%r" %s %b" />
    
          Host>
        Engine>
      Service>
    Server>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190

    文件时全部的配置,来看https的部分:

    
         
          
        <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
                  maxThreads="150" schema="https" secure="true" SSLEnabled="true">
          <SSLHostConfig>
            <Certificate
              certificateKeystoreFile="conf/https.keystore" 
              certificateKeystorePassword="123456" type="RSA"
            />
          SSLHostConfig>
        Connector>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    监听的端口为8443,这个certificateKeystoreFile配置项填你生成证书的位置,certificateKeystorePassword这个填之前的密钥。就可以了!!!没错配置https就这么简单,只需要这就行就可以了,启动tomcat服务器即可。

    keytool 错误: java.io.IOException: Invalid keystore format

    在配置的过程中还遇到了一个问题keytool 错误: java.io.IOException: Invalid keystore format,出现这个问题的原因时jdk版本问题,不要使用高本版的jdk,用jdk8就可以了,在构建https.keystore是用的是jdk11导致tomcat一致报上面的错误,将jdk回退到jdk8之后就没问了。

    配置完后进入bin目录开启tomcat服务器,通过http协议访问:http://localhost:8080

    在这里插入图片描述

    再通过https协议访问https://localhost:8443

    在这里插入图片描述
    如上图所示配置成功了,通过两个协议都可以访问。也可以进行其他的配置如修改为默认的端口号,这样就不需要每次添加端口号了,http协议是80,https协议是443。也可以配置跳转,将http协议访问的跳转到https协议上去即在web.xml配置文件中添加:

    <security-constraint> 
        <web-resource-collection > 
            <web-resource-name >SSLweb-resource-name>  
            <url-pattern>/*url-pattern> 
        web-resource-collection> 
        <user-data-constraint> 
            <transport-guarantee>CONFIDENTIALtransport-guarantee> 
        user-data-constraint> 
    security-constraint>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    添加上面内容就会实现实现HTTP自动跳转为HTTPS。甚至在跳转到一个项目上直接跳到网站首页也是可以的。

    服务器上SSL证书配置

    前面已经配置了本地即离线的SSL证书,那么该如何配置服务器上的SSL证书呢?道理和步骤和本地几乎是一样的。唯一不同的时外网的需要提供外网的SSL证书,可以在购买云服务器的网站免费下载证书,也可以通过第三方下载。不限于,华为云,阿里云,腾讯云等。

    接下来以阿里云,云服务器内核为CentOS7为例,WEB服务器为Tomcat下载并配置SSL证书,来开放HTTPS协议访问。

    SSL证书下载

    下载好证书后是一个压缩包类型:

    在这里插入图片描述
    解压得到两个文件,一个是证书文件pfx类型,一个是密码txt类型:

    在这里插入图片描述

    和之前配置本地的方式一样主要就是配置https的证书,将解压的证书上传到服务的可见文件夹,记录该文件地址,配置server.xml文件:

    
    
    
    <Server port="8005" shutdown="SHUTDOWN">
      <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
      
      
      <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
      
      <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
      <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
      <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
    
      
      <GlobalNamingResources>
        
        <Resource name="UserDatabase" auth="Container"
                  type="org.apache.catalina.UserDatabase"
                  description="User database that can be updated and saved"
                  factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                  pathname="conf/tomcat-users.xml" />
      GlobalNamingResources>
    
      
      <Service name="Catalina">
    
        
        
    
    
        
        <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" URIEncoding="UTF-8"/>
    
    
         
         
          
        
    
        <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/cert/6340408_www.qiyuanrenli.com.pfx" 
                         certificateKeystoreType="PKCS12"
                         certificateKeystorePassword="zXOfVn8u" />
        SSLHostConfig>
    Connector>
    
    
        
        
        
        
        
        
    
        
        
    
        
    
        
        <Engine name="Catalina" defaultHost="localhost">
    
          
          
    
          
          <Realm className="org.apache.catalina.realm.LockOutRealm">
            
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                   resourceName="UserDatabase"/>
          Realm>
    
          <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
    
            
            
    
            
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t "%r" %s %b" />
    
          Host>
        Engine>
      Service>
    Server>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199

    同样的核心配置就这几行:

    
    
    
        <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/cert/6340408.pfx" 
                         certificateKeystoreType="PKCS12"
                         certificateKeystorePassword="-----" />
        SSLHostConfig>
    Connector>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    certificateKeystoreFile是上传的证书所在的位置,certificateKeystoreType是证书的类型,不是所有证书都是fpx类型,也要适当更改例如还有RSA类型。certificateKeystorePassword是证书密钥,打开下载的哪个txt文档就可以看见密钥。

    配置完成后重启Tomcat服务器即可。成功的HTTP和HTTPS都可以访问。

    在这里插入图片描述

    在这里插入图片描述

    如果HTTPS配置了还能访问的话分析错误的原因:

    1. 该网站为提供安全连接

    如果浏览器出现该情况的大概率就是证书配置有问题,检查一下是否有配置错误的问题。

    1. 无法访问此网页

    浏览器出现该提示,首先检查域名或ip是否正确,然后是端口号,http协议的tomcat默认是8080,https协议在配置时监听的端口时8443。

    3.服务器未响应

    浏览器出现这个提示时,可能时服务上的服务未启动、端口被占用、或者是防火墙未开放端口,需要去云服务器的安全组开放端口。

    最后要注意的是端口问题,如果安装了防火墙工具的话要开启用到的端口,云服务器去安全组开放端口,安装了宝塔的也要开启端口。

  • 相关阅读:
    力扣:34,在排序数组这种查找元素的第一个和最后一个位置
    WPF路由事件
    VM501开发套件开发版单振弦式传感器采集模块岩土工程监测
    C++day3
    Redis
    模态框确认按钮的保存
    计算机网络期末复习——简明扼要介绍考点及相关知识
    web:[ACTF2020 新生赛]Exec
    Cpp浅析系列-STL之priority_queue
    简单爬虫案例——爬取快手视频
  • 原文地址:https://blog.csdn.net/xwh3165037789/article/details/126074845