嗨,我已经保存了通过HttpSession中的servlet生成的验证码,并将其发送到Angular组件。现在,当用户发回他在那看到的验证码时,我无法将其与HttpSession中的验证码进行比较,因为它为null。
控制器
@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 {
...
} }
将会话视为:
公共类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);
}
}
在控制器session.getAttribute(“ captcha”)为空。
向语音通话添加{withCredentials: true}
this.http.get(someUrl, {withCredentials: true})
这将随请求一起发送cookie,这是造成问题的最可能原因