Netflix / zuul

Zuul is a gateway service that provides dynamic routing, monitoring, resiliency, security, and more.
Apache License 2.0
13.53k stars 2.38k forks source link

Client app in Zuul RestTemplate gets login page instead of JSON #411

Open fr33land opened 6 years ago

fr33land commented 6 years ago

I am experimenting with microservices architecture and I have an issue. I have a Zuul gateway and client web application with login form and controllers inside in zuul gateway. Routes to the microservices are defined in Zuul application.yaml config. Microservices also register them selves to eureka server. Users is authenticated against database using jdbcAuthentication. When user enters http://localhost:8080 address in browser zuul displays login form. After successful login user is redirected to home page (index) controller. This controller additionally executes RestTemplate call to the user-info microservice that is under zuul proxy with address http://localhost:8080/user/ defined in config as /user route. Then instead of json response from microservice I am getting same login form html code. If I try to access http://localhost:8080/user/ rest endpoint from browser then I get a nice and clear object with attributes in JSON response. It looks like that RestTemplate losts or doesn't pass authentication or session information. I am adding my config code examples

ZUUL config

zuul:
    routes:
       user:
            path: /user/**
            serviceId: user-service

Zuul client web app controller controller

@Controller
public class IndexController{
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/")
    public String index(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
        Long userId = 2L;
        String user = restTemplate.getForObject("http://localhost:8080/user/find/{id}", String.class, userId); 
        // this is the place where I get html code of login form instead of JSON
        return "index";
    }
}

User microservice rest controller

@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("find/{id}")
    public ResponseEntity<User> findById(@PathVariable long id) {
        return new ResponseEntity<>(userService.findById(id), HttpStatus.OK);
    }
}

I hope that I clearly explained my situation. Thanks for help in advance.

spencergibb commented 6 years ago

Not the spring cloud Netflix issue tracker