zoukankan      html  css  js  c++  java
  • HttpServletRequest的getRequestURL方法获取不到https协议请求问题

    近来公司官网的域名协议提升为https后,原先在SpringMVC中使用拦截器HandlerInterceptor拦截请求,在拦截器中使用HttpServletRequest获取拦截到的请求路径(不包含请求参数的路径)对老官网的资讯url重定向到新官网的url。

    代码如下:

    public class RedirectInterceptor implements HandlerInterceptor {
    
        private static final Map<String, String> map = new HashMap<String, String>();
    
       
    static { map.put("http://www.xxxxxx.com/mtbd_1817.html","http://www.xxxxxx.com/ynjsx/3382.html"); map.put("http://www.xxxxxx.com/mtbd_28.html","http://www.xxxxxx.com/jkdt/4626.html"); } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) { String requestURL = request.getRequestURL().toString(); String redirectURL = map.get(requestURL); if (Strings.isNotBlank(redirectURL)) { response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader("Location", redirectURL); } return true; } }

    因为现在官网的请求协议升级为https了,所以我将静态代码块中的http协议都替换成https了。

    但是出现的问题是,原先官网为http协议是正常的重定向,更换为https后,请求的路径https协议的,程序获取到的一直是http协议的,造成后面的逻辑出现错误。

    原因分析:

    由于项目使用到了Nginx代理,整个项目是使用Nginx+Tomcat部署的,因此Tomcat端收到的请求都是从Nginx转发过来的,因此产生这个问题的原因就是Nginx的配置问题了

    解决方法:

    需要在nginx里面配置好对应ssl以及把证书放到对应位置
    例如:

    listen 443;
     server_name xxx.xxxx.com;
     ssl on;
     root html;
     index index.html index.htm;
     ssl_certificate cert/123456xxxxxx.pem;
     ssl_certificate_key cert/123456xxxxxx.key;
     ssl_session_timeout 5m;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     ssl_prefer_server_ciphers on;


    配置完成后修改tomcat,/conf/server.xml

    找到70行,默认位置修改为:

    <Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    scheme="https"
    redirectPort="8443" proxyPort="443" />

    重启tomcat即可

  • 相关阅读:
    iot 表索引dump《2》
    heap表和iot表排序规则不同
    Cannot complete the install because one or more required items could not be found.
    iot表输出按主键列排序,heap表不是
    iot 表主键存放所有数据,且按数据插入顺序排序
    iot表和heap表排序规则不同
    org.eclipse.graphiti.ui.editor.DiagramEditorInput.
    Oracle 排序规则
    perl 异步超时 打印错误
    14.6.3 Grouping DML Operations with Transactions 组DML操作
  • 原文地址:https://www.cnblogs.com/qukun/p/12341723.html
Copyright © 2011-2022 走看看