1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

Fix typo: LogIntercreptorHandler -> LogInterceptorHandler

This commit is contained in:
dengchao@xgtl 2020-04-22 11:30:18 +08:00
parent 45ace80228
commit 424f53816f
2 changed files with 78 additions and 77 deletions

View File

@ -1,40 +1,45 @@
/* /*
* Copyright (C) 2017 优客服-多渠道客服系统 * Copyright (C) 2017 优客服-多渠道客服系统
* Modifications copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com> * Modifications copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.chatopera.cc.config; package com.chatopera.cc.config;
import com.chatopera.cc.interceptor.CrossInterceptorHandler; import com.chatopera.cc.interceptor.CrossInterceptorHandler;
import com.chatopera.cc.interceptor.LogIntercreptorHandler; import com.chatopera.cc.interceptor.LogInterceptorHandler;
import com.chatopera.cc.interceptor.UserInterceptorHandler; import com.chatopera.cc.interceptor.UserInterceptorHandler;
import org.springframework.context.annotation.Configuration; import lombok.RequiredArgsConstructor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@Configuration import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class CSKeFuWebAppConfigurer
extends WebMvcConfigurerAdapter { @Configuration
@RequiredArgsConstructor
@Override public class CSKeFuWebAppConfigurer implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) { @NonNull
// 多个拦截器组成一个拦截器链 private final LogInterceptorHandler logInterceptorHandler;
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截 @Override
registry.addInterceptor(new UserInterceptorHandler()).addPathPatterns("/**").excludePathPatterns("/login.html","/im/**","/res/image*","/res/file*","/cs/**"); public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CrossInterceptorHandler()).addPathPatterns("/**"); // 多个拦截器组成一个拦截器链
registry.addInterceptor(new LogIntercreptorHandler()).addPathPatterns("/**"); // addPathPatterns 用于添加拦截规则
super.addInterceptors(registry); // excludePathPatterns 用户排除拦截
} registry.addInterceptor(new UserInterceptorHandler())
} .addPathPatterns("/**")
.excludePathPatterns("/login.html", "/im/**", "/res/image*", "/res/file*", "/cs/**");
registry.addInterceptor(new CrossInterceptorHandler()).addPathPatterns("/**");
registry.addInterceptor(logInterceptorHandler).addPathPatterns("/**");
}
}

View File

@ -24,9 +24,11 @@ import com.chatopera.cc.model.RequestLog;
import com.chatopera.cc.model.User; import com.chatopera.cc.model.User;
import com.chatopera.cc.persistence.repository.RequestLogRepository; import com.chatopera.cc.persistence.repository.RequestLogRepository;
import com.chatopera.cc.util.Menu; import com.chatopera.cc.util.Menu;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.springframework.lang.NonNull;
import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
@ -41,24 +43,24 @@ import java.util.Enumeration;
* *
* @author admin * @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); @NonNull
private final RequestLogRepository requestLogRes;
private static RequestLogRepository requestLogRes;
@Override @Override
public void afterCompletion(HttpServletRequest request, public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler,
HttpServletResponse arg1, Object arg2, Exception arg3) ModelAndView modelAndView) {
throws Exception { if (!(handler instanceof HandlerMethod)) {
log.debug("Request {} invoked with handler {}", request.getServletPath(), handler.getClass());
return;
}
} HandlerMethod handlerMethod = (HandlerMethod) handler;
Object actualHandler = handlerMethod.getBean();
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object arg2, ModelAndView arg3) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) arg2;
Object hander = handlerMethod.getBean();
RequestMapping obj = handlerMethod.getMethod().getAnnotation(RequestMapping.class); 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/"))) { 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(); RequestLog log = new RequestLog();
@ -69,11 +71,9 @@ public class LogIntercreptorHandler implements org.springframework.web.servlet.H
} }
log.setMethodname(handlerMethod.toString()); log.setMethodname(handlerMethod.toString());
log.setIp(request.getRemoteAddr()); log.setIp(request.getRemoteAddr());
if (hander != null) { log.setClassname(actualHandler.getClass().toString());
log.setClassname(hander.getClass().toString()); if (actualHandler instanceof Handler && ((Handler) actualHandler).getStartTime() != 0) {
if (hander instanceof Handler && ((Handler) hander).getStartTime() != 0) { log.setQuerytime(System.currentTimeMillis() - ((Handler) actualHandler).getStartTime());
log.setQuerytime(System.currentTimeMillis() - ((Handler) hander).getStartTime());
}
} }
log.setUrl(request.getRequestURI()); log.setUrl(request.getRequestURI());
@ -87,11 +87,11 @@ public class LogIntercreptorHandler implements org.springframework.web.servlet.H
log.setUsermail(user.getEmail()); log.setUsermail(user.getEmail());
log.setOrgi(user.getOrgi()); log.setOrgi(user.getOrgi());
} }
StringBuffer str = new StringBuffer(); StringBuilder str = new StringBuilder();
Enumeration<String> names = request.getParameterNames(); Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) { while (names.hasMoreElements()) {
String paraName = names.nextElement(); String paraName = names.nextElement();
if (paraName.indexOf("password") >= 0) { if (paraName.contains("password")) {
str.append(paraName).append("=").append(MainUtils.encryption(request.getParameter(paraName))).append(","); str.append(paraName).append("=").append(MainUtils.encryption(request.getParameter(paraName))).append(",");
} else { } else {
str.append(paraName).append("=").append(request.getParameter(paraName)).append(","); 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()); log.setParameters(str.toString());
getRequestLogRes().save(log); requestLogRes.save(log);
} }
} }
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) {
Object arg2) { if (!(handler instanceof HandlerMethod)) {
HandlerMethod handlerMethod = (HandlerMethod) arg2; log.debug("Request {} invoked with handler {}", request.getServletPath(), handler.getClass());
Object hander = handlerMethod.getBean(); return true;
if (hander instanceof Handler) { }
((Handler) hander).setStartTime(System.currentTimeMillis()); HandlerMethod handlerMethod = (HandlerMethod) handler;
Object actualHandler = handlerMethod.getBean();
if (actualHandler instanceof Handler) {
((Handler) actualHandler).setStartTime(System.currentTimeMillis());
} }
return true; return true;
} }
private static RequestLogRepository getRequestLogRes() {
if (requestLogRes == null) {
requestLogRes = MainContext.getContext().getBean(RequestLogRepository.class);
}
return requestLogRes;
}
} }