Search code examples
javaspring-mvcspring-cloudnetflix-zuul

Spring Cloud Zuul: RedirectView redirecting to service and not gateway


I'm a bit new to microservices and Spring. I have Spring Cloud microservices (ports: 8xxx-8xxx) with a Zuul gateway running on port 9000. There's a method inside a controller on a UI service which should do a login and then return to a index.html page:

@RequestMapping(value="/do-login", method = RequestMethod.POST)
    public RedirectView doLogin (@ModelAttribute("authEntity") final AuthEntity authEntity, final Model model) {
        model.addAttribute(VERSION, applicationVersion);
        model.addAttribute("authEntity", new AuthEntity());
        authenticatedStatus = true;
        model.addAttribute(AUTHENTICATED, authenticatedStatus);

        return new RedirectView("index");
    }

The problem is that when above method completes it returns an url of the microservice itself localhost:8888/index but not localhost:9000/services/ui/.

If I use a simpler method:

@RequestMapping(value="/do-login", method = RequestMethod.POST)
public String doLogin (@ModelAttribute("authEntity") final AuthEntity authEntity, final Model model) {
    model.addAttribute(VERSION, applicationVersion);
    model.addAttribute("authEntity", new AuthEntity());
    authenticatedStatus = true;
    model.addAttribute(AUTHENTICATED, authenticatedStatus);

    return "index";
}

This returns correctly an url of gateway localhost:9000/services/ui/do-login but with a /do-login which I do not need.

Maybe I can get rid of /do-login/ part of url? Or maybe there is a solution for the incorrect redirect?

Thanks in advance!


Solution

  • I found that this (thanks to answer in here: Spring redirect url issue when behind Zuul proxy) seems to work as required (but is considered a 'workaround'):

    @RequestMapping(value="/do-login", method = RequestMethod.POST)
        public void doLogin (@ModelAttribute("authEntity") final AuthEntity authEntity,
                             final Model model,
                             HttpServletResponse servletResponse) throws IOException {
            ...
    
            String rUrl = ServletUriComponentsBuilder.fromCurrentContextPath().path("/").build().toUriString();
            servletResponse.sendRedirect(rUrl);
        }