package com.ycl.aspect; import com.ycl.annotation.LogSave; import com.ycl.bo.AdminUserDetails; import com.ycl.entity.user.UmsAdminLoginLog; import com.ycl.mapper.user.UmsAdminLoginLogMapper; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.Date; @Component @Aspect public class LogAspect { @Resource UmsAdminLoginLogMapper umsAdminLoginLogMapper; @Pointcut("@annotation(com.ycl.annotation.LogSave)") public void pointCut() { } @Before("pointCut()") public void handle(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); LogSave annotation = method.getAnnotation(LogSave.class); UmsAdminLoginLog loginLog = new UmsAdminLoginLog(); try { AdminUserDetails adminUserDetails = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); loginLog.setAdminId(adminUserDetails.getUserId()); } catch (Exception ex) { } loginLog.setCreateTime(new Date()); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); loginLog.setIp(request.getRemoteAddr()); String header = request.getHeader("user-agent"); if (header.contains("Windows NT")) { loginLog.setUserAgent("Web端"); } else if (header.contains("Android")) { loginLog.setUserAgent("Android端"); } else if (header.contains("iPhone")) { loginLog.setUserAgent("ios端"); } else if (header.contains("iPad")) { loginLog.setUserAgent("iPad端"); } else { loginLog.setUserAgent("Web端"); } if (annotation.operationType().equals("")) { loginLog.setOperationType("其他操作"); } else { loginLog.setOperationType(annotation.operationType()); } loginLog.setContain(annotation.contain()); umsAdminLoginLogMapper.insert(loginLog); } }