Skip to content

Commit 7eb9aa7

Browse files
committed
feat:idempotency aop feature and test code added
1 parent 64053f1 commit 7eb9aa7

17 files changed

+1936
-34
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
KafkaServer {
2+
org.apache.kafka.common.security.plain.PlainLoginModule required
3+
username="admin"
4+
password="admin!23"
5+
user_admin="admin!23";
6+
};
7+
8+
Client {
9+
org.apache.kafka.common.security.plain.PlainLoginModule required
10+
username="admin"
11+
password="admin!23";
12+
};

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ build/
3131

3232
### VS Code ###
3333
.vscode/
34+
35+
.docker-compose/.docker-volumes

auth/logs/auth.log

Lines changed: 714 additions & 0 deletions
Large diffs are not rendered by default.

auth/src/main/java/com/woobeee/auth/aop/IdempotencyAspect.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.woobeee.auth.aop;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import com.woobeee.auth.dto.IdempotencyResult;
54
import com.woobeee.auth.dto.response.ApiResponse;
6-
import com.woobeee.auth.exception.*;
7-
import com.woobeee.auth.service.IdempotencyService;
5+
import com.woobeee.auth.exception.CustomConflictException;
6+
import com.woobeee.auth.exception.JwtExpiredException;
7+
import com.woobeee.auth.exception.JwtNotValidException;
8+
import com.woobeee.auth.exception.PasswordNotMatchException;
9+
import com.woobeee.auth.exception.UserConflictException;
10+
import com.woobeee.auth.exception.UserNotFoundException;
811
import jakarta.servlet.http.HttpServletRequest;
912
import lombok.RequiredArgsConstructor;
1013
import lombok.extern.slf4j.Slf4j;
@@ -20,14 +23,15 @@
2023
import org.springframework.web.server.ResponseStatusException;
2124
import org.springframework.web.util.ContentCachingRequestWrapper;
2225
import org.springframework.web.util.WebUtils;
26+
import tools.jackson.databind.ObjectMapper;
2327

2428
import java.nio.charset.StandardCharsets;
2529
import java.util.Base64;
2630

