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) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
server.port=8080
|
||||
server.port=8888
|
||||
logging.level.root=info
|
||||
|
||||
security.basic.enabled=false
|
||||
|
Loading…
x
Reference in New Issue
Block a user