Skip to content

Commit 7a704cc

Browse files
committed
feat: Integrate security framework into core components - Add SecurityInterceptor support in RequestHandler - Add AuthProcessor integration in Server - Enhance DIContainer with component scanning methods
1 parent b35d3f5 commit 7a704cc

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/main/java/jazzyframework/core/RequestHandler.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import jazzyframework.http.validation.ValidationResult;
2020
import jazzyframework.routing.Route;
2121
import jazzyframework.routing.Router;
22+
import jazzyframework.security.SecurityInterceptor;
2223

2324
/**
2425
* Handles individual HTTP requests received by the server.
@@ -191,6 +192,25 @@ public void run() {
191192

192193
Request request = new Request(method, path, headers, pathParams, queryParams, body);
193194

195+
// Security interceptor check
196+
if (router.getDIContainer() != null) {
197+
try {
198+
SecurityInterceptor securityInterceptor = router.getDIContainer().getComponent(SecurityInterceptor.class);
199+
Response securityResponse = securityInterceptor.intercept(request);
200+
if (securityResponse != null) {
201+
// Security check failed, return security response
202+
out.write(securityResponse.toHttpResponse());
203+
out.flush();
204+
logger.fine("Request blocked by security interceptor");
205+
return;
206+
}
207+
logger.fine("Security check passed");
208+
} catch (Exception e) {
209+
// SecurityInterceptor not found or error, continue without security
210+
logger.fine("No SecurityInterceptor found, proceeding without security check");
211+
}
212+
}
213+
194214
boolean isBodyRequired = method.equals("POST") || method.equals("PUT") || method.equals("PATCH");
195215
boolean isBodyEmpty = body == null || body.trim().isEmpty();
196216

src/main/java/jazzyframework/core/Server.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import jazzyframework.data.CrudProcessor;
1010
import jazzyframework.di.DIContainer;
1111
import jazzyframework.routing.Router;
12+
import jazzyframework.security.AuthProcessor;
1213

1314
/**
1415
* The main server class of the Jazzy Framework.
@@ -50,6 +51,9 @@ public Server(Router router, Config config) {
5051

5152
// Process @Crud annotations after DI initialization
5253
processCrudAnnotations();
54+
55+
// Process @EnableJazzyAuth annotations after DI initialization
56+
processAuthAnnotations();
5357

5458
if (config.isEnableMetrics()) {
5559
router.addRoute("GET", "/metrics", "getMetrics", MetricsController.class);
@@ -84,6 +88,21 @@ private void processCrudAnnotations() {
8488
}
8589
}
8690

91+
/**
92+
* Processes @EnableJazzyAuth annotations and automatically configures authentication.
93+
*/
94+
private void processAuthAnnotations() {
95+
try {
96+
AuthProcessor authProcessor = new AuthProcessor(router, diContainer);
97+
authProcessor.processAuthAnnotations();
98+
logger.info("@EnableJazzyAuth annotations processed and authentication configured");
99+
} catch (Exception e) {
100+
logger.warning("Failed to process @EnableJazzyAuth annotations: " + e.getMessage());
101+
e.printStackTrace();
102+
// Don't fail startup if auth processing fails
103+
}
104+
}
105+
87106
/**
88107
* Gets the DI container used by this server.
89108
*

src/main/java/jazzyframework/di/DIContainer.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.HashMap;
1717
import java.util.List;
1818
import java.util.Map;
19+
import java.util.Set;
1920
import java.util.logging.Logger;
2021
import java.util.stream.Collectors;
2122

@@ -475,6 +476,47 @@ public Map<String, BeanDefinition> getBeanDefinitions() {
475476
return new HashMap<>(beanDefinitions);
476477
}
477478

479+
/**
480+
* Gets all registered classes.
481+
*
482+
* @return set of all registered classes
483+
*/
484+
public Set<Class<?>> getAllRegisteredClasses() {
485+
return beanDefinitions.values().stream()
486+
.map(BeanDefinition::getBeanClass)
487+
.collect(Collectors.toSet());
488+
}
489+
490+
/**
491+
* Gets a bean instance by class type (alias for getComponent).
492+
*
493+
* @param type the class type
494+
* @param <T> the type parameter
495+
* @return the component instance
496+
*/
497+
public <T> T getBean(Class<T> type) {
498+
return getComponent(type);
499+
}
500+
501+
/**
502+
* Registers a pre-created instance as a singleton bean.
503+
*
504+
* @param type the class type
505+
* @param instance the instance to register
506+
* @param <T> the type parameter
507+
*/
508+
public <T> void registerInstance(Class<T> type, T instance) {
509+
BeanDefinition beanDef = new BeanDefinition(type);
510+
beanDef.setSingletonInstance(instance);
511+
registerBeanDefinition(beanDef);
512+
513+
if (!managedBeans.contains(instance)) {
514+
managedBeans.add(instance);
515+
}
516+
517+
logger.info("Registered singleton instance: " + type.getSimpleName());
518+
}
519+
478520
/**
479521
* Gets the underlying PicoContainer.
480522
*

0 commit comments

Comments
 (0)