跨域訪問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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM