瀏覽器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)證,配紅色部分