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);
        }
    

      

  • 相关阅读:
    SQL注入攻击
    新手指引,php什么是常量、变量、数组、类和对象及方法?
    JQuery坑,说说哪些大家都踩过的坑
    利用Jsonp实现跨域请求,spring MVC+JQuery
    【实用】需要收藏备用的JQuery代码片段
    【动画】JQuery实现冒泡排序算法动画演示
    【基础】26个命令玩转linux,菜鸟及面试必备
    【收藏】8段JQuery处理表单的代码片段,很实用
    【实用】Html5实现文件异步上传
    【基础】新手任务,五分钟全面掌握JQuery选择器
  • 原文地址:https://www.cnblogs.com/barrysgy/p/14833386.html
Copyright © 2011-2022 走看看