-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtAuthenticationEntryPoint.java
More file actions
32 lines (25 loc) · 1.3 KB
/
JwtAuthenticationEntryPoint.java
File metadata and controls
32 lines (25 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package org.openpodcastapi.opa.auth;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
@RequiredArgsConstructor
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
private final ObjectMapper objectMapper;
/// Returns a 401 when a request is made without a valid bearer token
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
// If the request is being made without a valid bearer token, return a 401.
response.setStatus(HttpStatus.UNAUTHORIZED.value());
// Set content type to JSON
response.setContentType("application/json");
AuthDTO.ErrorMessageDTO message = new AuthDTO.ErrorMessageDTO("Access denied", "You need to log in to access this resource");
response.getWriter().write(objectMapper.writeValueAsString(message));
}
}