通过注解的方式记录网站登录信息
通过注解的方式记录网站登录信息
在applicationContext.xml
中添加以下代码配置
<!-- AOP -->
<aop:config>
<aop:pointcut id="controllerMethods"
expression="execution(* xx.xxxxx.controller.*.*(..))" />
<!--事务通知-->
<!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="controllerMethods" /> -->
</aop:config>
创建domain类
import javax.persistence.Column;
import javax.persistence.Table;
/**
* @author : xiao
* create at: 2019-09-03 11:19
* @description:
*/
@Table(name = "SysLoginInfo")
public class SysLoginInfo extends BaseEntity {
private String ipaddr;//ip地址
private String rcshd;//访问活动
@Column(name = "isNew")
private String isNew;//是否新用户
private String os;//操作系统
public String getIpaddr() {
return ipaddr;
}
public void setIpaddr(String ipaddr) {
this.ipaddr = ipaddr;
}
public String getRcshd() {
return rcshd;
}
public void setRcshd(String rcshd) {
this.rcshd = rcshd;
}
public String getIsNew() {
return isNew;
}
public void setIsNew(String isNew) {
this.isNew = isNew;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
}
继承的BaseEntity类
import com.fasterxml.jackson.annotation.JsonFormat;
import javax.persistence.Column;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;
/**
* @author : xiao
* create at: 2019-09-02 09:29
* @description:基础实体类
*/
public class BaseEntity implements Serializable {
private static final long serialVersionUID =1L;
/** 表id **/
@Id
private String id;
/** 手机号 **/
private String phone;
/** 创建时间 **/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@Column(name = "createTime")
private Date createTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
创建自定义注解 LoginInfo.java
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
@Target({METHOD, TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface LoginInfo {
//访问活动名称
String rcshd ();
}
该方法使用的后置通知,需要环绕通知或者前置通知的修改以下实现类
创建注解实现方法类
import com.mchange.v1.util.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
/**
* @author : xiaozihan
* create at: 2019-09-03 15:05
* @description:
*/
@Component
@Aspect
public class RcsLoginInfo {
//注解的位置
@Pointcut("@annotation(xx.xxxx.xx.LoginInfo)")
public void method(){
}
@After("method()")
public void after(JoinPoint joinPoint){
System.err.println("开始记录登录日志");
}
@AfterReturning("method()")
public void afterReturning(JoinPoint joinPoint){
// 在切面方法里面获取一个request,
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 通过springAOP切面JoinPoint类对象,获取该类,或者该方法,或者该方法的参数
Class<? extends Object> clazz = joinPoint.getTarget().getClass();
// String controllerOperation = clazz.getName();
// if(clazz.isAnnotationPresent(LoginInfo.class)){
// // 当前controller操作的名称
// controllerOperation = clazz.getAnnotation(LoginInfo.class).rcshd();
// }
// 获取当前方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
// clazz类下的所有方法
Method[] methods = clazz.getDeclaredMethods();
String methodOperation = "";
for (Method m : methods) {
if(m.equals(method)){
methodOperation = m.getName();
if(m.isAnnotationPresent(LoginInfo.class)){
methodOperation = m.getAnnotation(LoginInfo.class).rcshd();
}
}
}
/**
自己实现方法
**/
// String userAgent = request.getHeader("user-agent").toLowerCase();
//
// String os ="";
// // 获取客户端操作系统
// if(userAgent.indexOf("android") != -1){
// os = "android";
// }else if(userAgent.indexOf("iphone") != -1){
// os = "iphone";
// }else{
// os="其他";
// }
//
// String phone = getFieldsName(joinPoint);
//
// if(!StringUtil.isBlank(phone)){
// // 封装对象
// SysLoginInfo logininfor = new SysLoginInfo();
// logininfor.setPhone(phone);
// logininfor.setIpaddr(request.getRemoteHost());
// logininfor.setIsNew("1");
// logininfor.setRcshd(methodOperation);
// logininfor.setOs(os);
// logininfor.setCreateTime(new Date());
// // 插入数据
// sysLoginInfoDao.insert(logininfor);
// }
}
//获取方法请求参数值
private static String getFieldsName(JoinPoint joinPoint) {
//这里获取到所有的参数值的数组
Object[] args = joinPoint.getArgs();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
//通过这获取到方法的所有参数名称的字符串数组
String[] parameterNames = methodSignature.getParameterNames();
try {
//通过你需要获取的参数名称的下标获取到对应的值
int phone = ArrayUtils.indexOf(parameterNames, "phone");
if (phone != -1) {
String phoneStr = (String) args[phone];
return phoneStr;
}
} catch (Throwable throwable) {
}
return null;
}
}