diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/WebAutoConfiguration.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/WebAutoConfiguration.java index 9d1146e..13afaf7 100644 --- a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/WebAutoConfiguration.java +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/autoconfigure/WebAutoConfiguration.java @@ -72,6 +72,7 @@ public class WebAutoConfiguration extends WebMvcConfigurerAdapter { ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false); } + /******************************************编码部分*****************************************************/ /** @@ -80,7 +81,14 @@ public class WebAutoConfiguration extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { // 日志拦截器 - registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**"); + boolean classFound = false; + try { + classFound = null != Class.forName("org.springframework.security.core.context.SecurityContextHolder"); + } catch (ClassNotFoundException e) { + } + if (classFound) { + registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**"); + } super.addInterceptors(registry); } diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/interceptor/LogInterceptor.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/interceptor/LogInterceptor.java index 5f9d68d..c7a63af 100644 --- a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/interceptor/LogInterceptor.java +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/interceptor/LogInterceptor.java @@ -9,7 +9,6 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; /** * 访问日志拦截器,用于记录用户访问日志 @@ -26,24 +25,16 @@ public class LogInterceptor extends HandlerInterceptorAdapter { // 如登录用户访问,则记录其用户名(手机号) try { String username = ""; - try { - if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null - && SecurityContextHolder.getContext().getAuthentication().getName() != null) { - username = SecurityContextHolder.getContext().getAuthentication().getName(); - } - } catch (Exception e) { - LogUtil.exceptionLogger.error(e.getMessage(), e); + if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null + && SecurityContextHolder.getContext().getAuthentication().getName() != null) { + username = SecurityContextHolder.getContext().getAuthentication().getName(); } + String ip = ""; + ip = NetUtil.getIp(request); // 写日志 String uri = request.getRequestURI(); String userAgent = LogUtil.format(request.getHeader(HttpHeaders.USER_AGENT)); String xuserAgent = request.getHeader(RespsConsts.HEADER_X_USER_AGENT); - String ip = ""; - try { - ip = NetUtil.getIp(request); - } catch (IOException e) { - LogUtil.exceptionLogger.error(e.getMessage(), e); - } LogUtil.accessLogger.info("{};{};{};{};{}", uri, userAgent, xuserAgent, ip, username); } catch (Exception e) { diff --git a/yexuejc-springboot-base/src/test/resources/application.properties b/yexuejc-springboot-base/src/test/resources/application.properties index 499140d..9d3bff4 100644 --- a/yexuejc-springboot-base/src/test/resources/application.properties +++ b/yexuejc-springboot-base/src/test/resources/application.properties @@ -1,4 +1,4 @@ -server.port=8080 +server.port=8888 logging.level.root=info security.basic.enabled=false