HttpOnly Cookie not sent

Hello, why isn't my HTTP-only cookie being transmitted with the subsequent request? Disregard code quality as this is solely for testing purposes. Additional details can be found within the chat.
4 Replies
JavaBot
JavaBot9mo ago
This post has been reserved for your question.
Hey @Groldi! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Groldi
GroldiOP9mo ago
@Override
public void addCorsMappings(@NotNull CorsRegistry registry)
{
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedOrigins("http://localhost:4200/")
.allowCredentials(true)
.exposedHeaders(HttpHeaders.SET_COOKIE)
.allowedMethods("GET", "POST", "PUT", "DELETE")
.maxAge(3600);
}
@Override
public void addCorsMappings(@NotNull CorsRegistry registry)
{
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedOrigins("http://localhost:4200/")
.allowCredentials(true)
.exposedHeaders(HttpHeaders.SET_COOKIE)
.allowedMethods("GET", "POST", "PUT", "DELETE")
.maxAge(3600);
}
@PostMapping("/login")
public @NotNull ResponseEntity<@Nullable String> loginUser(@NotNull @RequestBody UserLoginModel loginModel, HttpServletResponse response)
{
return login(loginModel).map(token ->
{
Cookie cookie = new Cookie("jwtToken", token);
cookie.setHttpOnly(true);
cookie.setSecure(false);
cookie.setPath("/");
cookie.setDomain("localhost");
cookie.setMaxAge(loginModel.keepLoggedIn() ? (14 * 24 * 60 * 60) : (24 * 60 * 60)); // 2 Weeks or 1 day
response.addCookie(cookie);
return ResponseEntity.ok(token);
}).orElseThrow(this::unauthorizedThrowable);
}
@PostMapping("/login")
public @NotNull ResponseEntity<@Nullable String> loginUser(@NotNull @RequestBody UserLoginModel loginModel, HttpServletResponse response)
{
return login(loginModel).map(token ->
{
Cookie cookie = new Cookie("jwtToken", token);
cookie.setHttpOnly(true);
cookie.setSecure(false);
cookie.setPath("/");
cookie.setDomain("localhost");
cookie.setMaxAge(loginModel.keepLoggedIn() ? (14 * 24 * 60 * 60) : (24 * 60 * 60)); // 2 Weeks or 1 day
response.addCookie(cookie);
return ResponseEntity.ok(token);
}).orElseThrow(this::unauthorizedThrowable);
}
login(data: LoginRequest)
{
this.loginRequest(data).subscribe({
next: () => {
console.log('Login successful');
this.requestData().subscribe({
next: (data) => {
console.log('Data ', data);
},
error: (error) => {
console.error('Failed to retrieve data: :', error);
}
})
},
error: (error) => {
console.error('Login failed:', error);
}
});
}

private requestData(): Observable<any> {
return this.http.get<any>("http://localhost:8080/user/get", { withCredentials: true });
}

private loginRequest(data: LoginRequest): Observable<void> {
return this.http.post<void>("http://localhost:8080/user/login", data, {responseType: "text" as "json"});
}
login(data: LoginRequest)
{
this.loginRequest(data).subscribe({
next: () => {
console.log('Login successful');
this.requestData().subscribe({
next: (data) => {
console.log('Data ', data);
},
error: (error) => {
console.error('Failed to retrieve data: :', error);
}
})
},
error: (error) => {
console.error('Login failed:', error);
}
});
}

private requestData(): Observable<any> {
return this.http.get<any>("http://localhost:8080/user/get", { withCredentials: true });
}

private loginRequest(data: LoginRequest): Observable<void> {
return this.http.post<void>("http://localhost:8080/user/login", data, {responseType: "text" as "json"});
}
The second request does not include the cookie received from the first request. Does anybody know why?
Groldi
GroldiOP9mo ago
No description
No description
JavaBot
JavaBot9mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?