悉野小楼

导航

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

  

posted on 2021-05-31 21:27  悉野  阅读(730)  评论(0编辑  收藏  举报