【问题标题】:How to redirect to flutter app from spring boot如何从 Spring Boot 重定向到 Flutter 应用程序
【发布时间】:2020-03-10 14:15:36
【问题描述】:

我一直在使用 Spring Boot 和 FLutter。我正在使用password-reset 功能。我希望当用户请求重置密码时,会向用户的电子邮件地址发送一封电子邮件,点击 link 后,后端服务将验证 token 并重定向到颤振应用页面,其中用户可以输入新密码

一切顺利,但我无法重定向到颤振网页。

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() + ",\n\n" + "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.";
        }

    }

请问我该怎么做。

【问题讨论】:

    标签: spring spring-boot flutter spring-security flutter-web


    【解决方案1】:

    您有两种从控制器进行重定向的方法。

    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)
    

    【讨论】:

    • 大佬如何在app中设置Flutter的路径?主要问题是应用程序如何知道自动打开页面,
    • 恐怕你做不到。您必须知道 Flutter 应用(或您使用的任何其他前端应用)的 URL。
    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2015-08-17
    • 2015-02-05
    • 2020-03-06
    • 1970-01-01
    • 2017-11-25
    • 2021-03-13
    相关资源
    最近更新 更多