我从事的项目是必须使用SpringBoot生成验证码。 我想要一个“文本到语音”功能,并且声音文件必须使用SpringBoot生成(否则这将是一个巨大的安全问题)。
我生成了一个验证码图像,并将其转换为Base64String并将其发送给客户端。 我使用了SimpleCaptcha。
我的问题是:我无法使用SimpleAudio生成具有相同随机字符串的音频文件。 它总是生成一个新的。 现在,我正在寻找一种解决方案-我只想生成一个与图像中的字符串相同的音频文件。
这是我的代码:
这是验证码图像:
@RestController
public class MyCaptchaController {
private String Captcha_Text;
TextProducer tp;
MyCaptcha cap = new MyCaptcha();
@GetMapping(value = "/getNewCaptcha")
public @ResponseBody MyCaptcha getNewCaptcha() throws IOException
{
Captcha captcha = new Captcha.Builder(200, 70)
.addText()
.addBackground(new FlatColorBackgroundProducer(new Color(231, 235, 240)))
.addNoise()
.addNoise()
.build(); // Required! Always!
System.out.println(captcha.getAnswer());
Captcha_Text = captcha.getAnswer();
String base64Captcha = imgToBase64String(captcha.getImage(), "jpeg");
System.out.println("Bild: " + base64Captcha);
cap.setText(base64Captcha);
cap.setKlartext(Captcha_Text);
return cap;
}
那应该是声音(不起作用):
@RequestMapping(value = "/recfile", method = RequestMethod.GET,
produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public HttpEntity downloadRecipientFile(
ModelMap model, HttpServletResponse response) throws IOException,
ServletException {
AudioCaptcha ac = new AudioCaptcha.Builder()
.addAnswer()
.addNoise()
.build();
CaptchaServletUtil.writeAudio(response, ac.getChallenge());
HttpHeaders header = new HttpHeaders();
header.setContentType(new MediaType("audio", "vnd.wav"));
return new HttpEntity(ac.getChallenge().asByteArray(), header);
}