跨域訪問sessionid不一致問題

在開發過程中遇到這麼一個問題,讓我花了一個下午的大好時光才解決。但是解決玩之後,發現那麼的容易。就是查找資料的時候很費勁。這裏把問題記錄一下。

問題的產生

  • 流程是這樣的,要做一個用戶登錄的接口。在登錄頁面,前端先請求驗證碼,然後輸入用戶名密碼和驗證碼之後,請求登錄接口。
  • 這裏存在兩個接口,驗證碼接口和登錄接口。在驗證碼接口中我用session保存驗證碼,在登錄接口中我從session取出驗證碼進行校驗。
    兩個接口的代碼如下:
@RequestMapping("/getImageCode")
    public void getImageCode(HttpServletRequest request,
                             HttpServletResponse response) throws IOException {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control",
                "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");

        String capText = captchaProducer.createText();
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,capText);
        logger.info("code is "+capText+" session id is "+request.getSession().getId());
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ModelMap login(HttpServletRequest request){
        ModelMapHelper helper = new ModelMapHelper();
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String imgCode = request.getParameter("imageCode");
        String sessionCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        logger.info("input code is "+imgCode+" session id is "+request.getSession().getId());
        if(StringUtils.isEmpty(imgCode)){
            helper.setErrorMap("驗證碼不能爲空");
            return helper;
        }
        if(!imgCode.equals(sessionCode)){
            helper.setErrorMap("驗證碼不正確");
            return helper;
        }
        try {
            User user = userService.checkLogin(userName, password);
            if (user == null) {
                helper.setErrorMap("用戶名或密碼錯誤");
                return helper;
            }
            helper.setSuccessMap("登錄成功");
            helper.setData(user);
            request.getSession().setAttribute("user",user);
        }catch (GeneralException g){
            g.printStackTrace();
            logger.warn(g.getMessage(),g);
            helper.setErrorMap(g.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("查詢失敗",e);
            helper.setInternalErrorMap();
        }
        return helper;
    }

*經過postman工具簡單的接口測試之後,沒有問題。但是與前端進行接口聯調的時候發現了問題。
兩次獲取的sessionid不一致,導致在登錄時候,沒有獲取session中的驗證碼!
問題

查找原因

百思不得其解!爲什麼用postman測試是正常的呢?而與前端聯調就有這種問題。
原來後臺是做了一個跨域訪問的設置

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    // 設置跨域訪問
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE")
                .allowCredentials(true);
    }
}

主要解釋如下:
registry.allowedOrigins(““)設置跨域訪問的域名,如果是,默認都可以訪問。
這個方法是後來找到問題後,自己加上去的
registry.allowCredentials(true)設置是否允許客戶端發送cookie信息。默認是false
具體關於這些頭信息的解釋可以參考:
http://www.ruanyifeng.com/blog/2016/04/cors.html

解決問題

其實最後就做了兩件事情,
1. 服務端設置可以接收cookie信息

registry.allowCredentials(true)
  1. 在ajax請求中設置發送cookie信息
 $.ajax({
   url: a_cross_domain_url,
   xhrFields: {
      withCredentials: true
   }
});

再看看結果,sessionid就一致了。
這裏寫圖片描述
參考的博客:
https://segmentfault.com/q/1010000002905817

發佈了206 篇原創文章 · 獲贊 82 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章