From 69a3c374ca3afb770e3b9ffcbdda07ce362cbf58 Mon Sep 17 00:00:00 2001
From: 1 <1@123>
Date: 星期五, 09 一月 2026 19:59:29 +0800
Subject: [PATCH] #

---
 rsf-open-api/src/main/java/com/vincent/rsf/openApi/controller/example/TokenAuthExampleController.java |  182 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 182 insertions(+), 0 deletions(-)

diff --git a/rsf-open-api/src/main/java/com/vincent/rsf/openApi/controller/example/TokenAuthExampleController.java b/rsf-open-api/src/main/java/com/vincent/rsf/openApi/controller/example/TokenAuthExampleController.java
new file mode 100644
index 0000000..a531bea
--- /dev/null
+++ b/rsf-open-api/src/main/java/com/vincent/rsf/openApi/controller/example/TokenAuthExampleController.java
@@ -0,0 +1,182 @@
+package com.vincent.rsf.openApi.controller.example;
+
+import com.vincent.rsf.framework.common.Cools;
+import com.vincent.rsf.openApi.entity.constant.Constants;
+import com.vincent.rsf.openApi.entity.dto.CommonResponse;
+import com.vincent.rsf.openApi.entity.AppAuthParam;
+import com.vincent.rsf.openApi.security.service.AppAuthService;
+import com.vincent.rsf.openApi.security.utils.TokenUtils;
+import io.swagger.annotations.ApiOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Token璁よ瘉绀轰緥鎺у埗鍣�
+ * 婕旂ず濡備綍浣跨敤JWT Token杩涜鎺ュ彛淇濇姢
+ */
+@RestController
+@RequestMapping("/api/example/token")
+public class TokenAuthExampleController {
+    private static final Logger log = LoggerFactory.getLogger(TokenAuthExampleController.class);
+
+    @Resource
+    private AppAuthService appAuthService;
+
+    /**
+     * 鑾峰彇鍙椾繚鎶ょ殑鏁版嵁 - 闇�瑕佹湁鏁堢殑Token
+     * 
+     * @param request HTTP璇锋眰
+     * @return 鍙椾繚鎶ょ殑鏁版嵁
+     */
+    @GetMapping("/protected-data")
+    public Map<String, Object> getProtectedData(HttpServletRequest request) {
+        // 浠庤姹傚睘鎬т腑鑾峰彇璁よ瘉淇℃伅锛堢敱TokenAuthenticationFilter璁剧疆锛�
+        String appId = (String) request.getAttribute(Constants.REQUEST_ATTR_APP_ID);
+        String userId = (String) request.getAttribute(Constants.REQUEST_ATTR_USER_ID);
+        
+        log.info("璁块棶鍙椾繚鎶ゆ帴鍙o紝AppId: {}, UserId: {}", appId, userId);
+        
+        Map<String, Object> response = new HashMap<>();
+        response.put("code", "200");
+        response.put("message", "璁块棶鎴愬姛");
+        response.put("data", Map.of(
+            "appId", appId,
+            "userId", userId,
+            "protectedInfo", "杩欐槸鍙椾繚鎶ょ殑鏁版嵁",
+            "timestamp", System.currentTimeMillis()
+        ));
+        response.put("success", true);
+        
+        return response;
+    }
+
+    /**
+     * 鑾峰彇鐢ㄦ埛淇℃伅 - 闇�瑕佹湁鏁堢殑Token
+     * 
+     * @param request HTTP璇锋眰
+     * @return 鐢ㄦ埛淇℃伅
+     */
+    @GetMapping("/user-info")
+    public Map<String, Object> getUserInfo(HttpServletRequest request) {
+        // 浠庤姹傚睘鎬т腑鑾峰彇璁よ瘉淇℃伅
+        String appId = (String) request.getAttribute(Constants.REQUEST_ATTR_APP_ID);
+        String userId = (String) request.getAttribute(Constants.REQUEST_ATTR_USER_ID);
+        
+        log.info("鑾峰彇鐢ㄦ埛淇℃伅锛孉ppId: {}, UserId: {}", appId, userId);
+        
+        Map<String, Object> response = new HashMap<>();
+        response.put("code", "200");
+        response.put("message", "鑾峰彇鐢ㄦ埛淇℃伅鎴愬姛");
+        response.put("data", Map.of(
+            "appId", appId,
+            "userId", userId,
+            "userName", "鐢ㄦ埛" + (userId != null ? userId : "鏈煡"),
+            "role", "USER",
+            "permissions", new String[]{"read", "write"}
+        ));
+        response.put("success", true);
+        
+        return response;
+    }
+
+    /**
+     * 鎵嬪姩鐢熸垚Token鐨勭ず渚嬫帴鍙�
+     * 娉ㄦ剰锛氬湪瀹為檯搴旂敤涓紝杩欎釜鎺ュ彛閫氬父闇�瑕佸叾浠栧舰寮忕殑璁よ瘉
+     * 
+     * @param appId 搴旂敤ID
+     * @param userId 鐢ㄦ埛ID
+     * @return 鍖呭惈Token鐨勫搷搴�
+     */
+    @PostMapping("/generate-token")
+    public Map<String, Object> generateToken(@RequestParam String appId, @RequestParam(required = false) String userId) {
+        log.info("鐢熸垚Token锛孉ppId: {}, UserId: {}", appId, userId);
+        
+        try {
+            // 鐢熸垚Token
+            String token = TokenUtils.generateToken(appId, userId);
+            
+            Map<String, Object> response = new HashMap<>();
+            response.put("code", "200");
+            response.put("message", "Token鐢熸垚鎴愬姛");
+            response.put("data", Map.of(
+                "token", token,
+                "appId", appId,
+                "userId", userId,
+                "expiresIn", 24 * 60 * 60 // 24灏忔椂杩囨湡
+            ));
+            response.put("success", true);
+            
+            return response;
+        } catch (Exception e) {
+            log.error("鐢熸垚Token澶辫触", e);
+            
+            Map<String, Object> response = new HashMap<>();
+            response.put("code", "500");
+            response.put("message", "鐢熸垚Token澶辫触: " + e.getMessage());
+            response.put("data", null);
+            response.put("success", false);
+            
+            return response;
+        }
+    }
+
+
+
+    /**
+     * 鑾峰彇App璁よ瘉Token
+     *
+     * @param param 搴旂敤ID鍜屽簲鐢ㄥ瘑閽�
+     * @return 璁よ瘉Token
+     */
+    @ApiOperation("鑾峰彇App璁よ瘉Token")
+    @PostMapping("/getToken")
+    public CommonResponse getToken(@RequestBody AppAuthParam param) {
+        String appId = param.getAppId();
+        String appSecret = param.getAppSecret();
+
+        if (Cools.isEmpty(appId, appSecret)) {
+            return CommonResponse.error("AppId鍜孉ppSecret涓嶈兘涓虹┖");
+        }
+
+        boolean isValid = appAuthService.validateApp(appId, appSecret);
+        if (isValid) {
+            String token = appAuthService.generateAppToken(appId, appSecret);
+            return CommonResponse.ok()
+                    .setMsg("鑾峰彇Token鎴愬姛")
+                    .setData(token);
+        } else {
+            return CommonResponse.error("AppId鎴朅ppSecret鏃犳晥");
+        }
+    }
+
+//    /**
+//     * 楠岃瘉Token鐨勬帴鍙�
+//     *
+//     * @param token 瑕侀獙璇佺殑Token
+//     * @return Token楠岃瘉缁撴灉
+//     */
+//    @PostMapping("/validateToken")
+//    public Map<String, Object> validateToken(@RequestParam String token) {
+//        log.info("楠岃瘉Token: {}", token.substring(0, Math.min(10, token.length())) + "...");
+//
+//        boolean isValid = TokenUtils.validateToken(token);
+//
+//        Map<String, Object> response = new HashMap<>();
+//        response.put("code", "200");
+//        response.put("message", isValid ? "Token鏈夋晥" : "Token鏃犳晥");
+//        response.put("data", Map.of(
+//                "valid", isValid,
+//                "appId", isValid ? TokenUtils.getAppIdFromToken(token) : null,
+//                "userId", isValid ? TokenUtils.getUserIdFromToken(token) : null
+//        ));
+//        response.put("success", isValid);
+//
+//        return response;
+//    }
+}
\ No newline at end of file

--
Gitblit v1.9.1