2019-09-27 06:37:55 +00:00
|
|
|
package org.ros.chatto.controller;
|
|
|
|
|
2019-11-26 06:02:40 +00:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
2019-10-22 12:12:18 +00:00
|
|
|
import javax.validation.Valid;
|
2019-09-27 06:37:55 +00:00
|
|
|
|
2019-10-02 16:25:46 +00:00
|
|
|
import org.ros.chatto.dto.UserRegistrationDTO;
|
2019-11-26 06:02:40 +00:00
|
|
|
import org.ros.chatto.service.CaptchaService;
|
2019-09-27 06:37:55 +00:00
|
|
|
import org.ros.chatto.service.UserService;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2019-11-26 06:02:40 +00:00
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2019-09-27 06:37:55 +00:00
|
|
|
import org.springframework.stereotype.Controller;
|
2019-10-22 12:12:18 +00:00
|
|
|
import org.springframework.ui.Model;
|
|
|
|
import org.springframework.validation.BindingResult;
|
2019-09-27 06:37:55 +00:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
2019-11-26 06:02:40 +00:00
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
2019-09-27 06:37:55 +00:00
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
2020-05-27 11:17:13 +00:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
2019-09-27 06:37:55 +00:00
|
|
|
@Controller
|
2020-05-27 11:17:13 +00:00
|
|
|
@RequiredArgsConstructor
|
|
|
|
@Slf4j
|
2019-10-22 12:12:18 +00:00
|
|
|
public class RegistrationController {
|
|
|
|
|
2019-09-27 06:37:55 +00:00
|
|
|
@Autowired
|
2020-05-27 11:17:13 +00:00
|
|
|
private final UserService userService;
|
2019-10-22 12:12:18 +00:00
|
|
|
|
2019-11-26 06:02:40 +00:00
|
|
|
@Autowired
|
2020-05-27 11:17:13 +00:00
|
|
|
private final CaptchaService captchaService;
|
2019-11-26 06:02:40 +00:00
|
|
|
|
|
|
|
private final Map<Long, String> captchaMap = new ConcurrentHashMap<>();
|
|
|
|
|
2019-09-27 06:37:55 +00:00
|
|
|
@GetMapping("/registration")
|
2019-10-22 12:12:18 +00:00
|
|
|
public String registrationForm(Model model) {
|
2019-11-26 06:02:40 +00:00
|
|
|
UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
|
|
|
|
String captchaText = captchaService.getRandomText();
|
|
|
|
userRegistrationDTO.setCaptchaText(captchaText);
|
2020-05-27 11:17:13 +00:00
|
|
|
log.debug("captcha text = {}", captchaText);
|
2019-11-26 06:02:40 +00:00
|
|
|
Long captchaID = ThreadLocalRandom.current().nextLong();
|
|
|
|
userRegistrationDTO.setCaptchaID(captchaID);
|
|
|
|
captchaMap.put(captchaID, captchaText);
|
|
|
|
model.addAttribute("userRegistrationDTO", userRegistrationDTO);
|
2019-10-22 12:12:18 +00:00
|
|
|
return "registration";
|
2019-09-27 06:37:55 +00:00
|
|
|
}
|
2019-10-22 12:12:18 +00:00
|
|
|
|
2019-09-27 06:37:55 +00:00
|
|
|
@PostMapping("/perform_registration")
|
2019-10-24 06:42:42 +00:00
|
|
|
public String performRegistration(
|
|
|
|
@ModelAttribute("userRegistrationDTO") @Valid UserRegistrationDTO userRegistrationDTO,
|
|
|
|
BindingResult bindingResult) {
|
2019-10-22 12:12:18 +00:00
|
|
|
if (bindingResult.hasErrors()) {
|
2020-05-27 11:17:13 +00:00
|
|
|
log.warn("Registration input has errors!");
|
2019-10-22 12:12:18 +00:00
|
|
|
return "registration";
|
|
|
|
}
|
2020-05-27 11:17:13 +00:00
|
|
|
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");
|
2020-01-31 04:46:13 +00:00
|
|
|
userService.createUser(userRegistrationDTO);
|
2020-01-09 07:24:15 +00:00
|
|
|
return "redirect:registration?success";
|
2019-11-26 06:02:40 +00:00
|
|
|
} else {
|
2020-05-27 11:17:13 +00:00
|
|
|
log.warn("Registration captcha equal fail");
|
2020-01-09 07:24:15 +00:00
|
|
|
return "redirect:registration?error";
|
2019-11-26 06:02:40 +00:00
|
|
|
}
|
2019-09-27 06:37:55 +00:00
|
|
|
}
|
2019-11-26 06:02:40 +00:00
|
|
|
|
2019-11-29 19:13:50 +00:00
|
|
|
@GetMapping(value = "/img/captcha/{image_id}", produces = MediaType.IMAGE_PNG_VALUE)
|
2020-05-27 11:17:13 +00:00
|
|
|
public ResponseEntity<byte[]> getImage(
|
|
|
|
@PathVariable("image_id") Long imageId) throws IOException {
|
2019-11-26 06:02:40 +00:00
|
|
|
|
|
|
|
final String captchaText = captchaMap.get(imageId);
|
|
|
|
final HttpHeaders headers = new HttpHeaders();
|
|
|
|
headers.setContentType(MediaType.IMAGE_PNG);
|
2020-05-27 11:17:13 +00:00
|
|
|
BufferedImage captchaBufferedImage = captchaService
|
|
|
|
.createCaptchaImage(captchaText);
|
2019-11-26 06:02:40 +00:00
|
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
ImageIO.write(captchaBufferedImage, "png", baos);
|
|
|
|
byte[] imageBytes = baos.toByteArray();
|
|
|
|
|
|
|
|
return new ResponseEntity<byte[]>(imageBytes, headers, HttpStatus.OK);
|
|
|
|
}
|
2019-09-27 06:37:55 +00:00
|
|
|
}
|