浏览代码

修改报错

yumeng 6 天之前
父节点
当前提交
c8c104c641

+ 12 - 4
src/main/java/com/adx/tencent/AppConfiguration.java

@@ -155,6 +155,15 @@ public class AppConfiguration {
                 props.getRedisStreamMaxLen());
     }
 
+
+    private static String firstNonBlank(String... values) {
+        if (values == null) return null;
+        for (String value : values) {
+            if (value != null && !value.isBlank()) return value;
+        }
+        return null;
+    }
+
     @Bean
     public RedisLock redisLock(StringRedisTemplate redis) {
         return new RedisLock(redis);
@@ -169,13 +178,12 @@ public class AppConfiguration {
         }
         return new OppoClient(props.getOppoConversionEndpoint(), props.getOppoConversionSalt(),
                 props.getOppoAesBase64Key(), ownerId -> authService.getOwnerToken(
-                        ownerId, props.getOppoApiId(), props.getOppoApiKey()), objectMapper);
+                        firstNonBlank(ownerId, props.getOppoOwnerId()), props.getOppoApiId(), props.getOppoApiKey()), objectMapper);
     }
 
     @Bean
-    public OppoAuthService oppoAuthService(@Nullable OppoColdStore coldStore) {
-        if (coldStore == null) return null;
-        return new OppoAuthService(coldStore);
+    public OppoAuthService oppoAuthService() {
+        return new OppoAuthService();
     }
 
     @Bean

+ 1 - 1
src/main/java/com/adx/tencent/oppo/client/OppoClient.java

@@ -98,7 +98,7 @@ public class OppoClient {
                 .header("signature", signature)
                 .POST(HttpRequest.BodyPublishers.ofString(postData, StandardCharsets.UTF_8))
                 .timeout(Duration.ofSeconds(15));
-        String currentOwnerToken = ownerTokenByOwnerSupplier != null && ownerId != null
+        String currentOwnerToken = ownerTokenByOwnerSupplier != null
                 ? ownerTokenByOwnerSupplier.apply(ownerId)
                 : (ownerTokenSupplier == null ? ownerToken : ownerTokenSupplier.get());
         if (currentOwnerToken != null && !currentOwnerToken.isBlank()) request.header("ownerToken", currentOwnerToken);

文件差异内容过多而无法显示
+ 0 - 6
src/main/java/com/adx/tencent/oppo/model/OppoTokenRecord.java


+ 23 - 12
src/main/java/com/adx/tencent/oppo/service/OppoAuthService.java

@@ -1,22 +1,33 @@
 package com.adx.tencent.oppo.service;
 
-import com.adx.tencent.oppo.model.OppoTokenRecord;
-import com.adx.tencent.oppo.store.OppoColdStore;
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
 import java.time.Instant;
 import java.util.Base64;
 
-/** OPPO ownerToken 生成与持久化;token 有效期按文档的 20 分钟窗口处理。 */
+/** OPPO ownerToken 本地生成服务。 */
 public class OppoAuthService {
-    private final OppoColdStore store;
-    public OppoAuthService(OppoColdStore store){this.store=store;}
-    public synchronized String getOwnerToken(String ownerId,String apiId,String apiKey) {
-        if(ownerId==null||ownerId.isBlank()||apiId==null||apiId.isBlank()||apiKey==null||apiKey.isBlank()) throw new IllegalStateException("OPPO ownerId/apiId/apiKey are required");
-        long now=Instant.now().getEpochSecond(); OppoTokenRecord old=store.getToken(ownerId);
-        if(old!=null&&old.getOwnerToken()!=null&&old.getIssuedAt()!=null&&Math.abs(now-old.getIssuedAt())<900)return old.getOwnerToken();
-        String sign=sha1(apiId+apiKey+now); String token=Base64.getEncoder().encodeToString((ownerId+","+apiId+","+now+","+sign).getBytes(StandardCharsets.UTF_8));
-        OppoTokenRecord record=new OppoTokenRecord();record.setOwnerId(ownerId);record.setApiId(apiId);record.setApiKey(apiKey);record.setIssuedAt(now);record.setOwnerToken(token);record.setUpdatedAt(Instant.now());store.saveToken(record);return token;
+
+    public String getOwnerToken(String ownerId, String apiId, String apiKey) {
+        if (ownerId == null || ownerId.isBlank()
+                || apiId == null || apiId.isBlank()
+                || apiKey == null || apiKey.isBlank()) {
+            throw new IllegalStateException("OPPO ownerId/apiId/apiKey are required");
+        }
+        long now = Instant.now().getEpochSecond();
+        String sign = sha1(apiId + apiKey + now);
+        return Base64.getEncoder().encodeToString(
+                (ownerId + "," + apiId + "," + now + "," + sign).getBytes(StandardCharsets.UTF_8));
+    }
+
+    private static String sha1(String value) {
+        try {
+            byte[] digest = MessageDigest.getInstance("SHA-1").digest(value.getBytes(StandardCharsets.UTF_8));
+            StringBuilder result = new StringBuilder();
+            for (byte b : digest) result.append(String.format("%02x", b));
+            return result.toString();
+        } catch (Exception e) {
+            throw new IllegalStateException("OPPO token sign failed", e);
+        }
     }
-    private static String sha1(String value){try{byte[] d=MessageDigest.getInstance("SHA-1").digest(value.getBytes(StandardCharsets.UTF_8));StringBuilder s=new StringBuilder();for(byte b:d)s.append(String.format("%02x",b));return s.toString();}catch(Exception e){throw new IllegalStateException("OPPO token sign failed",e);}}
 }

文件差异内容过多而无法显示
+ 0 - 3
src/main/java/com/adx/tencent/oppo/store/OppoColdStore.java


+ 0 - 9
src/main/resources/schema-oppo.sql

@@ -68,12 +68,3 @@ CREATE TABLE IF NOT EXISTS oppo_tag_event (
     data_type INT NOT NULL COMMENT 'OPPO dataType',
     PRIMARY KEY (tag_id, baidu_act, data_type)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='OPPO广告位转化事件映射';
-
-CREATE TABLE IF NOT EXISTS oppo_owner_tokens (
-    owner_id VARCHAR(128) NOT NULL PRIMARY KEY COMMENT 'OPPO广告主owner_id',
-    api_id VARCHAR(128) NOT NULL COMMENT 'OPPO Omni API id',
-    api_key VARCHAR(512) NOT NULL COMMENT 'OPPO Omni API key',
-    owner_token TEXT NOT NULL COMMENT '最近生成的ownerToken',
-    issued_at BIGINT NOT NULL COMMENT 'token生成时间(秒)',
-    updated_at TIMESTAMP(3) NOT NULL COMMENT '更新时间'
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='OPPO ownerToken授权缓存';