I have Angular 7 as frontend and Spring Boot as backend.
Angular app hosted on localhost:4200 and Spring app hosted on localhost:8080
On user login, I'm saving a spring session and a cookie is returned. When the user tries to login again, I first check whether the session exists.
For this, the cookie is required from the client. But I'm not getting any cookie in the client request.
In Chrome dev tools, Application tab, following cookie can be seen:
In Spring Boot's application.properties, I have:
spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=always
server.servlet.session.timeout=60
spring.session.jdbc.table-name=SPRING_SESSION
server.servlet.session.cookie.max-age=2592000
In controller:
@RestController
@CrossOrigin(allowCredentials = "true", origins = {"http://localhost:4200", "http://127.0.0.1:4200"})
@RequestMapping(value = "/users")
public class UsersController {
...
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ResponseEntity<?> login(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for (int i = 0; i < cookies.length; i++) {
String cookieName = cookies[i].getName();
String cookieValue = cookies[i].getValue();
logger.info("UsersController - cookie - {} {}", cookieName, cookieValue);
}
}
HttpSession springsession = request.getSession(false);
if (springsession != null) {
logger.info("UsersController - login - session exists");
...
}
logger.info("UsersController - login - session does not exist");
...
}
I'm not getting any cookie in the client request.
Whereas when I try multiple login requests from the Postman, it works.
So why doesn't it work from browser?
I have tried setting server.servlet.session.cookie.domain property to null, false, 127.0.0.1. But none worked.
How can this be solved?