• SSL 认证之后,request.getScheme()获取不到https的问题记录


    通过浏览器输入https://www.xxx.com,request.getScheme()获取到的确实http而不是https
    通过request.getRequestURL()拿到的也是http://www.xxx.com
    分析原因,是因为用nginx+tomcat部署web服务,tomcat接受到的请求都是来自于nginx的http请求。


    request.getScheme() //总是 http,而不是实际的http或https
    request.isSecure() //总是false(因为总是http)
    request.getRemoteAddr() //总是 nginx 请求的 IP,而不是用户的IP
    request.getRequestURL() //总是 nginx 请求的URL 而不是用户实际请求的 URL
    response.sendRedirect( 相对url ) //总是重定向到 http 上 (因为认为当前是 http 请求)

    解决办法:
      1、在nginx 配置location处加上proxy_set_header X-Forwarded-Scheme $scheme;
        通过request.getHeader("X-Forwarded-Scheme")获取真实的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认证,tomcat不添加,则只需要配置蓝色部分就好
      红色部分,如果tomcat 需要添加ssl认证,则配置红色部分

  • 相关阅读:
    Django如何把数据库里的html格式输出到前端
    如何修改Django中的日期和时间格式 DateTimeField
    python2.7无法安装python-ldap、django-auth-ldap
    windows10下Python如何设置环境变量
    微信小程序在开发者工具页面显示空白且控制台看不到报错信息
    CentOS7 升级 openssh 到 openssh-8.0p1版本
    CentOS系统升级OpenSSH版本
    SSL相关漏洞解决方法
    CentOS 7.4安装 MySQL数据库
    Python3 基础知识
  • 原文地址:https://www.cnblogs.com/yinliang/p/9802768.html
Copyright © 2020-2023  润新知