package com.landtool.lanbase.common.aspect;
|
|
import java.lang.reflect.Method;
|
|
import javax.servlet.http.HttpServletRequest;
|
import org.apache.shiro.SecurityUtils;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import com.landtool.lanbase.common.utils.HttpContextUtils;
|
import com.landtool.lanbase.common.utils.IPUtils;
|
import com.landtool.lanbase.config.LoginConfigProperties;
|
import com.landtool.lanbase.modules.log.entity.LogLogininfo;
|
import com.landtool.lanbase.modules.log.service.LogLogininfoService;
|
import com.landtool.lanbase.modules.org.entity.OrgUser;
|
import com.landtool.lanbase.modules.org.redis.OrgUserRedis;
|
import com.landtool.lanbase.modules.org.service.OrgUserService;
|
|
import eu.bitwalker.useragentutils.Browser;
|
import eu.bitwalker.useragentutils.UserAgent;
|
import eu.bitwalker.useragentutils.Version;
|
|
|
@Aspect
|
@Component
|
public class LogLogininfoAspect {
|
@Autowired
|
private LogLogininfoService logLogininfoService;
|
|
@Autowired
|
private OrgUserService orgUserService;
|
|
@Autowired
|
private LoginConfigProperties loginConfig;
|
|
@Autowired
|
private OrgUserRedis orgUserRedis;
|
|
|
@Pointcut("@annotation(com.landtool.lanbase.common.annotation.LogLogininfo)")
|
public void logPointCut() {
|
|
}
|
|
@Around("logPointCut()")
|
public Object around(ProceedingJoinPoint point) throws Throwable {
|
long beginTime = System.currentTimeMillis();
|
//执行方法
|
Object result = point.proceed();
|
//执行时长(毫秒)
|
long time = System.currentTimeMillis() - beginTime;
|
// System.out.println("time"+time);
|
//保存日志
|
saveLogLogininfo(point, time);
|
|
return result;
|
}
|
|
private void saveLogLogininfo(ProceedingJoinPoint joinPoint, long time) {
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
Method method = signature.getMethod();
|
LogLogininfo logLogininfo = new LogLogininfo();
|
com.landtool.lanbase.common.annotation.LogLogininfo log = method.getAnnotation(com.landtool.lanbase.common.annotation.LogLogininfo.class);
|
if(log != null){
|
//注解上的描述
|
if(log.value().equals("登录")){
|
logLogininfo.setLogintype((long) 1);
|
}else if(log.value().equals("登出")){
|
logLogininfo.setLogintype((long) 2);
|
logLogininfo.setRstatus((long) 0);
|
}else{
|
logLogininfo.setLogintype((long) 3);
|
}
|
}
|
logLogininfo.setAppfullname(loginConfig.getAppFullName());
|
|
logLogininfo.setAppid(loginConfig.getAppId());
|
|
|
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
|
String username = null;
|
String methodName = method.getName();
|
//登录入口并不能通过SecurityUtils获取用户名,需要特殊处理
|
if(methodName.indexOf("login")>-1){
|
// 通过cas登陆
|
if(methodName.indexOf("caslogin")>-1){
|
if(request.getRemoteUser() != null) {
|
username = request.getRemoteUser();
|
}
|
}
|
// 普通登陆
|
if(methodName.equalsIgnoreCase("login")){
|
Object[] args=joinPoint.getArgs();
|
if(args[0].toString() != null) {
|
username = args[0].toString();
|
}
|
}
|
if(username !=null && username !=""){
|
OrgUser user = orgUserService.queryByUserName(username);
|
logLogininfo.setLoginname(username);
|
if(user !=null){
|
//登陆成功
|
logLogininfo.setUserid(user.getUserid());
|
logLogininfo.setRstatus((long) 0);
|
}else{
|
//未登陆或或用戶不存在系統時不寫日志
|
return ;
|
}
|
}else{
|
//登陆失败
|
logLogininfo.setLoginname("未知");
|
logLogininfo.setUserid((long) -1);
|
logLogininfo.setRstatus((long) 1);
|
}
|
}else{
|
//非登陆入口的情况,比如logout
|
username = ((OrgUser) SecurityUtils.getSubject().getPrincipal()).getLoginname();
|
logLogininfo.setLoginname(username);
|
|
Long userid = ((OrgUser) SecurityUtils.getSubject().getPrincipal()).getUserid();
|
logLogininfo.setUserid(userid);
|
logLogininfo.setRstatus((long) 0);
|
}
|
|
|
//获取浏览器版本
|
Browser bro=UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
|
Version version=bro.getVersion(request.getHeader("User-Agent"));
|
String info=bro.getName()+"/"+version.getVersion();
|
logLogininfo.setBroserversion(info);
|
//设置IP地址
|
logLogininfo.setLoginip(IPUtils.getIpAddr(request));
|
|
//保存系统日志
|
logLogininfoService.save(logLogininfo);
|
}
|
}
|