https下sendRedirect或redirect重定向到http加443端口导致错误的解决办法

在给网站增加https之后,发现了一个重定向的问题。

网站架构:nginx做代理,应用端使用docker容器承载一个个不同的后端服务,在nginx层安装ssl证书。

问题:在https下访问网站 https://www.3kkg.com/admin 如果这时候没有登陆,则会重定向到登录页面即 https://www.3kkg.com/login 但问题是实际情况跳转到了http加端口的地址即 http://www.3kkg.com:443/admin 这里使用的是http协议,且又指定了443端口,故而直接报错。

解决办法:

一:直接使用forward的形式跳转
url地址没有改变,不太建议使用

二:重定向代码修改成如下形式,即使用302跳转

httpServletResponse.setStatus(302);
httpServletResponse.setHeader("location", httpServletRequest.getContextPath()+"/login")

三:使用spring mvc实现的重定向redirect:/login
结果仍然会失败,这个时候需要修改mvc的配置把viewResolver的redirectHttp10Compatible值改为false

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/" />
  <property name="suffix" value=".jsp" />
  <property name="redirectHttp10Compatible" value="false" />
</bean>

但是我使用的是springboot+jfinal的enjoy作为视图模板。在yml文件中找不到这个配置,于是只能从代码端解决。以下是enjoy的代码段配置(有删减)

@Configuration
public class EnjoyConfig {

    @Bean
    public JFinalViewResolver jFinalViewResolver() {
        JFinalViewResolver jfr = new JFinalViewResolver();
        jfr.setSessionInView(true);
        jfr.setRedirectHttp10Compatible(false);
        return jfr;
    }

}

参考资料:
https://blog.csdn.net/zhuye1992/article/details/80496151
https://blog.csdn.net/successli1994/article/details/82658176


觉得内容还不错?打赏个钢镚鼓励鼓励!!👍