|
|
@ -40,6 +40,7 @@ public class RegistrationController { |
|
|
|
@Autowired |
|
|
|
private final CaptchaService captchaService; |
|
|
|
|
|
|
|
// FIXME must change this to a timeout base cache otherwise memory leak! |
|
|
|
private final Map<Long, String> captchaMap = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
@GetMapping("/registration") |
|
|
@ -61,32 +62,31 @@ public class RegistrationController { |
|
|
|
BindingResult bindingResult) { |
|
|
|
if (bindingResult.hasErrors()) { |
|
|
|
log.warn("Registration input has errors!"); |
|
|
|
return "registration"; |
|
|
|
return "redirect:registration?error"; |
|
|
|
} |
|
|
|
if (userService.getUser(userRegistrationDTO.getUserName()).isPresent()) { |
|
|
|
return "redirect:registration?error&duplicate=true"; |
|
|
|
} |
|
|
|
log.debug("Captcha text from user input = {}", |
|
|
|
userRegistrationDTO.getCaptchaInput()); |
|
|
|
log.debug("Captcha text from captcha map = {}", |
|
|
|
captchaMap.get(userRegistrationDTO.getCaptchaID())); |
|
|
|
if (userRegistrationDTO.getCaptchaInput() |
|
|
|
.equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) { |
|
|
|
|
|
|
|
log.debug("Captcha text from user input = {}", userRegistrationDTO.getCaptchaInput()); |
|
|
|
log.debug("Captcha text from captcha map = {}", captchaMap.get(userRegistrationDTO.getCaptchaID())); |
|
|
|
if (userRegistrationDTO.getCaptchaInput().equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) { |
|
|
|
log.info("Registration captcha equal success"); |
|
|
|
userService.createUser(userRegistrationDTO); |
|
|
|
return "redirect:registration?success"; |
|
|
|
} else { |
|
|
|
log.warn("Registration captcha equal fail"); |
|
|
|
return "redirect:registration?error"; |
|
|
|
return "redirect:registration?error&captchaError=true"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@GetMapping(value = "/img/captcha/{image_id}", produces = MediaType.IMAGE_PNG_VALUE) |
|
|
|
public ResponseEntity<byte[]> getImage( |
|
|
|
@PathVariable("image_id") Long imageId) throws IOException { |
|
|
|
public ResponseEntity<byte[]> getImage(@PathVariable("image_id") Long imageId) throws IOException { |
|
|
|
|
|
|
|
final String captchaText = captchaMap.get(imageId); |
|
|
|
final HttpHeaders headers = new HttpHeaders(); |
|
|
|
headers.setContentType(MediaType.IMAGE_PNG); |
|
|
|
BufferedImage captchaBufferedImage = captchaService |
|
|
|
.createCaptchaImage(captchaText); |
|
|
|
BufferedImage captchaBufferedImage = captchaService.createCaptchaImage(captchaText); |
|
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
|
|
ImageIO.write(captchaBufferedImage, "png", baos); |
|
|
|