From 424f53816f78f5e4fe25e3cc3a359923600ec879 Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Wed, 22 Apr 2020 11:30:18 +0800 Subject: [PATCH] Fix typo: LogIntercreptorHandler -> LogInterceptorHandler --- .../cc/config/CSKeFuWebAppConfigurer.java | 85 ++++++++++--------- ...andler.java => LogInterceptorHandler.java} | 70 +++++++-------- 2 files changed, 78 insertions(+), 77 deletions(-) rename contact-center/app/src/main/java/com/chatopera/cc/interceptor/{LogIntercreptorHandler.java => LogInterceptorHandler.java} (65%) diff --git a/contact-center/app/src/main/java/com/chatopera/cc/config/CSKeFuWebAppConfigurer.java b/contact-center/app/src/main/java/com/chatopera/cc/config/CSKeFuWebAppConfigurer.java index df81be53..e0b44b8d 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/config/CSKeFuWebAppConfigurer.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/config/CSKeFuWebAppConfigurer.java @@ -1,40 +1,45 @@ -/* - * Copyright (C) 2017 优客服-多渠道客服系统 - * Modifications copyright (C) 2018-2019 Chatopera Inc, - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.chatopera.cc.config; - -import com.chatopera.cc.interceptor.CrossInterceptorHandler; -import com.chatopera.cc.interceptor.LogIntercreptorHandler; -import com.chatopera.cc.interceptor.UserInterceptorHandler; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -@Configuration -public class CSKeFuWebAppConfigurer - extends WebMvcConfigurerAdapter { - - @Override - public void addInterceptors(InterceptorRegistry registry) { - // 多个拦截器组成一个拦截器链 - // addPathPatterns 用于添加拦截规则 - // excludePathPatterns 用户排除拦截 - registry.addInterceptor(new UserInterceptorHandler()).addPathPatterns("/**").excludePathPatterns("/login.html","/im/**","/res/image*","/res/file*","/cs/**"); - registry.addInterceptor(new CrossInterceptorHandler()).addPathPatterns("/**"); - registry.addInterceptor(new LogIntercreptorHandler()).addPathPatterns("/**"); - super.addInterceptors(registry); - } -} \ No newline at end of file +/* + * Copyright (C) 2017 优客服-多渠道客服系统 + * Modifications copyright (C) 2018-2019 Chatopera Inc, + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.chatopera.cc.config; + +import com.chatopera.cc.interceptor.CrossInterceptorHandler; +import com.chatopera.cc.interceptor.LogInterceptorHandler; +import com.chatopera.cc.interceptor.UserInterceptorHandler; +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Configuration; +import org.springframework.lang.NonNull; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@RequiredArgsConstructor +public class CSKeFuWebAppConfigurer implements WebMvcConfigurer { + @NonNull + private final LogInterceptorHandler logInterceptorHandler; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 多个拦截器组成一个拦截器链 + // addPathPatterns 用于添加拦截规则 + // excludePathPatterns 用户排除拦截 + registry.addInterceptor(new UserInterceptorHandler()) + .addPathPatterns("/**") + .excludePathPatterns("/login.html", "/im/**", "/res/image*", "/res/file*", "/cs/**"); + registry.addInterceptor(new CrossInterceptorHandler()).addPathPatterns("/**"); + registry.addInterceptor(logInterceptorHandler).addPathPatterns("/**"); + } +} diff --git a/contact-center/app/src/main/java/com/chatopera/cc/interceptor/LogIntercreptorHandler.java b/contact-center/app/src/main/java/com/chatopera/cc/interceptor/LogInterceptorHandler.java similarity index 65% rename from contact-center/app/src/main/java/com/chatopera/cc/interceptor/LogIntercreptorHandler.java rename to contact-center/app/src/main/java/com/chatopera/cc/interceptor/LogInterceptorHandler.java index fd318956..4d2b25b8 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/interceptor/LogIntercreptorHandler.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/interceptor/LogInterceptorHandler.java @@ -24,9 +24,11 @@ import com.chatopera.cc.model.RequestLog; import com.chatopera.cc.model.User; import com.chatopera.cc.persistence.repository.RequestLogRepository; import com.chatopera.cc.util.Menu; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.ModelAndView; @@ -41,24 +43,24 @@ import java.util.Enumeration; * * @author admin */ -public class LogIntercreptorHandler implements org.springframework.web.servlet.HandlerInterceptor { +@Slf4j +@Component +@RequiredArgsConstructor +public class LogInterceptorHandler implements org.springframework.web.servlet.HandlerInterceptor { - private final static Logger logger = LoggerFactory.getLogger(LogIntercreptorHandler.class); - - private static RequestLogRepository requestLogRes; + @NonNull + private final RequestLogRepository requestLogRes; @Override - public void afterCompletion(HttpServletRequest request, - HttpServletResponse arg1, Object arg2, Exception arg3) - throws Exception { + public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, + ModelAndView modelAndView) { + if (!(handler instanceof HandlerMethod)) { + log.debug("Request {} invoked with handler {}", request.getServletPath(), handler.getClass()); + return; + } - } - - @Override - public void postHandle(HttpServletRequest request, HttpServletResponse response, - Object arg2, ModelAndView arg3) throws Exception { - HandlerMethod handlerMethod = (HandlerMethod) arg2; - Object hander = handlerMethod.getBean(); + HandlerMethod handlerMethod = (HandlerMethod) handler; + Object actualHandler = handlerMethod.getBean(); RequestMapping obj = handlerMethod.getMethod().getAnnotation(RequestMapping.class); if (StringUtils.isNotBlank(request.getRequestURI()) && !(request.getRequestURI().startsWith("/message/ping") || request.getRequestURI().startsWith("/res/css") || request.getRequestURI().startsWith("/error") || request.getRequestURI().startsWith("/im/"))) { RequestLog log = new RequestLog(); @@ -69,11 +71,9 @@ public class LogIntercreptorHandler implements org.springframework.web.servlet.H } log.setMethodname(handlerMethod.toString()); log.setIp(request.getRemoteAddr()); - if (hander != null) { - log.setClassname(hander.getClass().toString()); - if (hander instanceof Handler && ((Handler) hander).getStartTime() != 0) { - log.setQuerytime(System.currentTimeMillis() - ((Handler) hander).getStartTime()); - } + log.setClassname(actualHandler.getClass().toString()); + if (actualHandler instanceof Handler && ((Handler) actualHandler).getStartTime() != 0) { + log.setQuerytime(System.currentTimeMillis() - ((Handler) actualHandler).getStartTime()); } log.setUrl(request.getRequestURI()); @@ -87,11 +87,11 @@ public class LogIntercreptorHandler implements org.springframework.web.servlet.H log.setUsermail(user.getEmail()); log.setOrgi(user.getOrgi()); } - StringBuffer str = new StringBuffer(); + StringBuilder str = new StringBuilder(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String paraName = names.nextElement(); - if (paraName.indexOf("password") >= 0) { + if (paraName.contains("password")) { str.append(paraName).append("=").append(MainUtils.encryption(request.getParameter(paraName))).append(","); } else { str.append(paraName).append("=").append(request.getParameter(paraName)).append(","); @@ -106,25 +106,21 @@ public class LogIntercreptorHandler implements org.springframework.web.servlet.H } log.setParameters(str.toString()); - getRequestLogRes().save(log); + requestLogRes.save(log); } } @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, - Object arg2) { - HandlerMethod handlerMethod = (HandlerMethod) arg2; - Object hander = handlerMethod.getBean(); - if (hander instanceof Handler) { - ((Handler) hander).setStartTime(System.currentTimeMillis()); + public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) { + if (!(handler instanceof HandlerMethod)) { + log.debug("Request {} invoked with handler {}", request.getServletPath(), handler.getClass()); + return true; + } + HandlerMethod handlerMethod = (HandlerMethod) handler; + Object actualHandler = handlerMethod.getBean(); + if (actualHandler instanceof Handler) { + ((Handler) actualHandler).setStartTime(System.currentTimeMillis()); } return true; } - - private static RequestLogRepository getRequestLogRes() { - if (requestLogRes == null) { - requestLogRes = MainContext.getContext().getBean(RequestLogRepository.class); - } - return requestLogRes; - } }