Browse Source

인터셉터 추가

songjunekeun 5 years ago
parent
commit
b8d74aa4bb

+ 14 - 0
.vscode/launch.json

@@ -0,0 +1,14 @@
+{
+    "configurations": [
+        {
+            "type": "java",
+            "name": "Spring Boot-ConsentApiApplication<consentApi>",
+            "request": "launch",
+            "cwd": "${workspaceFolder}",
+            "console": "internalConsole",
+            "mainClass": "com.dbs.consentServer.ConsentApiApplication",
+            "projectName": "consentApi",
+            "args": ""
+        }
+    ]
+}

BIN
consentApi/bin/main/com/dbs/consentServer/controller/HospitalSvc.class


+ 0 - 4
consentApi/bin/main/com/dbs/consentServer/handler/.gitignore

@@ -1,4 +0,0 @@
-/BaseException.class
-/GlobalExceptionHandler.class
-/HttpInterceptor.class
-/WebMvcConfig.class

BIN
consentApi/bin/main/com/dbs/consentServer/handler/GlobalExceptionHandler.class


BIN
consentApi/bin/main/com/dbs/consentServer/handler/HttpInterceptor.class


BIN
consentApi/bin/main/com/dbs/consentServer/handler/WebMvcConfig.class


+ 0 - 1
consentApi/bin/main/com/dbs/consentServer/util/.gitignore

@@ -1 +0,0 @@
-/CommonUtils.class

BIN
consentApi/bin/main/com/dbs/consentServer/util/CommonUtils.class


+ 32 - 0
consentApi/src/main/java/com/dbs/consentServer/handler/GlobalExceptionHandler.java

@@ -0,0 +1,32 @@
+package com.dbs.consentServer.handler;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestController;
+
+@ControllerAdvice  
+@RestController
+public class GlobalExceptionHandler {
+
+  private final Logger logger = LoggerFactory.getLogger(this.getClass());
+ 
+  @ExceptionHandler(Exception.class)
+  protected List<Map<String, String>> handleException(Exception e) {
+    ArrayList<Map<String, String>> arResponse = new ArrayList<Map<String, String>>();
+    HashMap<String, String> response = new HashMap<String, String>();
+    response.put("code", "99");
+    response.put("errorMsg", e.getMessage());
+    arResponse.add(response);
+    
+    return arResponse;
+  }
+}

+ 60 - 0
consentApi/src/main/java/com/dbs/consentServer/handler/HttpInterceptor.java

@@ -0,0 +1,60 @@
+package com.dbs.consentServer.handler;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import com.dbs.consentServer.util.CommonUtils;
+
+import ch.qos.logback.classic.pattern.Util;
+
+/***
+ * http start, end point
+ * @author songjunekeun
+ */
+@Component
+public class HttpInterceptor extends HandlerInterceptorAdapter {
+
+  private final Logger logger = LoggerFactory.getLogger(this.getClass());
+  
+  /***
+   * http start method
+   * @author songjunekeun
+   */
+  @Override
+  public boolean preHandle(HttpServletRequest request,
+               HttpServletResponse response,
+               Object handler) {
+    logger.info("Before Method Time : " + CommonUtils.logTime());
+    return true;
+  }
+
+  /***
+   * http Executed method
+   * @author songjunekeun
+   */
+  @Override
+  public void postHandle( HttpServletRequest request,
+              HttpServletResponse response,
+              Object handler,
+              ModelAndView modelAndView) {
+    logger.info("Method Executed Time : " + CommonUtils.logTime());
+  }
+  
+  /***
+   * http Completed method
+   * @author songjunekeun
+   */
+  @Override
+  public void afterCompletion(HttpServletRequest request,
+                HttpServletResponse response, 
+                Object handler, 
+                Exception ex) {
+    logger.info("Method Completed Time : " + CommonUtils.logTime());
+  }
+}

+ 27 - 0
consentApi/src/main/java/com/dbs/consentServer/handler/WebMvcConfig.java

@@ -0,0 +1,27 @@
+package com.dbs.consentServer.handler;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/***
+ * http start, end point setting
+ * @author songjunekeun
+ */
+@Configuration
+public class WebMvcConfig implements WebMvcConfigurer {
+
+  @Autowired
+  @Qualifier(value = "httpInterceptor")
+  private HandlerInterceptor interceptor;
+
+  @Override
+  public void addInterceptors(InterceptorRegistry registry) {
+    registry.addInterceptor(interceptor)
+        .addPathPatterns("/**")
+        .excludePathPatterns("/user/**");
+  }
+}

+ 15 - 0
consentApi/src/main/java/com/dbs/consentServer/util/CommonUtils.java

@@ -0,0 +1,15 @@
+package com.dbs.consentServer.util;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class CommonUtils {
+
+    public static String logTime() {
+        SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss");
+        Date date = new Date();
+        String strDate = format.format(date);
+        return strDate;
+      }
+    
+}