0

So I am working with Angular, Java servlet and postgresql trying to build a little login system. I would like to use cookie to store the user name and user password.

        String username = request.getParameter("name");
        String password = request.getParameter("password");

        if(LoginInfoDAO.verifyuser(username, password)){
        String.valueOf(UserDAO.getUserByUsername(username).getUser_id()));
            Cookie c1 = new Cookie("userid",String.valueOf(UserDAO.getUserByUsername(username).getUser_id()));
            c1.setMaxAge(20);
            c1.setPath("/");
            c1.setDomain("localhost");
            response.addCookie(c1);
            PrintWriter out = response.getWriter();
            out.print(UserService.getUserJson(username));
        }
        else {
            response.setStatus(404);
        }

I was able to add the cookie and retrieve the stored data on the other servlet on the windows postman app but not in chrome nor the postman chrome extension.

    Cookie c[] = request.getCookies();
    int userid = Integer.parseInt(c[1].getValue());
    out.print(om.writeValueAsString(ReimbService.getTicketsByUserid(userid)));

please help, this is so weird

davedabull
  • 13
  • 5

1 Answers1

0

I finally solved the issue. Thank you for this post: https://stackoverflow.com/a/46412839/8735121

To solve the issue (sending cookies across domains, you need to add

response.addHeader("Access-Control-Allow-Credentials", "true");

to your servlet, and add

{withCredentials: true}

to your front end call, like this:

 this.httpClient.post<User>(url,params,{withCredentials: true})
    .subscribe((val)=>{//do something with the response}
davedabull
  • 13
  • 5