Skip to content

Commit 660e445

Browse files
committed
♻️ refactor: refactor codebase #2
1 parent 5c6bf9d commit 660e445

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version:
9999
// AOP enables modularization of cross-cutting concerns such as logging, security, and transactions by allowing aspects to be applied to various parts of the application.
100100
// This starter simplifies the setup and configuration of AOP-related functionality, promoting cleaner and more maintainable code by separating concerns effectively.
101101
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version: '2.7.18'
102+
// The "spring-boot-starter-security" library, version 2.7.18, is an essential component of Spring Boot applications,
103+
// offering robust security features to safeguard your application's endpoints, authenticate users, and manage access control effectively.
104+
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.7.18'
102105
```
103106
104107
## Integration

plugin/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ dependencies {
191191
// AOP enables modularization of cross-cutting concerns such as logging, security, and transactions by allowing aspects to be applied to various parts of the application.
192192
// This starter simplifies the setup and configuration of AOP-related functionality, promoting cleaner and more maintainable code by separating concerns effectively.
193193
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version: '2.7.18'
194+
// The "spring-boot-starter-security" library, version 2.7.18, is an essential component of Spring Boot applications,
195+
// offering robust security features to safeguard your application's endpoints, authenticate users, and manage access control effectively.
196+
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.7.18'
194197
}
195198

196199
test {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.clarify4j.common.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
@Documented
6+
@Retention(RetentionPolicy.RUNTIME)
7+
@Target({ElementType.METHOD, ElementType.TYPE})
8+
public @interface WithAuthHeader {
9+
boolean disabled() default false;
10+
11+
String message() default "Access denied";
12+
13+
String key() default "x-api-key";
14+
15+
String value() default "";
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.clarify4j.config.handler;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
import org.aspectj.lang.annotation.Around;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.reflect.MethodSignature;
7+
import org.clarify4j.common.Clarify4j;
8+
import org.clarify4j.common.annotation.WithAuthHeader;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.security.access.AccessDeniedException;
12+
import org.springframework.stereotype.Component;
13+
import org.unify4j.common.Request4j;
14+
15+
import java.lang.reflect.Method;
16+
import java.util.Map;
17+
18+
@Aspect
19+
@Component
20+
public class WithAuthHeaderHandler {
21+
protected static final Logger logger = LoggerFactory.getLogger(WithAuthHeaderHandler.class);
22+
23+
@Around(value = "@annotation(org.clarify4j.common.annotation.WithAuthHeader)")
24+
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
25+
Object proceed = joinPoint.proceed();
26+
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
27+
Method method = signature.getMethod();
28+
WithAuthHeader auth = method.getAnnotation(WithAuthHeader.class);
29+
if (auth.disabled()) {
30+
return proceed;
31+
}
32+
Map<String, Object> headers = Request4j.getHeaders(Clarify4j.getRequest());
33+
if (!headers.containsKey(auth.key())) {
34+
String message = String.format("Authentication header by key: '%s' not found", auth.key());
35+
throw new AccessDeniedException(message);
36+
}
37+
if (!headers.get(auth.key()).equals(auth.value())) {
38+
throw new AccessDeniedException(auth.message());
39+
}
40+
return proceed;
41+
}
42+
}

0 commit comments

Comments
 (0)