gpt4 book ai didi

java - 我应该为未经身份验证的用户使用 session 吗?

转载 作者:行者123 更新时间:2023-12-02 12:14:28 27 4
gpt4 key购买 nike

我有一个要求,让未经身份验证的用户填写一份包含后端生成的验证码的表单以便提交批准,我正在使用 Spring MVC 和 Cage 生成验证码

@Controller
@Slf4j
public class ContactController {

@RequestMapping("/contact")
public String contact(Map<String, Object> model, HttpSession session, HttpServletRequest request) {
log.info("In contact");
try {
final String captchaCode = RandomStringUtils.randomAlphanumeric(5);
generate(new GCage(), 10, "cg1", ".jpg", captchaCode);
final File file = Paths.get("./cg13.jpg").toFile();
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
String base64String = new String(Base64.encodeBase64(bytes), "UTF-8");;
model.put("image", "data:image/jpeg;base64," + base64String);
model.put("captcha", captchaCode);
session.setAttribute("captcha", captchaCode);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return "contact";
}

@PostMapping("/check")
public String checkCaptcha(final Map<String, Object> model, HttpSession session) {
log.info("in check captcha");
final String captcha = (String) session.getAttribute("captcha");
return "contact";
}

在 Thymeleaf 中我使用的是

<img th:src="@{${image}}" />

<form th:action="@{/check}" th:object="${captcha}" method="post">
<input type="submit" />
</form>

像这样为未经身份验证的用户创建 session 是一种不好的做法吗?那么还能如何处理呢?

最佳答案

没关系,但要小心session fixation 。 Spring 的 session 处理通常相当不错。其默认配置将导致change of session ID当用户最终进行身份验证时。

关于java - 我应该为未经身份验证的用户使用 session 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46290584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com