Browse Source

Updated registration controller

master
Rohan Sircar 4 years ago
parent
commit
90ed86400b
  1. 34
      chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java

34
chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java

@ -28,16 +28,19 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class RegistrationController {
@Autowired
private UserService userService;
private final UserService userService;
@Autowired
private CaptchaService captchaService;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final CaptchaService captchaService;
private final Map<Long, String> captchaMap = new ConcurrentHashMap<>();
@ -46,7 +49,7 @@ public class RegistrationController {
UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
String captchaText = captchaService.getRandomText();
userRegistrationDTO.setCaptchaText(captchaText);
logger.debug("captcha text = {}", captchaText);
log.debug("captcha text = {}", captchaText);
Long captchaID = ThreadLocalRandom.current().nextLong();
userRegistrationDTO.setCaptchaID(captchaID);
captchaMap.put(captchaID, captchaText);
@ -59,28 +62,33 @@ public class RegistrationController {
@ModelAttribute("userRegistrationDTO") @Valid UserRegistrationDTO userRegistrationDTO,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
logger.warn("Registration input has errors!");
log.warn("Registration input has errors!");
return "registration";
}
logger.debug("Captcha text from user input = {}", userRegistrationDTO.getCaptchaInput());
logger.debug("Captcha text from captcha map = {}", captchaMap.get(userRegistrationDTO.getCaptchaID()));
if (userRegistrationDTO.getCaptchaInput().equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) {
logger.info("Registration captcha equal success");
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 {
logger.warn("Registration captcha equal fail");
log.warn("Registration captcha equal fail");
return "redirect:registration?error";
}
}
@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);

Loading…
Cancel
Save