I am using Angular as the frontend and when the user tries to login and then we will fire a post request to Server(Java+REST with Jersey), which will respond with cookies.
Server Code:
@Path("/SampleApi/readCookies")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response readCookiesAlongWithJsonData(@Context HttpHeaders headers, String inputJsonObj)
{
Map<String, Cookie> cookies = headers.getCookies();
String maptoJSON = null;
JsonObject jsonObject = new JsonParser().parse(inputJsonObj).getAsJsonObject();
String encodedJWT = JWTEncodeDecode.createJWT(userId.toString(), maptoJSON, tenant, 10000);
NewCookie jwtCookie = null;
if(cookies != null && cookies.size() > 0 && cookies.get("JWT_Token") != null && cookies.get("JWT_Token").getValue() != null)
jwtCookie = new NewCookie("JWT_Token", cookies.get("JWT_Token").getValue());
else
jwtCookie = new NewCookie("JWT_Token", encodedJWT);
return Response.ok().entity("Valid").cookie(jwtCookie).build();
}
when I try to use this method using postman, I can read and write cookies, but in Angular through API, call the cookies are not available. I can't create cookies in Angular as there needs to be some authentication process that happens at the server which will generate cookies.
Angular Code
this.http.post("http://localhost:9090/restfuljersey/rest/service//SampleApi/readCookies",
formData, {observe: 'response'})
.subscribe( response =>
{
console.log(response);
}, errors =>
{
console.error(errors);
}
);