mirror of
https://gitee.com/incloudcode/yexuejc-springboot.git
synced 2025-07-13 15:32:43 +08:00
修复bug
This commit is contained in:
parent
2c2c1a85a8
commit
f1cdbf7549
@ -72,6 +72,7 @@ public class WebAutoConfiguration extends WebMvcConfigurerAdapter {
|
|||||||
ContentNegotiationConfigurer configurer) {
|
ContentNegotiationConfigurer configurer) {
|
||||||
configurer.favorPathExtension(false);
|
configurer.favorPathExtension(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************编码部分*****************************************************/
|
/******************************************编码部分*****************************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -80,7 +81,14 @@ public class WebAutoConfiguration extends WebMvcConfigurerAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
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);
|
super.addInterceptors(registry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 访问日志拦截器,用于记录用户访问日志
|
* 访问日志拦截器,用于记录用户访问日志
|
||||||
@ -26,24 +25,16 @@ public class LogInterceptor extends HandlerInterceptorAdapter {
|
|||||||
// 如登录用户访问,则记录其用户名(手机号)
|
// 如登录用户访问,则记录其用户名(手机号)
|
||||||
try {
|
try {
|
||||||
String username = "";
|
String username = "";
|
||||||
try {
|
if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null
|
||||||
if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null
|
&& SecurityContextHolder.getContext().getAuthentication().getName() != null) {
|
||||||
&& SecurityContextHolder.getContext().getAuthentication().getName() != null) {
|
username = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||||
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 uri = request.getRequestURI();
|
||||||
String userAgent = LogUtil.format(request.getHeader(HttpHeaders.USER_AGENT));
|
String userAgent = LogUtil.format(request.getHeader(HttpHeaders.USER_AGENT));
|
||||||
String xuserAgent = request.getHeader(RespsConsts.HEADER_X_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);
|
LogUtil.accessLogger.info("{};{};{};{};{}", uri, userAgent, xuserAgent, ip, username);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
server.port=8080
|
server.port=8888
|
||||||
logging.level.root=info
|
logging.level.root=info
|
||||||
|
|
||||||
security.basic.enabled=false
|
security.basic.enabled=false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user