|
| 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