302重定向引发的血案

646 阅读1分钟

背景

我们项目是单页面,具体页面路由使用的是hash路由 也就是在Java层,我们只有一个根路径 /

// IndexController.java
@RequestMapping(method = RequestMethod.GET, path = "/")
public String index(HttpServeletResponse response){
  ...
  return "index.html"
}

本来,正常增加一个页面,我们只需要写好代码,然后增加路由,最终访问效果是 https:/example.com/#/new-page?term=one

但现在需求变更,要求新路径要写到前面path里,所以访问路径变成了 https:/example.com/new-page?term=one

方案

  1. 尝试使用302重定向
// IndexController.java

...

// 新增接口  伪代码
@RequestMapping(method = RequestMethod.GET, path = "/new-page")
public void newPage(@RequestParam(value = "term") String term,HttpServletRequest request, HttpServeletResponse response){
  try{
     StringBuffer buffer = new StringBuffer();
     buffer.append("/#new-page/").append("?term=").append(term);
     response.sendRedirect(buffer.toString());
  } catch(){
    ...
  }
}

结果:https 莫名变成了 http

image.png

这个,经过了解,是服务端把 S 的证书校验做了,环回地址就变成了 HTTP

  1. 那HTTP可以正常访问吗? 答案:可以。因为服务端又做了一层 301 永久重定向到HTTPS

image.png

  1. 请求hash缺失 我们看到,截图里,请求缺失了 hash 参数,但实际情况,网址Location还是携带上了哦