更改https后,request.getScheme()只獲取到http

瀏覽器https://xxx.xxx.xxx,request.getScheme()獲取到的地址是http而不是https

試了request.getRequestURL()拿到的也是http
因?yàn)槲疫@里用的是nginx+tomcat轉(zhuǎn)發(fā)https,tomcat接受到的請(qǐng)求都是來自于nginx的http請(qǐng)求。
request.getScheme() //http,而不是實(shí)際的https
request.isSecure() //false(因?yàn)槭莌ttp)
request.getRemoteAddr() //nginx請(qǐng)求的 IP,而不是用戶的IP
request.getRequestURL() //nginx請(qǐng)求的URL 而不是用戶請(qǐng)求的 URL
response.sendRedirect( 相對(duì)url ) //重定向到 http 上 (因?yàn)檎J(rèn)為當(dāng)前是 http 請(qǐng)求)

解決辦法
1. 在nginx 配置location處加上proxy_set_header X-Forwarded-Scheme $scheme; 通過request.getHeader("X-Forwarded-Scheme")獲取真實(shí)的scheme
2. 在Tomcat server.xml中添加:
<Engine name="Catalina" defaultHost="localhost">
  這行之后

    <Valve className="org.apache.catalina.valves.RemoteIpValve"  remoteIpHeader="X-Forwarded-For" 
 protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>  

3.如果jsp中大量使用 request.getScheme() 獲取,避免更改代碼
    則需要配置 tomcat:

<Connector port="443" maxHttpHeaderSize="8192"
                 maxThreads="150"
                 enableLookups="false" disableUploadTimeout="true"
                 acceptCount="100" scheme="https" secure="true"
                 SSLEnabled="true"
                 SSLCertificateFile="${catalina.base}/conf/localhost.crt"
                 SSLCertificateKeyFile="${catalina.base}/conf/localhost.key" />

如果nginx添加了ssl認(rèn)證,tomcat不添加,只配藍(lán)色部分
  紅色部分,如果tomcat 要添加ssl認(rèn)證,配紅色部分

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容