修复系统启动监控定时开启无法获取request异常

This commit is contained in:
zxm 2018-01-09 11:36:21 +08:00
parent e431e1139a
commit 279678168f
3 changed files with 20 additions and 8 deletions

View File

@ -9,6 +9,10 @@ import com.len.util.IpUtil;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Date; import java.util.Date;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.AfterThrowing;
@ -17,6 +21,7 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
@ -45,12 +50,15 @@ public class LogAspect {
} }
private void addLog(JoinPoint jp,String text){ private void addLog(JoinPoint jp,String text){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
CurrentUser currentUser= ShiroUtil.getCurrentUse();
String ip= IpUtil.getIp(request);
Log.LOG_TYPE type=getType(jp); Log.LOG_TYPE type=getType(jp);
SysLog log=new SysLog(); SysLog log=new SysLog();
log.setIp(ip); RequestAttributes requestAttributes=RequestContextHolder.getRequestAttributes();
//一些系统监控
if(requestAttributes!=null){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip= IpUtil.getIp(request);
log.setIp(ip);
}
log.setCreateTime(new Date()); log.setCreateTime(new Date());
log.setType(type.toString()); log.setType(type.toString());
log.setText(text); log.setText(text);
@ -65,10 +73,13 @@ public class LogAspect {
} }
} }
log.setParam(buffer.toString()); log.setParam(buffer.toString());
if(currentUser!=null){ try {
CurrentUser currentUser = ShiroUtil.getCurrentUse();
log.setUserName(currentUser.getUsername()); log.setUserName(currentUser.getUsername());
logMapper.insert(log); }catch (UnavailableSecurityManagerException e){
} }
logMapper.insert(log);
} }
/** /**
@ -79,7 +90,7 @@ public class LogAspect {
@AfterThrowing(value="pointcut()",throwing="e") @AfterThrowing(value="pointcut()",throwing="e")
public void afterException(JoinPoint joinPoint,Exception e){ public void afterException(JoinPoint joinPoint,Exception e){
System.out.print("-----------afterException:"+e.getMessage()); System.out.print("-----------afterException:"+e.getMessage());
// addLog(joinPoint,getDesc(joinPoint)+e.getMessage()); addLog(joinPoint,getDesc(joinPoint)+e.getMessage());
} }

View File

@ -1,5 +1,6 @@
package com.len.core.quartz.CustomQuartz; package com.len.core.quartz.CustomQuartz;
import com.len.core.annotation.Log;
import com.len.entity.SysUser; import com.len.entity.SysUser;
import com.len.service.SysUserService; import com.len.service.SysUserService;
import com.len.service.impl.SysUserServiceImpl; import com.len.service.impl.SysUserServiceImpl;

View File

@ -60,7 +60,7 @@ public class JobTask {
/** /**
* 开启 * 开启
*/ */
//@Log(desc = "开启定时任务") @Log(desc = "开启定时任务")
public boolean startJob(SysJob job) { public boolean startJob(SysJob job) {
Scheduler scheduler = schedulerFactoryBean.getScheduler(); Scheduler scheduler = schedulerFactoryBean.getScheduler();
try { try {