LifeCenterQueryLog.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.lemon.lifecenter.common;
  2. import java.lang.reflect.Field;
  3. import java.sql.Statement;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Properties;
  7. import java.util.regex.Matcher;
  8. import javax.servlet.http.HttpServletRequest;
  9. import org.apache.ibatis.executor.statement.StatementHandler;
  10. import org.apache.ibatis.mapping.BoundSql;
  11. import org.apache.ibatis.mapping.ParameterMapping;
  12. import org.apache.ibatis.plugin.Interceptor;
  13. import org.apache.ibatis.plugin.Intercepts;
  14. import org.apache.ibatis.plugin.Invocation;
  15. import org.apache.ibatis.plugin.Plugin;
  16. import org.apache.ibatis.plugin.Signature;
  17. import org.apache.ibatis.session.ResultHandler;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.web.context.request.RequestContextHolder;
  21. import org.springframework.web.context.request.ServletRequestAttributes;
  22. import com.lemon.lifecenter.dto.PrivateLogDTO;
  23. import com.lemon.lifecenter.service.PrivateLogService;
  24. import ch.qos.logback.classic.Logger;
  25. import java.sql.Connection;
  26. import java.sql.DriverManager;
  27. import java.sql.SQLException;
  28. import java.sql.Statement;
  29. import java.sql.*;
  30. import cubrid.sql.*; //a
  31. import cubrid.jdbc.driver.*;
  32. /*
  33. @Intercepts(@Signature(
  34. type=Executor.class,
  35. method="query",
  36. args= {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
  37. */
  38. @Intercepts({ @Signature(type = StatementHandler.class, method = "update", args = { Statement.class }),
  39. @Signature(type = StatementHandler.class, method = "query", args = { Statement.class, ResultHandler.class }) })
  40. public class LifeCenterQueryLog implements Interceptor {
  41. private final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
  42. private Connection con;
  43. private Statement stmt;
  44. @Override
  45. public Object intercept(Invocation invocation) throws Throwable {
  46. String method = invocation.getMethod().getName();
  47. StatementHandler handler = (StatementHandler) invocation.getTarget();
  48. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest();
  49. String sql = bindSql(handler); // SQL 추출
  50. String param = handler.getParameterHandler().getParameterObject()!=null ?
  51. handler.getParameterHandler().getParameterObject().toString() : "";
  52. Object session = request.getSession().getAttribute( "sesId" );
  53. String url = request.getRequestURI().toString();
  54. // 환자관리, 진료관리, 의료진관리
  55. if( url.contains( "/patient/" ) || url.contains( "/clinic/" ) || url.contains( "/staff/" ) ) {
  56. if( !url.contains( "/statistics/patient/" ) ) {
  57. if( session != null && !url.contains( "/mobile" ) ) {
  58. if( !param.contains( "com.lemon.lifecenter.dto.LoginDTO" ) ) {
  59. if( !sql.contains( "-- selectNowPathRoleCheckTotal" ) &&
  60. !sql.contains( "-- selectNowPathRoleCheckData" ) &&
  61. !sql.contains( "-- insertMenuAccessLog" ) &&
  62. !sql.contains( "-- selectSideMenuList" ) &&
  63. !sql.contains( "-- insertPrivateAccessLog" )
  64. ) {
  65. //System.err.println("★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★");
  66. //System.err.println(sql);
  67. try {
  68. //application.properties 에서 값을 들고옴
  69. String durl = PropertyUtil.getProperty("spring.datasource.url");
  70. String user = PropertyUtil.getProperty("spring.datasource.username");
  71. String passwd = PropertyUtil.getProperty("spring.datasource.password");
  72. con = DriverManager.getConnection(durl, user, passwd);
  73. // System.out.println("DB연결 성공");
  74. stmt = con.createStatement();
  75. // System.out.println("Statement객체 생성 성공");
  76. String sesName = LifeCenterSessionController.getSession( request, "sesName" );
  77. String sesCenterName = LifeCenterSessionController.getSession( request, "sesCenterName" );
  78. String logCenterName = !sesCenterName.equals( "" )? "["+sesCenterName+"] " : "";
  79. String sesId = LifeCenterSessionController.getSession( request, "sesId" );
  80. String insertQuery = ""
  81. + "INSERT "
  82. + " INTO private_access_log "
  83. + " ( id, accessor_detail, ip, create_date, full_url, processing_contents, processing_target ) "
  84. + "VALUES ( ?, ?, ?, NOW(), ?, ?, '')";
  85. PreparedStatement stmt = con.prepareStatement( insertQuery );
  86. stmt.setString(1, sesId );
  87. stmt.setString(2, logCenterName + sesName + " (" + sesId + ")" );
  88. stmt.setString(3, LifeCenterFunction.getRemoteAddr( request ));
  89. stmt.setString(4, LifeCenterFunction.getFullURL( request ) );
  90. stmt.setString(5, sql);
  91. stmt.executeUpdate();
  92. stmt.close();
  93. con.close();
  94. } catch (SQLException e) {
  95. System.out.println("DB연결 실패");
  96. System.out.print("사유 : " + e.getMessage());
  97. }
  98. //System.err.println("★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★");
  99. }
  100. }
  101. }
  102. }
  103. }
  104. return invocation.proceed();
  105. }
  106. @Override
  107. public Object plugin(Object target) {
  108. return Plugin.wrap(target, this);
  109. }
  110. @Override
  111. public void setProperties(Properties properties) {
  112. }
  113. /**
  114. * <pre>
  115. * bindSql
  116. *
  117. * <pre>
  118. *
  119. * @param boundSql
  120. * @param sql
  121. * @param param
  122. * @return
  123. * @throws NoSuchFieldException
  124. * @throws IllegalAccessException
  125. */
  126. @SuppressWarnings("rawtypes")
  127. private String bindSql(StatementHandler handler) throws NoSuchFieldException, IllegalAccessException {
  128. BoundSql boundSql = handler.getBoundSql();
  129. // 쿼리실행시 맵핑되는 파라미터를 구한다
  130. Object param = handler.getParameterHandler().getParameterObject();
  131. // 쿼리문을 가져온다(이 상태에서의 쿼리는 값이 들어갈 부분에 ?가 있다)
  132. String sql = boundSql.getSql();
  133. // 바인딩 파라미터가 없으면
  134. if (param == null) {
  135. sql = sql.replaceFirst("\\?", "''");
  136. return sql;
  137. }
  138. // 해당 파라미터의 클래스가 Integer, Long, Float, Double 클래스일 경우
  139. if (param instanceof Integer || param instanceof Long || param instanceof Float || param instanceof Double) {
  140. sql = sql.replaceFirst("\\?", param.toString());
  141. }
  142. // 해당 파라미터의 클래스가 String인 경우
  143. else if (param instanceof String) {
  144. sql = sql.replaceFirst("\\?", "'" + param + "'");
  145. }
  146. // 해당 파라미터의 클래스가 Map인 경우
  147. else if (param instanceof Map) {
  148. List<ParameterMapping> paramMapping = boundSql.getParameterMappings();
  149. for (ParameterMapping mapping : paramMapping) {
  150. String propValue = mapping.getProperty();
  151. Object value = ((Map) param).get(propValue);
  152. if (value == null) {
  153. continue;
  154. }
  155. if (value instanceof String) {
  156. // sql = sql.replaceFirst("\\?", "'" + value + "'");
  157. sql = sql.replaceFirst("\\?", "'"+Matcher.quoteReplacement(value.toString())+"'");
  158. } else {
  159. sql = sql.replaceFirst("\\?", value.toString());
  160. }
  161. }
  162. }
  163. // 해당 파라미터의 클래스가 사용자 정의 클래스인 경우
  164. else {
  165. List<ParameterMapping> paramMapping = boundSql.getParameterMappings();
  166. Class<? extends Object> paramClass = param.getClass();
  167. for (ParameterMapping mapping : paramMapping) {
  168. String propValue = mapping.getProperty();
  169. Field field = paramClass.getDeclaredField(propValue);
  170. field.setAccessible(true);
  171. Class<?> javaType = mapping.getJavaType();
  172. if (String.class == javaType) {
  173. String str = field.get(param) == null ? "" : field.get(param).toString();
  174. sql = sql.replaceFirst("\\?", "'"+Matcher.quoteReplacement(str)+"'");
  175. } else {
  176. sql = sql.replaceFirst("\\?", field.get(param).toString());
  177. }
  178. }
  179. }
  180. // return sql
  181. return sql;
  182. }
  183. }