0

I am using Spring Boot MVC in my application. I need to redirect the user to invalid-token.html page, if the token is invalid. When the token is invalid, the url is changes in the browser, but, the invalid-token.html page does not load.

I have configured the security to allow for /invalid-token path and also have the invalid-token.html file inside the resources directory.

Do I also need to have a controller mapping for /invalid-token path inside my controller?

@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");
        }
}

1 Answer 1

1

Your input parameter to RedirectView is incorrect, it should be RedirectView("/invalid-token.html"). If you look at the spring doc, it takes String url as a parameter.

Sign up to request clarification or add additional context in comments.

7 Comments

I have changed to /invalid-token.html. I am now getting a 404.Should I write a GetMapping method inside my controller for this?
Your html file(s) should be located under src/main/webapp folder
You can check my sample github project for code reference. Its a spring boot project, and I have just now added a redirection example under DefaultController for your reference.
Its working now. So, in essense, the files are under resources, it should go through the controller and if the files are in webapp, it will not go through the controller mapping. Is this right.
Its not entirely true, you can also have static resources placed in these directories: src/main/resources/META-INF/resources/index.html src/main/resources/resources/index.html src/main/resources/static/index.html src/main/resources/public/index.html. Spring Boot will automatically add static web resources located from any of the above locations.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.