我一直在使用Spring Boot和FLutter。我正在使用password-reset
功能。我希望当用户请求重置密码时,会向用户的电子邮件地址发送一封电子邮件,在单击link
后,后端服务将验证令牌,并重定向到用户可以在其中输入新密码的flutter应用程序页面。
一切都很顺利,但我无法重定向到flutter网页。
Controller.java
@RequestMapping(value = "/forgot-password", method = RequestMethod.POST)
public String forgotUserPassword(@RequestParam("emailId") String emailId) {
User existingUser = userRepository.findByEmailIdIgnoreCase(emailId);
if (existingUser != null) {
ConfirmationToken confirmationToken = new ConfirmationToken(existingUser);
confirmationTokenRepository.save(confirmationToken);
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(existingUser.getEmailId());
mailMessage.setSubject("Complete Password Reset!");
mailMessage.setFrom("abc@gmail.com");
mailMessage.setText("Dear " + existingUser.getFirstName().toUpperCase() + " " + existingUser.getLastName().toUpperCase() + ",nn" + "You have requested to reset password. To complete the password reset process, please click here: "
+ "http://localhost:8082/confirm-reset?token=" + confirmationToken.getConfirmationToken()");
emailSenderService.sendEmail(mailMessage);
return "Request to reset password received. Check your inbox for the reset link.";
} else {
return "This email does not exist!";
}
}
String emailByPasswordRestToken = null;
@RequestMapping(value = "/confirm-reset", method = {RequestMethod.GET, RequestMethod.POST})
public String validateResetToken(@RequestParam("token") String confirmationToken) {
ConfirmationToken token = confirmationTokenRepository.findByConfirmationToken(confirmationToken);
if (token != null) {
User user = userRepository.findByEmailIdIgnoreCase(token.getUser().getEmailId());
user.setEnabled(true);
userRepository.save(user);
emailByPasswordRestToken = user.getEmailId();
return "valid token";
//here it should return and redirect to the activity of flutter app automatically to enter the `New Password`.
} else {
return "This link is broken or expired.";
}
}
请问我该怎么做。
有两种方法可以从控制器进行重定向。
1.使用HttpServletResponse#sendRedirect
方法
为此,您必须更改validateResetToken
函数的签名,使其成为void
,并添加HttpServletResponse
参数:
@RequestMapping(value = "/confirm-reset", method = { RequestMethod.GET, RequestMethod.POST })
public void validateResetToken(@RequestParam("token") String confirmationToken,
HttpServletResponse response) {
// ... your logic ...
response.sendRedirect("http://your-flutter-url/path");
}
2.只需将"redirect:"
添加到您的响应字符串
@RequestMapping(value = "/confirm-reset", method = { RequestMethod.GET, RequestMethod.POST })
public String validateResetToken(@RequestParam("token") String confirmationToken) {
// ... your logic ...
return "redirect:http://your-flutter-url/path"
}
注意:不太确定当请求为POST
时重定向是否有效。因此,我还将RequestMapping
更改为仅接受GET
请求。您可以将其更改为:
@GetMapping("/confirm-reset")
public String validateResetToken(@RequestParam("token") String confirmationToken)