1
0
mirror of https://gitee.com/incloudcode/yexuejc-springboot.git synced 2025-07-13 15:32:43 +08:00

修复bug

This commit is contained in:
yexuejc 2018-05-15 17:39:27 +08:00
parent 2c2c1a85a8
commit f1cdbf7549
3 changed files with 15 additions and 16 deletions

View File

@ -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) {
// 日志拦截器
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);
}

View File

@ -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);
}
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) {

View File

@ -1,4 +1,4 @@
server.port=8080
server.port=8888
logging.level.root=info
security.basic.enabled=false