-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiBearerTokenAuthenticationConverter.java
More file actions
38 lines (30 loc) · 1.35 KB
/
ApiBearerTokenAuthenticationConverter.java
File metadata and controls
38 lines (30 loc) · 1.35 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
33
34
35
36
37
38
package org.openpodcastapi.opa.auth;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.server.resource.web.authentication.BearerTokenAuthenticationConverter;
import org.springframework.security.web.authentication.AuthenticationConverter;
import org.springframework.stereotype.Component;
/// A converter that handles JWT-based auth for API requests.
///
/// This converter targets only the API endpoints at `/api`.
/// Auth for the frontend is handled by Spring's form login.
@Component
public class ApiBearerTokenAuthenticationConverter implements AuthenticationConverter {
private final BearerTokenAuthenticationConverter delegate =
new BearerTokenAuthenticationConverter();
@Override
public Authentication convert(HttpServletRequest request) {
final var path = request.getRequestURI();
// Don't authenticate the auth endpoints
if (path.startsWith("/api/auth/")) {
return null;
}
// If the request has no Bearer token, return null
final var header = request.getHeader("Authorization");
if (header == null || !header.startsWith("Bearer ")) {
return null;
}
// Task Spring Boot with handling the request
return delegate.convert(request);
}
}