Updated registration controller
This commit is contained in:
parent
3fdb464b55
commit
90ed86400b
@ -28,16 +28,19 @@ import org.springframework.web.bind.annotation.ModelAttribute;
|
|||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class RegistrationController {
|
public class RegistrationController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CaptchaService captchaService;
|
private final CaptchaService captchaService;
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
||||||
|
|
||||||
private final Map<Long, String> captchaMap = new ConcurrentHashMap<>();
|
private final Map<Long, String> captchaMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@ -46,7 +49,7 @@ public class RegistrationController {
|
|||||||
UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
|
UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
|
||||||
String captchaText = captchaService.getRandomText();
|
String captchaText = captchaService.getRandomText();
|
||||||
userRegistrationDTO.setCaptchaText(captchaText);
|
userRegistrationDTO.setCaptchaText(captchaText);
|
||||||
logger.debug("captcha text = {}", captchaText);
|
log.debug("captcha text = {}", captchaText);
|
||||||
Long captchaID = ThreadLocalRandom.current().nextLong();
|
Long captchaID = ThreadLocalRandom.current().nextLong();
|
||||||
userRegistrationDTO.setCaptchaID(captchaID);
|
userRegistrationDTO.setCaptchaID(captchaID);
|
||||||
captchaMap.put(captchaID, captchaText);
|
captchaMap.put(captchaID, captchaText);
|
||||||
@ -59,28 +62,33 @@ public class RegistrationController {
|
|||||||
@ModelAttribute("userRegistrationDTO") @Valid UserRegistrationDTO userRegistrationDTO,
|
@ModelAttribute("userRegistrationDTO") @Valid UserRegistrationDTO userRegistrationDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
if (bindingResult.hasErrors()) {
|
if (bindingResult.hasErrors()) {
|
||||||
logger.warn("Registration input has errors!");
|
log.warn("Registration input has errors!");
|
||||||
return "registration";
|
return "registration";
|
||||||
}
|
}
|
||||||
logger.debug("Captcha text from user input = {}", userRegistrationDTO.getCaptchaInput());
|
log.debug("Captcha text from user input = {}",
|
||||||
logger.debug("Captcha text from captcha map = {}", captchaMap.get(userRegistrationDTO.getCaptchaID()));
|
userRegistrationDTO.getCaptchaInput());
|
||||||
if (userRegistrationDTO.getCaptchaInput().equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) {
|
log.debug("Captcha text from captcha map = {}",
|
||||||
logger.info("Registration captcha equal success");
|
captchaMap.get(userRegistrationDTO.getCaptchaID()));
|
||||||
|
if (userRegistrationDTO.getCaptchaInput()
|
||||||
|
.equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) {
|
||||||
|
log.info("Registration captcha equal success");
|
||||||
userService.createUser(userRegistrationDTO);
|
userService.createUser(userRegistrationDTO);
|
||||||
return "redirect:registration?success";
|
return "redirect:registration?success";
|
||||||
} else {
|
} else {
|
||||||
logger.warn("Registration captcha equal fail");
|
log.warn("Registration captcha equal fail");
|
||||||
return "redirect:registration?error";
|
return "redirect:registration?error";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/img/captcha/{image_id}", produces = MediaType.IMAGE_PNG_VALUE)
|
@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 String captchaText = captchaMap.get(imageId);
|
||||||
final HttpHeaders headers = new HttpHeaders();
|
final HttpHeaders headers = new HttpHeaders();
|
||||||
headers.setContentType(MediaType.IMAGE_PNG);
|
headers.setContentType(MediaType.IMAGE_PNG);
|
||||||
BufferedImage captchaBufferedImage = captchaService.createCaptchaImage(captchaText);
|
BufferedImage captchaBufferedImage = captchaService
|
||||||
|
.createCaptchaImage(captchaText);
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
ImageIO.write(captchaBufferedImage, "png", baos);
|
ImageIO.write(captchaBufferedImage, "png", baos);
|
||||||
|
Loading…
Reference in New Issue
Block a user