简体   繁体   中英

How to consume Login Service from UI Service in Spring Boot?

I have 2 microservices, 1 is having all UI and 1 is having Login Solution connected with MySQL Database.

I have tried Login and UI working in same services.

@RequestMapping("/accounts/login") //login 
public String login(HttpServletRequest request) {
    request.setAttribute("mode", "MODE_LOGIN");
    return "login";
}

@RequestMapping ("/login-user") // checking user details in database
public String loginUser(@ModelAttribute User user, HttpServletRequest request) {
    //ModelAndView model = new ModelAndView();
    if(userService.findByEmailAndPassword(user.getEmail(), user.getPassword())!=null) {
        return user.getFirstname();

    }
    else {
        request.setAttribute("error", "Invalid username and password!");
        request.setAttribute("mode", "MODE_LOGIN");
        return "login";
    }
}

This is my code for Login, and in JSP i have put like

 <c:choose>
    <c:when test="${mode=='MODE_HOME' }"> //some code after this
  <c:when test="${mode=='MODE_LOGIN' }">    
   <form action="/login-user" method="POST"> //form after this

This above one is done in 1 service only, but now i want UI should be separated and that UI should consume this Login Solution from there.

My Understanding is: I have created same POJO in UI Microservice and Controller i am trying to consume login service in below given way

@RequestMapping("validate")
private String login1()
{
    final String uri = "http://localhost:8091/accounts/login";

    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class);

    return result;
}

I am confused how will i call then @RequestMapping("/login-user") where the actual validation is happening.

I am to new to this architecture. Any other way out here.

You should call @RequestMapping("/login-user") only from your html page and internally from that with rest templet you can call another microservices and get the validation error message and than redirect accordingly or show the response message.

This should be written in the login-user mappring side so you can get the error there . restTemplate = new RestTemplate(); String result = restTemplate.getForObject(uri, String.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM