Skip to content

Commit cc3ca3a

Browse files
committed
activity-log-service lib eklendi. llm-service activity loglama eklendi.
1 parent f73ea11 commit cc3ca3a

17 files changed

Lines changed: 632 additions & 261 deletions

File tree

activity-log-service/src/main/java/com/craftpilot/activitylogservice/config/LightSecurityConfig.java

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
import lombok.extern.slf4j.Slf4j;
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
6-
import org.springframework.http.HttpStatus;
6+
import org.springframework.core.annotation.Order;
77
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
8-
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
98
import org.springframework.security.config.web.server.ServerHttpSecurity;
109
import org.springframework.security.web.server.SecurityWebFilterChain;
11-
import org.springframework.web.server.ServerWebExchange;
10+
import org.springframework.http.server.reactive.ServerHttpRequest;
1211
import org.springframework.web.server.WebFilter;
13-
import org.springframework.web.server.WebFilterChain;
1412
import reactor.core.publisher.Mono;
15-
import org.springframework.security.web.server.header.ReferrerPolicyServerHttpHeadersWriter.ReferrerPolicy;
1613

1714
import java.util.Arrays;
1815
import java.util.List;
@@ -23,87 +20,65 @@
2320
public class LightSecurityConfig {
2421

2522
private static final List<String> PUBLIC_PATHS = Arrays.asList(
26-
"/actuator/",
27-
"/v3/api-docs",
28-
"/swagger-ui",
29-
"/webjars/"
30-
);
31-
32-
private static final List<RequiredHeader> REQUIRED_HEADERS = Arrays.asList(
33-
new RequiredHeader("X-User-Id", "User ID is required"),
34-
new RequiredHeader("X-User-Role", "User role is required"),
35-
new RequiredHeader("X-User-Email", "User email is required")
23+
"/actuator",
24+
"/actuator/health",
25+
"/actuator/info",
26+
"/health",
27+
"/info",
28+
"/v3/api-docs",
29+
"/swagger-ui",
30+
"/webjars/"
3631
);
3732

3833
@Bean
34+
@Order(1)
3935
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
4036
return http
41-
.csrf(ServerHttpSecurity.CsrfSpec::disable)
42-
.cors(cors -> cors.disable())
43-
.httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
44-
.formLogin(ServerHttpSecurity.FormLoginSpec::disable)
45-
.authorizeExchange(exchanges -> exchanges
46-
.pathMatchers(getPublicPaths()).permitAll()
47-
.pathMatchers("/admin/**").hasRole("ADMIN")
48-
.anyExchange().authenticated()
49-
)
50-
.addFilterAt(headerValidationFilter(), SecurityWebFiltersOrder.AUTHENTICATION)
51-
.exceptionHandling(handling -> handling
52-
.authenticationEntryPoint((exchange, ex) -> {
53-
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
54-
return exchange.getResponse().setComplete();
55-
})
56-
)
57-
.headers(headers -> headers
58-
.contentSecurityPolicy(csp -> csp.policyDirectives("default-src 'self'"))
59-
.frameOptions(frame -> frame.mode(org.springframework.security.web.server.header.XFrameOptionsServerHttpHeadersWriter.Mode.DENY))
60-
.referrerPolicy(referrer -> referrer.policy(ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN))
61-
.xssProtection(xss -> xss.disable())
62-
)
63-
.build();
37+
.csrf(ServerHttpSecurity.CsrfSpec::disable)
38+
.formLogin(ServerHttpSecurity.FormLoginSpec::disable)
39+
.httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
40+
.anonymous(anonymous -> anonymous.authorities("ROLE_ANONYMOUS"))
41+
.authorizeExchange(exchanges -> exchanges
42+
.pathMatchers("/**").permitAll() // Tüm isteklere izin ver, header kontrolünü WebFilter ile yap
43+
)
44+
.build();
6445
}
6546

6647
@Bean
67-
public WebFilter headerValidationFilter() {
48+
@Order(0) // En önce çalışacak
49+
public WebFilter loggingHeadersFilter() {
6850
return (exchange, chain) -> {
69-
if (isPublicPath(exchange.getRequest().getPath().value())) {
51+
ServerHttpRequest request = exchange.getRequest();
52+
String path = request.getPath().value();
53+
54+
log.debug("İstek geldi: {} {}", request.getMethod(), path);
55+
56+
if (isPublicPath(path)) {
57+
log.debug("Public path erişimi: {} - kontrolsüz geçiyor", path);
7058
return chain.filter(exchange);
7159
}
72-
73-
for (RequiredHeader header : REQUIRED_HEADERS) {
74-
String headerValue = exchange.getRequest().getHeaders().getFirst(header.name);
75-
if (headerValue == null || headerValue.trim().isEmpty()) {
76-
return handleMissingHeader(exchange, header.message);
77-
}
60+
61+
// Debug için tüm headerları yazdıralım
62+
request.getHeaders().forEach((key, values) ->
63+
log.debug("Header: {} = {}", key, values));
64+
65+
// X-User-Id header'ı kontrolü
66+
String userId = request.getHeaders().getFirst("X-User-Id");
67+
68+
if (userId == null || userId.isEmpty()) {
69+
log.warn("Gerekli X-User-Id header eksik, ancak isteğe devam ediliyor");
70+
// İsteği reddetmek yerine loga yazıp devam edelim
71+
// API Gateway zaten yetkilendirmeyi yapıyor
72+
} else {
73+
log.debug("İstek kimlik doğrulaması başarılı: {}", userId);
7874
}
79-
75+
8076
return chain.filter(exchange);
8177
};
8278
}
8379

8480
private boolean isPublicPath(String path) {
8581
return PUBLIC_PATHS.stream().anyMatch(path::startsWith);
8682
}
87-
88-
private String[] getPublicPaths() {
89-
return PUBLIC_PATHS.stream()
90-
.map(path -> path + "**")
91-
.toArray(String[]::new);
92-
}
93-
94-
private Mono<Void> handleMissingHeader(ServerWebExchange exchange, String message) {
95-
exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
96-
exchange.getResponse().getHeaders().add("X-Error-Message", message);
97-
return exchange.getResponse().setComplete();
98-
}
99-
100-
private static class RequiredHeader {
101-
final String name;
102-
final String message;
103-
104-
RequiredHeader(String name, String message) {
105-
this.name = name;
106-
this.message = message;
107-
}
108-
}
10983
}
84+

activity-log-service/src/main/java/com/craftpilot/activitylogservice/config/WebFluxConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.context.annotation.Configuration;
44
import org.springframework.web.reactive.config.CorsRegistry;
55
import org.springframework.web.reactive.config.EnableWebFlux;
6+
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
67
import org.springframework.web.reactive.config.WebFluxConfigurer;
78

89
@Configuration
@@ -22,8 +23,14 @@ public void addCorsMappings(CorsRegistry registry) {
2223
.allowedHeaders("*")
2324
.exposedHeaders("Content-Type", "X-Requested-With", "Accept", "Origin",
2425
"Access-Control-Request-Method", "Access-Control-Request-Headers",
25-
"X-Total-Count")
26+
"X-Total-Count", "X-Error-Message")
2627
.allowCredentials(true)
2728
.maxAge(3600);
2829
}
30+
31+
@Override
32+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
33+
registry.addResourceHandler("/static/**")
34+
.addResourceLocations("classpath:/static/");
35+
}
2936
}

craft-pilot-commons/pom.xml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.1.4</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.craftpilot</groupId>
12+
<artifactId>craft-pilot-commons</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>craft-pilot-commons</name>
15+
<description>Common utilities and shared components for CraftPilot microservices</description>
16+
17+
<properties>
18+
<java.version>17</java.version>
19+
<spring-kafka.version>3.0.10</spring-kafka.version>
20+
<lombok.version>1.18.30</lombok.version>
21+
</properties>
22+
23+
<dependencies>
24+
<!-- Spring Boot Core -->
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter</artifactId>
28+
<optional>true</optional>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-configuration-processor</artifactId>
33+
<optional>true</optional>
34+
</dependency>
35+
36+
<!-- Spring AOP -->
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-aop</artifactId>
40+
<optional>true</optional>
41+
</dependency>
42+
43+
<!-- Kafka -->
44+
<dependency>
45+
<groupId>org.springframework.kafka</groupId>
46+
<artifactId>spring-kafka</artifactId>
47+
<version>${spring-kafka.version}</version>
48+
<optional>true</optional>
49+
</dependency>
50+
51+
<!-- Project Reactor -->
52+
<dependency>
53+
<groupId>io.projectreactor</groupId>
54+
<artifactId>reactor-core</artifactId>
55+
<optional>true</optional>
56+
</dependency>
57+
58+
<!-- Jackson -->
59+
<dependency>
60+
<groupId>com.fasterxml.jackson.core</groupId>
61+
<artifactId>jackson-databind</artifactId>
62+
<optional>true</optional>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.fasterxml.jackson.datatype</groupId>
66+
<artifactId>jackson-datatype-jsr310</artifactId>
67+
<optional>true</optional>
68+
</dependency>
69+
70+
<!-- Validation -->
71+
<dependency>
72+
<groupId>jakarta.validation</groupId>
73+
<artifactId>jakarta.validation-api</artifactId>
74+
<optional>true</optional>
75+
</dependency>
76+
77+
<!-- Lombok -->
78+
<dependency>
79+
<groupId>org.projectlombok</groupId>
80+
<artifactId>lombok</artifactId>
81+
<version>${lombok.version}</version>
82+
<optional>true</optional>
83+
</dependency>
84+
85+
<!-- Testing -->
86+
<dependency>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-starter-test</artifactId>
89+
<scope>test</scope>
90+
</dependency>
91+
</dependencies>
92+
93+
<build>
94+
<plugins>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-compiler-plugin</artifactId>
98+
<configuration>
99+
<annotationProcessorPaths>
100+
<path>
101+
<groupId>org.projectlombok</groupId>
102+
<artifactId>lombok</artifactId>
103+
<version>${lombok.version}</version>
104+
</path>
105+
</annotationProcessorPaths>
106+
</configuration>
107+
</plugin>
108+
</plugins>
109+
</build>
110+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.craftpilot.commons.activity.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Bir metodun çalışması sonucunda otomatik aktivite kaydı oluşturmak için kullanılır.
10+
* Aspect tarafından işlenir.
11+
*/
12+
@Target({ElementType.METHOD})
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface LogActivity {
15+
/**
16+
* Aktivite tipi
17+
*/
18+
String actionType();
19+
20+
/**
21+
* Kullanıcı ID'si hangi parametre üzerinden alınacak
22+
* Parametre adı veya SpEL ifadesi olabilir
23+
* Örn: "userId" veya "#chatHistory.userId"
24+
*/
25+
String userIdParam() default "userId";
26+
27+
/**
28+
* Ek metadatalar için SpEL ifadesi
29+
* Örn: "{id: #result.id, title: #result.title}"
30+
*/
31+
String metadata() default "";
32+
}

0 commit comments

Comments
 (0)