I will introduce the flow of the mail authentication function implemented when creating the web service. All you need to do is register the DB and check for duplicates. However, at first it took time to get an image of the implementation method, so I would like to introduce mainly the implementation flow.
Overall flow:
Pass user information to the server side in any way you like, either Ajax or Post.
We will process in the following flow.
/ validate / id = UUID
.To identify that the user clicked on the URL It is necessary to add the information associated with the user saved in the temporary table to the URL. If you can batting or predict, you can authenticate other temporary users, so Use UUID.
RegisterUserController
boolean isMember = memberRepository.existsByUsername(user);
if(!isMember){
String vali = UuidUtil.generateUUID();
BCryptPasswordEncoder passEncoder = new BCryptPasswordEncoder();
try {
TmpMember tmpMember = new TmpMember(user, passEncoder.encode(pass), displyname, vali);
tmpMemberRepository.saveAndFlush(tmpMember);
} catch (Exception e) {
e.printStackTrace();
//status = "Error: DB save failure";
return status;
}
String IPadnPort = myIP.getYourIP();
String from = "Sender's email address";
String title = "Request for Tobidemo account confirmation";
String content = displyname + "Mr." + "\n" + "\n" + "Please follow the link below to authenticate your account" + "\n"
+"http://" + IPadnPort
+ "/validate"+ "?id=" + vali ;
try {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(from);
msg.setTo(user);
msg.setSubject(title);//Title setting
msg.setText(content); //Body settings
mailSender.send(msg);
} catch (Exception e) {
e.printStackTrace();
//status = "Error: Email sending failure";
return status;
}
status = "ok";
}
return status; //ng
}
;
return status; //ng
}
The following email will be sent to the user.
If the user clicks on the URL to access it, they will receive a UUID with id =. Check if the received UUID is stored in a temporary table. If confirmed, in the table that stores the authenticated user information, Re-register. You are then redirected to the service login page.
ValidateUserController.java
@CrossOrigin
@RequestMapping(value = "/validate", method = RequestMethod.GET)
public String validate(RedirectAttributes redirectAttributes,ModelAndView mav, @RequestParam("id") String id) throws Exception {
String isRegisterd = "false";
boolean isExist = tmpMemberRepository.existsByValidation(id);
//System.out.println(isExist);
if (isExist) {
try {
TmpMember tmp = tmpMemberRepository.findByValidation(id);
String username = tmp.getUsername();
String displyname = tmp.getDisplyname();
String password = tmp.getPassword();
Member member = new Member();
member.setDisplyname(displyname);
member.setPassword(password);
member.setUsername(username);
memberRepository.saveAndFlush(member);
isRegisterd = "true";
} catch (Exception e) {
//TODO auto-generated catch block
e.printStackTrace();
isRegisterd = "false";
}
}
redirectAttributes.addFlashAttribute("isRegisterd", isRegisterd);
return "redirect:/edit/begin";
}
It's a minimal feature, but it was surprisingly easy to implement email authentication. Since it is my own email authentication logic, there may be something strange, but since it is personal development, it is important to work!
Recommended Posts