-
Notifications
You must be signed in to change notification settings - Fork 0
Fix/#62 payment response #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/jobdri/jobdri_api/domain/audit/annotation/AuditLogEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.jobdri.jobdri_api.domain.audit.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface AuditLogEvent { | ||
| String action(); | ||
|
|
||
| String targetType(); | ||
|
|
||
| String targetId() default ""; | ||
| } |
117 changes: 117 additions & 0 deletions
117
src/main/java/com/jobdri/jobdri_api/domain/audit/aop/AuditLogAspect.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package com.jobdri.jobdri_api.domain.audit.aop; | ||
|
|
||
| import com.jobdri.jobdri_api.domain.audit.annotation.AuditLogEvent; | ||
| import com.jobdri.jobdri_api.domain.audit.service.AuditLogService; | ||
| import com.jobdri.jobdri_api.domain.user.entity.User; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.aspectj.lang.ProceedingJoinPoint; | ||
| import org.aspectj.lang.annotation.Around; | ||
| import org.aspectj.lang.annotation.Aspect; | ||
| import org.aspectj.lang.reflect.MethodSignature; | ||
| import org.springframework.context.expression.MethodBasedEvaluationContext; | ||
| import org.springframework.core.DefaultParameterNameDiscoverer; | ||
| import org.springframework.core.ParameterNameDiscoverer; | ||
| import org.springframework.expression.ExpressionParser; | ||
| import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| @Aspect | ||
| @Component | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class AuditLogAspect { | ||
|
|
||
| private static final String RESULT_VARIABLE = "result"; | ||
|
|
||
| private final AuditLogService auditLogService; | ||
| private final ExpressionParser expressionParser = new SpelExpressionParser(); | ||
| private final ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer(); | ||
|
|
||
| @Around("@annotation(auditLogEvent)") | ||
| public Object recordAuditLog(ProceedingJoinPoint joinPoint, AuditLogEvent auditLogEvent) throws Throwable { | ||
| Map<String, Object> beforeValue = extractParameters(joinPoint); | ||
|
|
||
| Object result = joinPoint.proceed(); | ||
|
|
||
| try { | ||
| auditLogService.record( | ||
| extractUser(joinPoint), | ||
| auditLogEvent.action(), | ||
| auditLogEvent.targetType(), | ||
| evaluateTargetId(joinPoint, auditLogEvent.targetId(), result), | ||
| beforeValue, | ||
| result | ||
| ); | ||
| } catch (RuntimeException e) { | ||
| log.warn("Audit log recording failed. action={}, method={}", | ||
| auditLogEvent.action(), | ||
| joinPoint.getSignature().toShortString(), | ||
| e | ||
| ); | ||
| throw e; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private User extractUser(ProceedingJoinPoint joinPoint) { | ||
| for (Object arg : joinPoint.getArgs()) { | ||
| if (arg instanceof User user) { | ||
| return user; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private Long evaluateTargetId(ProceedingJoinPoint joinPoint, String targetIdExpression, Object result) { | ||
| if (targetIdExpression == null || targetIdExpression.isBlank()) { | ||
| return null; | ||
| } | ||
|
|
||
| Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); | ||
| MethodBasedEvaluationContext context = new MethodBasedEvaluationContext( | ||
| null, | ||
| method, | ||
| joinPoint.getArgs(), | ||
| parameterNameDiscoverer | ||
| ); | ||
| Object[] args = joinPoint.getArgs(); | ||
| for (int i = 0; i < args.length; i++) { | ||
| context.setVariable("arg" + i, args[i]); | ||
| context.setVariable("p" + i, args[i]); | ||
| } | ||
| context.setVariable(RESULT_VARIABLE, result); | ||
|
|
||
| Object value = expressionParser.parseExpression(targetIdExpression).getValue(context); | ||
| if (value instanceof Number number) { | ||
| return number.longValue(); | ||
| } | ||
| if (value instanceof String string && !string.isBlank()) { | ||
| return Long.parseLong(string); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private Map<String, Object> extractParameters(ProceedingJoinPoint joinPoint) { | ||
| MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
| String[] parameterNames = signature.getParameterNames(); | ||
| Object[] args = joinPoint.getArgs(); | ||
|
|
||
| Map<String, Object> parameters = new LinkedHashMap<>(); | ||
| for (int i = 0; i < args.length; i++) { | ||
| if (args[i] instanceof User) { | ||
| continue; | ||
| } | ||
| String parameterName = parameterNames != null && i < parameterNames.length | ||
| ? parameterNames[i] | ||
| : "arg" + i; | ||
| parameters.put(parameterName, args[i]); | ||
| } | ||
| return parameters; | ||
| } | ||
| } | ||
102 changes: 102 additions & 0 deletions
102
src/main/java/com/jobdri/jobdri_api/domain/audit/entity/AuditLog.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package com.jobdri.jobdri_api.domain.audit.entity; | ||
|
|
||
| import com.jobdri.jobdri_api.domain.user.entity.User; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Entity | ||
| @Table(name = "audit_logs") | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class AuditLog { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "user_id") | ||
| private User user; | ||
|
|
||
| @Column(nullable = false, length = 80) | ||
| private String action; | ||
|
|
||
| @Column(nullable = false, length = 80) | ||
| private String targetType; | ||
|
|
||
| private Long targetId; | ||
|
|
||
| @Column(columnDefinition = "TEXT") | ||
| private String beforeValue; | ||
|
|
||
| @Column(columnDefinition = "TEXT") | ||
| private String afterValue; | ||
|
|
||
| @Column(length = 100) | ||
| private String ipAddress; | ||
|
|
||
| @Column(length = 500) | ||
| private String userAgent; | ||
|
|
||
| @Column(nullable = false) | ||
| private LocalDateTime createdAt; | ||
|
|
||
| @Builder(access = AccessLevel.PRIVATE) | ||
| private AuditLog( | ||
| User user, | ||
| String action, | ||
| String targetType, | ||
| Long targetId, | ||
| String beforeValue, | ||
| String afterValue, | ||
| String ipAddress, | ||
| String userAgent, | ||
| LocalDateTime createdAt | ||
| ) { | ||
| this.user = user; | ||
| this.action = action; | ||
| this.targetType = targetType; | ||
| this.targetId = targetId; | ||
| this.beforeValue = beforeValue; | ||
| this.afterValue = afterValue; | ||
| this.ipAddress = ipAddress; | ||
| this.userAgent = userAgent; | ||
| this.createdAt = createdAt; | ||
| } | ||
|
|
||
| public static AuditLog create( | ||
| User user, | ||
| String action, | ||
| String targetType, | ||
| Long targetId, | ||
| String beforeValue, | ||
| String afterValue, | ||
| String ipAddress, | ||
| String userAgent | ||
| ) { | ||
| return AuditLog.builder() | ||
| .user(user) | ||
| .action(action) | ||
| .targetType(targetType) | ||
| .targetId(targetId) | ||
| .beforeValue(beforeValue) | ||
| .afterValue(afterValue) | ||
| .ipAddress(ipAddress) | ||
| .userAgent(userAgent) | ||
| .createdAt(LocalDateTime.now()) | ||
| .build(); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/jobdri/jobdri_api/domain/audit/repository/AuditLogRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.jobdri.jobdri_api.domain.audit.repository; | ||
|
|
||
| import com.jobdri.jobdri_api.domain.audit.entity.AuditLog; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface AuditLogRepository extends JpaRepository<AuditLog, Long> { | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid propagating audit write failures to business flow.
Rethrowing here means transient audit issues (serialization/DB) can fail user-facing write operations across all annotated services. This should be best-effort logging unless fail-closed is explicitly required.
Suggested fix
} catch (RuntimeException e) { log.warn("Audit log recording failed. action={}, method={}", auditLogEvent.action(), joinPoint.getSignature().toShortString(), e ); - throw e; }🤖 Prompt for AI Agents