当从角度发出请求时,HttpSession 不返回检索值



嗨,我已经保存了通过 HttpSession 中的 servlet 生成的验证码代码,并将其发送到 Angular 组件。现在,当用户发回他在那里看到的验证码时,我无法将其与 HttpSession 中的验证码进行比较,因为它显示为空。

控制器

@PostMapping(value = "/register")
public GenericResponse registerNewUserAccount(@RequestBody UserVO user,
Model model, HttpServletRequest request) throws Exception {
if (user.getCaptchaResponse() != null ) {
if (user.getCaptchaResponse().equals(request.getAttribute("captcha"))) {
...
} else {
...
}
}
else {
...
} }

参见会话为:

public class CaptchServlet 扩展 HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String captchaStr = "";
captchaStr = CaptchUtility.generateCaptchaTextMethod2(6);
try {
HttpSession session = request.getSession();
session.setAttribute("captcha", captchaStr);
OutputStream outputStream = response.getOutputStream();
ImageIO.write(cpimg, FILE_TYPE, outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

}

in controller session.getAttribute("captcha"( 为 null。

{withCredentials: true}添加到角度调用

this.http.get(someUrl, {withCredentials: true})

这将在请求中发送cookie,这是您出现问题的最可能原因,即未发送cookie

相关内容

  • 没有找到相关文章

最新更新