8
8
import org .springframework .scheduling .annotation .Async ;
9
9
import org .springframework .stereotype .Service ;
10
10
11
+ import java .security .SecureRandom ;
11
12
import java .util .Random ;
12
13
import java .util .UUID ;
13
14
import java .util .concurrent .CompletableFuture ;
19
20
public class ExternalCarrierService {
20
21
21
22
private final ShipmentService shipmentService ;
22
- private final Random random = new Random ();
23
+ @ SuppressWarnings ("java:S2245" )
24
+ private final SecureRandom secureRandom = new SecureRandom ();
23
25
24
26
private static final String [] CARRIER_NAMES = {
25
27
"Express Delivery" , "Fast Shipping" , "Quick Carrier" , "Safe Transport"
@@ -32,11 +34,11 @@ public void requestDelivery(Shipment shipment) {
32
34
log .info ("Requesting delivery to external carrier for Shipment : {}" , shipment .getShipUUID ());
33
35
34
36
// 택배사 정보 생성 (랜덤)
35
- String carrierName = CARRIER_NAMES [random .nextInt (CARRIER_NAMES .length )];
37
+ String carrierName = CARRIER_NAMES [secureRandom .nextInt (CARRIER_NAMES .length )];
36
38
String trackingNumber = generateTrackingNumber ();
37
39
38
40
// 배송 접수 처리 시간 시뮬레이션 (1-3초)
39
- TimeUnit .SECONDS .sleep (1 + random .nextInt (3 ));
41
+ TimeUnit .SECONDS .sleep (1 + secureRandom .nextInt (3 ));
40
42
41
43
CarrierUpdateRequest readyRequest = CarrierUpdateRequest .of (
42
44
carrierName ,
@@ -69,12 +71,12 @@ private void simulateDeliveryProcess(UUID shipUUID) {
69
71
CompletableFuture .runAsync (() -> {
70
72
try {
71
73
// 배송출발 단계
72
- TimeUnit .SECONDS .sleep (5 + random .nextInt (10 ));
74
+ TimeUnit .SECONDS .sleep (5 + secureRandom .nextInt (10 ));
73
75
shipmentService .updateShipmentStatusByUUID (shipUUID , ExternalShippingStatus .SHIPPING );
74
76
75
77
// 최종 배송 완료/실패 (90% 성공률)
76
- TimeUnit .SECONDS .sleep (5 + random .nextInt (10 ));
77
- if (random .nextInt (10 ) < 9 ) {
78
+ TimeUnit .SECONDS .sleep (5 + secureRandom .nextInt (10 ));
79
+ if (secureRandom .nextInt (10 ) < 9 ) {
78
80
shipmentService .updateShipmentStatusByUUID (shipUUID , ExternalShippingStatus .DELIVERED );
79
81
log .info ("Delivery completed successfully: {}" , shipUUID );
80
82
} else {
@@ -97,12 +99,12 @@ private String generateTrackingNumber() {
97
99
98
100
// 2자리 대문자 알파벳
99
101
for (int i = 0 ; i < 2 ; i ++) {
100
- sb .append ((char ) ('A' + random .nextInt (26 )));
102
+ sb .append ((char ) ('A' + secureRandom .nextInt (26 )));
101
103
}
102
104
103
105
// 10자리 숫자
104
106
for (int i = 0 ; i < 10 ; i ++) {
105
- sb .append (random .nextInt (10 ));
107
+ sb .append (secureRandom .nextInt (10 ));
106
108
}
107
109
108
110
return sb .toString ();
0 commit comments