Compare commits
3 Commits
3865c26397
...
trafic-log
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dee473fe46 | ||
|
|
5647a5a959 | ||
|
|
c817371a15 |
@@ -25,6 +25,10 @@ public class CustomUserDetails implements UserDetails {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return user.id().toString();
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.codiki.application.traffic;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import org.codiki.domain.traffic.exception.TrafficTraceCreationException;
|
||||
import org.codiki.domain.traffic.model.TrafficEndpoint;
|
||||
import org.codiki.domain.traffic.model.TrafficTrace;
|
||||
import org.codiki.domain.traffic.port.TrafficTracePort;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
import static org.codiki.domain.traffic.model.TrafficTrace.aTrafficTrace;
|
||||
|
||||
@Component
|
||||
public class TrafficTraceUseCases {
|
||||
private final TrafficTracePort trafficTracePort;
|
||||
private final Clock clock;
|
||||
|
||||
public TrafficTraceUseCases(TrafficTracePort trafficTracePort, Clock clock) {
|
||||
this.trafficTracePort = trafficTracePort;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@Async
|
||||
public void saveNewTrace(
|
||||
TrafficEndpoint trafficEndpoint,
|
||||
@Nullable UUID userId,
|
||||
@Nullable String correlationId
|
||||
) {
|
||||
if (isNull(trafficEndpoint)) {
|
||||
throw new TrafficTraceCreationException("Traffic endpoint should not be null.");
|
||||
}
|
||||
|
||||
TrafficTrace newTrace = aTrafficTrace()
|
||||
.withId(UUID.randomUUID())
|
||||
.withDateTime(ZonedDateTime.now(clock))
|
||||
.withEndpoint(trafficEndpoint)
|
||||
.withUserId(userId)
|
||||
.withCorrelationId(correlationId)
|
||||
.build();
|
||||
trafficTracePort.save(newTrace);
|
||||
}
|
||||
}
|
||||
@@ -87,9 +87,7 @@ public class UserUseCases {
|
||||
.map(Authentication::getPrincipal)
|
||||
.filter(CustomUserDetails.class::isInstance)
|
||||
.map(CustomUserDetails.class::cast)
|
||||
.map(CustomUserDetails::getUsername)
|
||||
.map(UUID::fromString)
|
||||
.flatMap(userPort::findById);
|
||||
.map(CustomUserDetails::getUser);
|
||||
}
|
||||
|
||||
private UserAuthenticationData generateAuthenticationData(User user) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.codiki.domain.traffic.exception;
|
||||
|
||||
import org.codiki.domain.exception.FunctionnalException;
|
||||
|
||||
public class TrafficTraceCreationException extends FunctionnalException {
|
||||
public TrafficTraceCreationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.codiki.domain.traffic.model;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public enum HttpMethod {
|
||||
GET, POST, PUT, DELETE;
|
||||
|
||||
public static Optional<HttpMethod> fromString(String methodAsString) {
|
||||
return Arrays.stream(values())
|
||||
.filter(method -> method.name().equals(methodAsString))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.codiki.domain.traffic.model;
|
||||
|
||||
public record TrafficEndpoint(
|
||||
HttpMethod method,
|
||||
String path
|
||||
) {}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.codiki.domain.traffic.model;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
public record TrafficTrace(
|
||||
UUID id,
|
||||
ZonedDateTime dateTime,
|
||||
TrafficEndpoint endpoint,
|
||||
UUID userId,
|
||||
String correlationId
|
||||
) {
|
||||
public static Builder aTrafficTrace() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private UUID id;
|
||||
private ZonedDateTime dateTime;
|
||||
private TrafficEndpoint endpoint;
|
||||
private UUID userId;
|
||||
private String correlationId;
|
||||
|
||||
private Builder() {}
|
||||
|
||||
public Builder withId(UUID id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withDateTime(ZonedDateTime dateTime) {
|
||||
this.dateTime = dateTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withEndpoint(TrafficEndpoint endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withUserId(UUID userId) {
|
||||
this.userId = userId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withCorrelationId(String correlationId) {
|
||||
this.correlationId = correlationId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrafficTrace build() {
|
||||
return new TrafficTrace(id, dateTime, endpoint, userId, correlationId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.codiki.domain.traffic.port;
|
||||
|
||||
import org.codiki.domain.traffic.model.TrafficTrace;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public interface TrafficTracePort {
|
||||
void save(TrafficTrace trace);
|
||||
List<TrafficTrace> getAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate);
|
||||
List<TrafficTrace> getAllByCorrelationId(String correlationId);
|
||||
Integer countAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate);
|
||||
Integer countByCorrelationId(String correlationId);
|
||||
}
|
||||
@@ -25,6 +25,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
@@ -33,28 +37,5 @@
|
||||
<groupId>org.apache.tika</groupId>
|
||||
<artifactId>tika-core</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.postgresql</groupId>-->
|
||||
<!-- <artifactId>postgresql</artifactId>-->
|
||||
<!-- <scope>runtime</scope>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.security</groupId>-->
|
||||
<!-- <artifactId>spring-security-test</artifactId>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- </dependency>-->
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.codiki.exposition.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@Configuration
|
||||
@EnableAspectJAutoProxy
|
||||
@EnableAsync
|
||||
public class TrafficTraceConfiguration {
|
||||
|
||||
}
|
||||
@@ -38,10 +38,6 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||
.filter(authorizationHeader -> !isEmpty(authorizationHeader))
|
||||
.filter(authorizationHeader -> authorizationHeader.startsWith(BEARER_PREFIX))
|
||||
.map(authorizationHeader -> authorizationHeader.substring(BEARER_PREFIX.length()))
|
||||
.filter(token -> {
|
||||
String authorizationHeader = request.getHeader(AUTHORIZATION);
|
||||
return !isEmpty(authorizationHeader) && authorizationHeader.startsWith(BEARER_PREFIX);
|
||||
})
|
||||
.filter(jwtService::isValid)
|
||||
.flatMap(jwtService::extractUser)
|
||||
.map(CustomUserDetails::new)
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.codiki.exposition.traffic;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.codiki.application.traffic.TrafficTraceUseCases;
|
||||
import org.codiki.application.user.UserUseCases;
|
||||
import org.codiki.domain.traffic.model.HttpMethod;
|
||||
import org.codiki.domain.traffic.model.TrafficEndpoint;
|
||||
import org.codiki.domain.user.model.User;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
public class ApiCallsLoggerAspect {
|
||||
private static final String HTTP_HEADER_CORRELATION_ID = "x-correlation-id";
|
||||
|
||||
private final TrafficTraceUseCases trafficTraceUseCases;
|
||||
private final UserUseCases userUseCases;
|
||||
|
||||
public ApiCallsLoggerAspect(
|
||||
TrafficTraceUseCases trafficTraceUseCases,
|
||||
UserUseCases userUseCases
|
||||
) {
|
||||
this.trafficTraceUseCases = trafficTraceUseCases;
|
||||
this.userUseCases = userUseCases;
|
||||
}
|
||||
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.GetMapping)")
|
||||
public void logGetApiCall(JoinPoint joinPoint) {
|
||||
logApiCall();
|
||||
}
|
||||
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.PostMapping)")
|
||||
public void logPostApiCall(JoinPoint joinPoint) {
|
||||
logApiCall();
|
||||
}
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.PutMapping)")
|
||||
public void logPutApiCall(JoinPoint joinPoint) {
|
||||
logApiCall();
|
||||
}
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
|
||||
public void logDeleteApiCall(JoinPoint joinPoint) {
|
||||
logApiCall();
|
||||
}
|
||||
|
||||
private void logApiCall() {
|
||||
getHttpServletRequest().ifPresent(request ->
|
||||
Optional.of(request.getMethod())
|
||||
.flatMap(HttpMethod::fromString)
|
||||
.ifPresent(queryHttpMethod -> {
|
||||
String queryUriPath = request.getRequestURI();
|
||||
TrafficEndpoint endpoint = new TrafficEndpoint(queryHttpMethod, queryUriPath);
|
||||
UUID userId = userUseCases.getAuthenticatedUser()
|
||||
.map(User::id)
|
||||
.orElse(null);
|
||||
String correlationId = request.getHeader(HTTP_HEADER_CORRELATION_ID);
|
||||
trafficTraceUseCases.saveNewTrace(endpoint, userId, correlationId);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private static Optional<HttpServletRequest> getHttpServletRequest() {
|
||||
return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
|
||||
.filter(ServletRequestAttributes.class::isInstance)
|
||||
.map(ServletRequestAttributes.class::cast)
|
||||
.map(ServletRequestAttributes::getRequest);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.codiki.infrastructure.traffic;
|
||||
|
||||
import org.codiki.domain.traffic.model.TrafficTrace;
|
||||
import org.codiki.domain.traffic.port.TrafficTracePort;
|
||||
import org.codiki.infrastructure.traffic.model.TrafficTraceEntity;
|
||||
import org.codiki.infrastructure.traffic.repository.TrafficTraceEntityJpaRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class TrafficTraceJpaAdapter implements TrafficTracePort {
|
||||
private final TrafficTraceEntityJpaRepository repository;
|
||||
|
||||
public TrafficTraceJpaAdapter(TrafficTraceEntityJpaRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(TrafficTrace trace) {
|
||||
TrafficTraceEntity entity = new TrafficTraceEntity(trace);
|
||||
repository.save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TrafficTrace> getAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TrafficTrace> getAllByCorrelationId(String correlationId) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer countAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer countByCorrelationId(String correlationId) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.codiki.infrastructure.traffic.model;
|
||||
|
||||
import org.codiki.domain.traffic.model.HttpMethod;
|
||||
|
||||
public enum HttpMethodEntity {
|
||||
GET, POST, PUT, DELETE;
|
||||
|
||||
public HttpMethod toDomain() {
|
||||
return switch (this) {
|
||||
case GET -> HttpMethod.GET;
|
||||
case POST -> HttpMethod.POST;
|
||||
case PUT -> HttpMethod.PUT;
|
||||
case DELETE -> HttpMethod.DELETE;
|
||||
};
|
||||
}
|
||||
|
||||
public static HttpMethodEntity fromDomain(HttpMethod method) {
|
||||
return switch (method) {
|
||||
case HttpMethod.GET -> GET;
|
||||
case HttpMethod.POST -> POST;
|
||||
case HttpMethod.PUT -> PUT;
|
||||
case HttpMethod.DELETE -> DELETE;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.codiki.infrastructure.traffic.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.codiki.domain.traffic.model.TrafficEndpoint;
|
||||
import org.codiki.domain.traffic.model.TrafficTrace;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.codiki.domain.traffic.model.TrafficTrace.aTrafficTrace;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "traffic")
|
||||
public class TrafficTraceEntity {
|
||||
@Id
|
||||
private UUID id;
|
||||
@Column(nullable = false)
|
||||
private ZonedDateTime dateTime;
|
||||
@Column(nullable = false)
|
||||
@Enumerated
|
||||
private HttpMethodEntity endpointMethod;
|
||||
@Column(nullable = false)
|
||||
private String endpointPath;
|
||||
private UUID userId;
|
||||
private String correlationId;
|
||||
|
||||
public TrafficTraceEntity(TrafficTrace trace) {
|
||||
id = trace.id();
|
||||
dateTime = trace.dateTime();
|
||||
endpointMethod = HttpMethodEntity.fromDomain(trace.endpoint().method());
|
||||
endpointPath = trace.endpoint().path();
|
||||
userId = trace.userId();
|
||||
correlationId = trace.correlationId();
|
||||
}
|
||||
|
||||
public TrafficTrace toDomain() {
|
||||
return aTrafficTrace()
|
||||
.withId(id)
|
||||
.withDateTime(dateTime)
|
||||
.withEndpoint(new TrafficEndpoint(
|
||||
endpointMethod.toDomain(),
|
||||
endpointPath
|
||||
))
|
||||
.withUserId(userId)
|
||||
.withCorrelationId(correlationId)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.codiki.infrastructure.traffic.repository;
|
||||
|
||||
import org.codiki.infrastructure.traffic.model.TrafficTraceEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Repository
|
||||
public interface TrafficTraceEntityJpaRepository extends JpaRepository<TrafficTraceEntity, UUID> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE IF NOT EXISTS traffic (
|
||||
id UUID NOT NULL,
|
||||
date_time TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
endpoint_method SMALLINT NOT NULL,
|
||||
endpoint_path VARCHAR NOT NULL,
|
||||
user_id UUID,
|
||||
correlation_id VARCHAR,
|
||||
CONSTRAINT traffic_pk PRIMARY KEY (id),
|
||||
CONSTRAINT traffic_user_id_fk FOREIGN KEY (user_id) REFERENCES "user" (id)
|
||||
);
|
||||
CREATE INDEX traffic_user_id_idx ON traffic (user_id);
|
||||
@@ -15,11 +15,11 @@
|
||||
<java.version>21</java.version>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
|
||||
<jakarta.servlet-api.version>6.1.0</jakarta.servlet-api.version>
|
||||
<java-jwt.version>4.4.0</java-jwt.version>
|
||||
<postgresql.version>42.7.0</postgresql.version>
|
||||
<tika-core.version>2.9.0</tika-core.version>
|
||||
<commons-lang3.version>3.14.0</commons-lang3.version>
|
||||
<postgresql.version>42.7.4</postgresql.version>
|
||||
<commons-lang3.version>3.17.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
@@ -35,7 +35,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.3.4</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
@@ -84,8 +84,6 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
30
frontend/package-lock.json
generated
30
frontend/package-lock.json
generated
@@ -20,6 +20,7 @@
|
||||
"@angular/router": "^18.2.5",
|
||||
"rxjs": "~7.8.0",
|
||||
"tslib": "^2.3.0",
|
||||
"uuid": "^10.0.0",
|
||||
"zone.js": "~0.14.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -28,6 +29,7 @@
|
||||
"@angular/compiler-cli": "^18.2.5",
|
||||
"@angular/localize": "^18.2.5",
|
||||
"@types/jasmine": "~5.1.0",
|
||||
"@types/uuid": "^10.0.0",
|
||||
"jasmine-core": "~5.1.0",
|
||||
"karma": "~6.4.0",
|
||||
"karma-chrome-launcher": "~3.2.0",
|
||||
@@ -4655,6 +4657,13 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/uuid": {
|
||||
"version": "10.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz",
|
||||
"integrity": "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/wrap-ansi": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/wrap-ansi/-/wrap-ansi-3.0.0.tgz",
|
||||
@@ -12427,6 +12436,16 @@
|
||||
"websocket-driver": "^0.7.4"
|
||||
}
|
||||
},
|
||||
"node_modules/sockjs/node_modules/uuid": {
|
||||
"version": "8.3.2",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
|
||||
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"uuid": "dist/bin/uuid"
|
||||
}
|
||||
},
|
||||
"node_modules/socks": {
|
||||
"version": "2.8.3",
|
||||
"resolved": "https://registry.npmjs.org/socks/-/socks-2.8.3.tgz",
|
||||
@@ -13315,10 +13334,13 @@
|
||||
}
|
||||
},
|
||||
"node_modules/uuid": {
|
||||
"version": "8.3.2",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
|
||||
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
|
||||
"dev": true,
|
||||
"version": "10.0.0",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz",
|
||||
"integrity": "sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==",
|
||||
"funding": [
|
||||
"https://github.com/sponsors/broofa",
|
||||
"https://github.com/sponsors/ctavan"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"uuid": "dist/bin/uuid"
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
"@angular/router": "^18.2.5",
|
||||
"rxjs": "~7.8.0",
|
||||
"tslib": "^2.3.0",
|
||||
"uuid": "^10.0.0",
|
||||
"zone.js": "~0.14.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -37,6 +38,7 @@
|
||||
"@angular/compiler-cli": "^18.2.5",
|
||||
"@angular/localize": "^18.2.5",
|
||||
"@types/jasmine": "~5.1.0",
|
||||
"@types/uuid": "^10.0.0",
|
||||
"jasmine-core": "~5.1.0",
|
||||
"karma": "~6.4.0",
|
||||
"karma-chrome-launcher": "~3.2.0",
|
||||
|
||||
@@ -5,6 +5,7 @@ import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@a
|
||||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||
import { routes } from './app.routes';
|
||||
import { JwtInterceptor } from './core/interceptor/jwt.interceptor';
|
||||
import { CorrelationIdInterceptor } from './core/interceptor/correlation-id.interceptor';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
@@ -18,5 +19,6 @@ export const appConfig: ApplicationConfig = {
|
||||
provideAnimationsAsync(),
|
||||
provideHttpClient(withInterceptorsFromDi()),
|
||||
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
|
||||
{ provide: HTTP_INTERCEPTORS, useClass: CorrelationIdInterceptor, multi: true },
|
||||
]
|
||||
};
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http";
|
||||
import { inject, Injectable } from "@angular/core";
|
||||
import { Observable } from "rxjs";
|
||||
import { CorrelationIdService } from "../service/correlation-id.service";
|
||||
|
||||
@Injectable()
|
||||
export class CorrelationIdInterceptor implements HttpInterceptor {
|
||||
private readonly correlationIdService = inject(CorrelationIdService);
|
||||
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
const correlationId = this.correlationIdService.getCorrelationId();
|
||||
|
||||
const requestWithCorrelationId = request.clone({
|
||||
headers: request.headers.set('x-correlation-id', correlationId)
|
||||
});
|
||||
|
||||
return next.handle(requestWithCorrelationId);
|
||||
}
|
||||
|
||||
}
|
||||
27
frontend/src/app/core/service/correlation-id.service.ts
Normal file
27
frontend/src/app/core/service/correlation-id.service.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import * as uuid from 'uuid';
|
||||
|
||||
const CORRELATION_ID_KEY = 'correlationId';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class CorrelationIdService {
|
||||
getCorrelationId(): string {
|
||||
let correlationId = this.getCorrelationFromLocalStorage();
|
||||
if (correlationId === undefined) {
|
||||
correlationId = this.createNewCorrelationId();
|
||||
}
|
||||
return correlationId;
|
||||
}
|
||||
|
||||
private getCorrelationFromLocalStorage(): string | undefined {
|
||||
return localStorage.getItem(CORRELATION_ID_KEY) ?? undefined;
|
||||
}
|
||||
|
||||
private createNewCorrelationId(): string {
|
||||
const newCorrelationId = uuid.v4();
|
||||
localStorage.setItem(CORRELATION_ID_KEY, newCorrelationId);
|
||||
return newCorrelationId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user