2731
@Aspect
2832
@Component
29-
@RequiredArgsConstructor
3033
@Slf4j
34+
@RequiredArgsConstructor
3135
public class IdempotencyAspect {
3236
private final IdempotencyService idempotencyService;
3337
private final ObjectMapper objectMapper;

auth/src/main/java/com/woobeee/auth/aop/IdempotencyService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.woobeee.auth.service;
1+
package com.woobeee.auth.aop;
22

33

44
import com.woobeee.auth.dto.IdempotencyResult;

auth/src/main/java/com/woobeee/auth/aop/IdempotencyServiceImpl.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
package com.woobeee.auth.service;
1+
package com.woobeee.auth.aop;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
53
import com.woobeee.auth.dto.IdempotencyResult;
64
import com.woobeee.auth.entity.IdempotencyRecord;
75
import com.woobeee.auth.exception.CustomConflictException;
@@ -13,14 +11,14 @@
1311
import org.springframework.stereotype.Service;
1412
import org.springframework.transaction.annotation.Propagation;
1513
import org.springframework.transaction.annotation.Transactional;
14+
import tools.jackson.databind.ObjectMapper;
1615

1716
import java.time.Duration;
1817

19-
20-
@RequiredArgsConstructor
2118
@Slf4j
2219
@Service
2320
@Transactional
21+
@RequiredArgsConstructor
2422
public class IdempotencyServiceImpl implements IdempotencyService{
2523
private final IdempotencyRecordRepository idempotencyRecordRepository;
2624
private final ObjectMapper objectMapper;
@@ -66,26 +64,18 @@ public void complete(String clientId, String domainKey, int code, Object body) {
6664
IdempotencyRecord r = idempotencyRecordRepository
6765
.findByClientIdAndDomainKey(clientId, domainKey).orElseThrow();
6866

69-
try {
70-
String b = objectMapper.writeValueAsString(body);
71-
r.markCompleted(code, b);
72-
idempotencyRecordRepository.saveAndFlush(r);
73-
} catch (JsonProcessingException e) {
74-
throw new RuntimeException("Failed to serialize response body", e);
75-
}
67+
String b = objectMapper.writeValueAsString(body);
68+
r.markCompleted(code, b);
69+
idempotencyRecordRepository.saveAndFlush(r);
7670
}
7771

7872
@Override
7973
public void fail(String clientId, String domainKey, int code, Object body) {
8074
IdempotencyRecord r = idempotencyRecordRepository
8175
.findByClientIdAndDomainKey(clientId, domainKey).orElseThrow();
8276

83-
try {
84-
String b = objectMapper.writeValueAsString(body);
85-
r.markFailed(code, b);
86-
idempotencyRecordRepository.saveAndFlush(r);
87-
} catch (JsonProcessingException e) {
88-
throw new RuntimeException("Failed to serialize response body", e);
89-
}
77+
String b = objectMapper.writeValueAsString(body);
78+
r.markFailed(code, b);
79+
idempotencyRecordRepository.saveAndFlush(r);
9080
}
9181
}

auth/src/main/java/com/woobeee/auth/entity/IdempotencyRecord.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.woobeee.auth.entity;
22

33
import jakarta.persistence.*;
4-
import lombok.Getter;
54

65
import java.time.Duration;
76
import java.time.LocalDateTime;
87

98
@Entity
10-
@Getter
119
@Table(
1210
uniqueConstraints = @UniqueConstraint(columnNames = {"clientId", "domainKey"})
1311
)
@@ -60,4 +58,40 @@ public void markFailed(int responseCode, String responseBody) {
6058
this.responseBody = responseBody;
6159
}
6260

61+
public Long getId() {
62+
return id;
63+
}
64+
65+
public String getClientId() {
66+
return clientId;
67+
}
68+
69+
public String getDomainKey() {
70+
return domainKey;
71+
}
72+
73+
public String getRequestHash() {
74+
return requestHash;
75+
}
76+
77+
public Status getStatus() {
78+
return status;
79+
}
80+
81+
public Integer getResponseCode() {
82+
return responseCode;
83+
}
84+
85+
public String getResponseBody() {
86+
return responseBody;
87+
}
88+
89+
public LocalDateTime getCreatedAt() {
90+
return createdAt;
91+
}
92+
93+
public LocalDateTime getExpiresAt() {
94+
return expiresAt;
95+
}
96+
6397
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
package com.woobeee.auth.exception;
22

33
public enum ErrorCode {
4+
signIn_GoogleTokenNotValid,
5+
signIn_userConflict,
6+
login_userNotFound,
7+
login_passwordNotMatch,
48
login_jwtExpired,
5-
login_jwtInvalid
9+
login_jwtInvalid,
10+
login_refreshTokenMissing,
11+
login_refreshTokenNotFound,
12+
13+
comment_needAuthentication,
14+
like_needAuthentication,
15+
post_notFound,
16+
17+
api_idempotencyKeyConflictStopTryingToMessWithMyServer,
18+
api_idempotencyKeyConflict
619
}

auth/src/main/java/com/woobeee/auth/token/AccessTokenProvider.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.jsonwebtoken.JwtException;
1010
import io.jsonwebtoken.Jwts;
1111
import io.jsonwebtoken.security.Keys;
12-
import lombok.Getter;
1312
import lombok.extern.slf4j.Slf4j;
1413
import org.springframework.beans.factory.annotation.Value;
1514
import org.springframework.stereotype.Component;
@@ -27,9 +26,7 @@ public class AccessTokenProvider {
2726
private static final String ACCESS_TOKEN_TYPE = "access";
2827
private static final String REFRESH_TOKEN_TYPE = "refresh";
2928

30-
@Getter
3129
private final long accessTokenExpirationMillis;
32-
@Getter
3330
private final long refreshTokenExpirationMillis;
3431
private final SecretKey accessTokenKey;
3532
private final SecretKey refreshTokenKey;
@@ -109,9 +106,17 @@ private void validateTokenType(Claims claims) {
109106
}
110107
}
111108

109+
public long getAccessTokenExpirationMillis() {
110+
return accessTokenExpirationMillis;
111+
}
112+
113+
public long getRefreshTokenExpirationMillis() {
114+
return refreshTokenExpirationMillis;
115+
}
116+
112117
public record RefreshTokenPayload(
113118
String loginId,
114119
UUID tokenId
115120
) {
116121
}
117-
}
122+
}

auth/src/main/java/com/woobeee/auth/token/RefreshTokenCookieProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import java.time.Duration;
1010

1111
@Component
12-
public class RefreshTokenProvider {
12+
public class RefreshTokenCookieProvider {
1313
private final AccessTokenProvider accessTokenProvider;
1414
private final String cookieName;
1515
private final String cookiePath;
1616
private final boolean secureCookie;
1717
private final String sameSite;
1818

19-
public RefreshTokenProvider(
19+
public RefreshTokenCookieProvider(
2020
AccessTokenProvider accessTokenProvider,
2121
@Value("${jwt.refresh-token.cookie-name:refreshToken}") String cookieName,
2222
@Value("${jwt.refresh-token.cookie-path:/api/auth}") String cookiePath,

0 commit comments

Comments
 (0)