【问题标题】:spring boot MVC is not loading the redirected pagespring boot MVC 未加载重定向页面
【发布时间】:2019-04-22 05:36:44
【问题描述】:

我在我的应用程序中使用 Spring Boot MVC。如果令牌无效,我需要将用户重定向到 invalid-token.html 页面。令牌无效时,浏览器中的url会发生变化,但是invalid-token.html页面不会加载。

我已将security 配置为允许/invalid-token 路径,并且资源目录中还有invalid-token.html 文件。

我是否还需要在我的控制器中为/invalid-token 路径提供控制器映射?

@GetMapping("/confirm")
    public RedirectView confirmUser(RedirectAttributes attributes, @RequestParam("token") String token){
        // Find the user associated with the reset token
        Optional<User> optionalUser= userService.findByConfirmationToken(token);

        if (!optionalUser.isPresent()) {
            //redirect user to reset password page.
            attributes.addFlashAttribute("invalidToken", "Invalid Token. Please enter your email address.");
            return new RedirectView("redirect:invalid-token");
        }
}

【问题讨论】:

    标签: spring spring-boot spring-mvc


    【解决方案1】:

    RedirectView 的输入参数不正确,应该是RedirectView("/invalid-token.html")。如果你看spring doc,它以String url为参数。

    【讨论】:

    • 我已更改为/invalid-token.html。我现在得到一个 404。我应该为此在我的控制器中编写一个 GetMapping 方法吗?
    • 您的 html 文件应位于 src/main/webapp 文件夹下
    • 您可以查看我的示例github project 以获取代码参考。它是一个spring boot项目,我刚刚在DefaultController下添加了一个重定向示例供您参考。
    • 它现在可以工作了。因此,本质上,文件在资源下,它应该通过控制器,如果文件在 webapp 中,则不会通过控制器映射。是这样吗?
    • 不完全正确,您也可以将静态资源放在这些目录中:src/main/resources/META-INF/resources/index.htmlsrc/main/resources/resources/index.htmlsrc/main/resources/static/index.htmlsrc/main/resources/public/index.html。 Spring Boot 将自动添加位于上述任何位置的静态 Web 资源。
    猜你喜欢
    • 1970-01-01
    • 2013-03-06
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多