zoukankan      html  css  js  c++  java
  • slb,ngix转发https后 springboot返回跳转为http

    网上一般五种方法

    1.配置nginx, 2.改为war发布修改tomcat配置 3.使用shiro 4.在启动类型里面修改EmbeddedServletContainerFactory(spring boot低配版本的才行) 5.在control里面的使用RedirectView跳转

    见下面的参考链接

    参考:

    https://www.cnblogs.com/linyufeng/p/12636322.html

    https://blog.csdn.net/qq_35510622/article/details/79400477?utm_source=blogxgwz6
    https://blog.csdn.net/blomule/article/details/105269149/

    我这儿的问题:运维加了证书, 使用https请求域名解析, slb转发http请求到我的springboot.

    使用的阿里的slb转发, 配置不了nginx, 不想用war发布(要改一些配置, 测试不方便, 还要架个tomcat服务器), shiro看了下也麻烦, spring boot版本是最新的已经找不到TomcatEmbeddedServletContainerFactory

    我的解决方法:

    在配置表中加个配置responseHttps(boolen类型), 表示网站返给浏览器是http还是https.

    用myRedirect替换response.sendRedirect代码.

    用myRedirectView替换FORWARD_URL_PREFIX. 在一些control里面, 返回字符串表示模板名html的函数里面,  但使用了FORWARD_URL_PREFIX的, 如:return FORWARD_URL_PREFIX + "mypage";  control的返回类型改为Object, 可以返回string, 也可以返回RedirectView, RedirectView可以配置返给客户端为https

    /**
         * 替换跳转为https
         * @param response
         * @param relativeUrl
         */
        public static void myRedirect(HttpServletResponse response, String relativeUrl) {
            if(responseHttps) {
                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                //http://长度为7
                int endIndex = request.getRequestURL().indexOf("/", 8);
                String url = request.getRequestURL().substring(0, endIndex);
                try {
                    response.sendRedirect(url.replace("http://", "https://") + relativeUrl);
                }catch (Exception ex) {
                }
            } else {
                try {
                    response.sendRedirect(relativeUrl);
                }catch (Exception ex) {
                }
            }
        }
    
        /**
         * 替换前端跳转FORWARD_URL_PREFIX, 因为跳转时给前端的url不是https的
         * @param relativeUrl
         * @return
         */
        public static RedirectView myRedirectView(String relativeUrl) {
            return new RedirectView(relativeUrl,true,false);
        }
    

      

  • 相关阅读:
    偷窃转基因玉米种子引发中美打农业官司
    关于PreferenceActivity的使用和一些问题的解决(自己定义Title和取值)
    大写中文数字-財务
    【leetcode】LRU
    【AC大牛陈鸿的ACM总结贴】【ID AekdyCoin】人家当初也一样是菜鸟
    android面试题 不单单为了面试也是一次非常好的学习
    存储系统的实现-探析存储的机制和原理
    unity3d脚本编程
    ubuntu12.04 安装配置jdk1.7
    Android中一个类实现的接口数不能超过七个
  • 原文地址:https://www.cnblogs.com/barrysgy/p/14833386.html
Copyright © 2011-2022 走看看