박민혁 박민혁 08-08
240808 박민혁 토큰 인증 후 정보 가져오기 기능 추가
@d0cb6ec4c18020c5009d43f98281e0532d7f934c
src/main/java/com/takensoft/ai_lms/lms/auth/service/AuthService.java
--- src/main/java/com/takensoft/ai_lms/lms/auth/service/AuthService.java
+++ src/main/java/com/takensoft/ai_lms/lms/auth/service/AuthService.java
@@ -15,4 +15,6 @@
     String login(LoginDTO loginDTO) throws Exception;
 
     void logout(String token) throws Exception;
+
+    UserVO checkToken(String token) throws Exception;
 }
src/main/java/com/takensoft/ai_lms/lms/auth/service/Impl/AuthServiceImpl.java
--- src/main/java/com/takensoft/ai_lms/lms/auth/service/Impl/AuthServiceImpl.java
+++ src/main/java/com/takensoft/ai_lms/lms/auth/service/Impl/AuthServiceImpl.java
@@ -5,6 +5,7 @@
 import com.takensoft.ai_lms.lms.auth.dao.AuthDAO;
 import com.takensoft.ai_lms.lms.auth.dto.LoginDTO;
 import com.takensoft.ai_lms.lms.auth.service.AuthService;
+import com.takensoft.ai_lms.lms.auth.vo.UserAuthorVO;
 import com.takensoft.ai_lms.lms.auth.vo.UserVO;
 import lombok.RequiredArgsConstructor;
 import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
@@ -19,6 +20,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
@@ -109,6 +111,11 @@
         }
     }
 
+    /**
+     * @author  : 박민혁
+     * @since   : 2024.08.02
+     * 로그아웃 기능
+     */
     @Override
     public void logout(String token) throws Exception {
         try {
@@ -123,6 +130,31 @@
         }
     }
 
+    /**
+     * @author  : 박민혁
+     * @since   : 2024.08.07
+     * 토근 확인 기능
+     */
+    @Override
+    public UserVO checkToken(String token) throws Exception{
+        UserVO userVO = new UserVO();
+        try {
+            // 토큰에서 로그인 ID 추출
+            Boolean isExpire = jwtUtil.isExpired(token);
+
+            if (!isExpire) {
+                String userId = jwtUtil.getUsid(token);
+                userVO.setUsid(userId);
+                List<UserAuthorVO> author = jwtUtil.getRoles(token);
+                userVO.setAuthor(author);
+                return userVO;
+            } else {
+                throw new Exception("유효하지 않은 토큰");
+            }
+        } catch (Exception e) {
+            throw new Exception("토큰 확인 중 문제가 발생했습니다", e);
+        }
+    }
 }
 
 
src/main/java/com/takensoft/ai_lms/lms/auth/web/AuthController.java
--- src/main/java/com/takensoft/ai_lms/lms/auth/web/AuthController.java
+++ src/main/java/com/takensoft/ai_lms/lms/auth/web/AuthController.java
@@ -73,6 +73,7 @@
 
         try {
             String token = authService.login(loginDTO);
+
             response.addProperty("status", "success");
             response.addProperty("token", token);
             return gson.toJson(response);
@@ -83,6 +84,11 @@
         }
     }
 
+    /**
+     * @author  : 박민혁
+     * @since   : 2024.08.02
+     * 로그아웃 기능
+     */
     @PostMapping("/logout.json")
     @Operation(summary = "사용자 로그아웃")
     public String logout(@RequestHeader("Authorization") String token) {
@@ -102,4 +108,31 @@
         }
     }
 
+    @PostMapping("/validateToken.json")
+    @Operation(summary = "토큰 유효성 검사")
+    public String validateToken(@RequestHeader("Authorization") String token) {
+        Gson gson = new Gson();
+        JsonObject response = new JsonObject();
+
+        try {
+            // 토큰에서 사용자 ID 추출
+            UserVO result = authService.checkToken(token);
+
+            if (result != null) {
+                // 사용자 정보를 JSON으로 변환하여 반환
+                response.addProperty("status", "success");
+                response.add("userInfo", new Gson().toJsonTree(result));
+                return gson.toJson(response);
+            } else {
+                response.addProperty("status", "error");
+                response.addProperty("message", "유효하지 않은 사용자입니다.");
+                return gson.toJson(response);
+            }
+        } catch (Exception e) {
+            response.addProperty("status", "error");
+            response.addProperty("message", e.getMessage());
+            return gson.toJson(response);
+        }
+    }
+
 }
Add a comment
List