angular - 验证码会话空
问题描述
下午好,程序员,我需要你的帮助。我有一个在 java 中工作的后端,并根据以下代码生成一个完美工作的验证码图像:
@RestController
public class CaptchaControler {
private static final String FILE_TYPE = "png";
@ResponseStatus(HttpStatus.OK)
@RequestMapping(method=RequestMethod.GET, value="/api/captchaWS", produces = MediaType.APPLICATION_JSON_VALUE)
public void gerarCaptcha(HttpServletRequest request, HttpServletResponse response) {
CaptchaService captchaService = new CaptchaService();
OutputStream outputStream = null;
String captcha = "";
BufferedImage bufferedImage = null;
captcha = captchaService.gerarCaptchaLetra(5);
bufferedImage = captchaService.criarImagem(captcha);
WebUtils.setSessionAttribute(request, "captcha", captcha);
try {
System.out.println(captcha);
response.setContentType("image/jpeg");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Progma", "no-cache");
response.setDateHeader("Max-Age", 0);
outputStream = response.getOutputStream();
ImageIO.write(bufferedImage, FILE_TYPE, outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在 try catch 里面我给了一个System.out.println (captcha); 显示生成的验证码,它工作正常,我也有WebUtils.setSessionAttribute(request, "captcha", captcha); 保存生成的验证码当我使用邮递员 http://localhost:8080/api/captchaWS 测试时显示验证码图像并保存会话中生成的数字 Hhen 我将在邮递员 http://localhost:8080/api 上验证客户端/authenticarWS 并在检查用户名和密码之前拦截过滤器以检查验证码,如下所示
@Component
public class CaptchaFilter extends OncePerRequestFilter {
@Autowired
private MessageSource messageSource;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
Map<String, Object> jsonObject = new HashMap<>();
Gson gson = new Gson();
String captchaDigitado = "";
String captchaGerado = "";
String mensagemErro = "";
if (request.getRequestURI().equals("/api/aberta/autenticacaoWS")) {
captchaGerado = (String) WebUtils.getSessionAttribute(request, "captcha");
captchaDigitado = request.getParameter("captcha");
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Access-control-allow-origin", "*");
System.out.println("------");
System.out.println(captchaGerado);
System.out.println(captchaDigitado);
System.out.println("------");
if (captchaGerado == null || captchaDigitado == null || captchaGerado == "-1") {
mensagemErro = messageSource.getMessage("captcha['invalido']", null, Locale.getDefault());
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
WebUtils.setSessionAttribute(request, "captcha", "-1");
jsonObject.put("mensagem", new ApiErros(mensagemErro, HttpServletResponse.SC_BAD_REQUEST, HttpStatus.BAD_REQUEST.name()));
response.getWriter().write(gson.toJsonTree(jsonObject).toString());
} else if (!captchaGerado.equals(captchaDigitado)) {
mensagemErro = messageSource.getMessage("captcha['invalido']", null, Locale.getDefault());
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
WebUtils.setSessionAttribute(request, "captcha", "-1");
jsonObject.put("mensagem", new ApiErros(mensagemErro, HttpServletResponse.SC_BAD_REQUEST, HttpStatus.BAD_REQUEST.name()));
response.getWriter().write(gson.toJsonTree(jsonObject).toString());
} else {
filterChain.doFilter(request, response);
}
} else {
filterChain.doFilter(request, response);
}
}
}
当在身份验证邮递员中输入信息时,过滤器被拦截,当我要求显示变量 captchaGerado 时,将显示会话中保存的验证码,并在 captchaDigitado 中显示用户键入的内容。邮递员工作完美。
现在,当我通过浏览器调用时,我正在使用 http: localhost: 4200 angular 并且我有一个调用 http://localhost: 8080/api/captchaWS 的服务,图像完美地以 angular 生成。
问题是,当我以角度进行身份验证时,过滤器拦截变量 captchaGenerado 的时间为空,会话 captchaGerado = (String) WebUtils.getSessionAttribute(request, "captcha"); 好像没有保存
我想从同事那里得到一些帮助,告诉我我错了什么非常感谢
解决方案
推荐阅读
- angular - 如何在Angular中将多个数据从子组件传递到父组件?
- android - 无法通过 Kotlin 中的 LayoutInflater 访问视图元素
- python - Django 模型.TextChoices 与模型.IntegerChoices
- python - 阻止用户在在线编译器中运行恶意 python 代码
- algorithm - 可以从 O(n2) 朴素解决方案在 O(n) 中运行的算法的泛化
- kivy - Kivy 和 KivyMD 可以在一个项目中一起使用吗?
- plot - 放大时 Gnuplot 会擦除圆圈
- ios - 相同的可选值有时为零,有时不是
- pandas - 使用 Panda 数据框创建多个 .txt 文件并将其保存为 .csv 文件
- google-apis-explorer - 无法找出原因 youtube 分析的响应始终为零