SpringMVC记录⽤户的操作⽇志⽤spring mvc 做⽤户的操作⽇志记录基本有两种⽅式
1.利⽤aop做(推荐)
2.利⽤做(不推荐)
AOP做操作⽇志记录基本步骤
1.数据库创建⽇志表(定义UserOperateLog实体类)
@Entity
@Table(name = "USER_OPERATE_LOG")
public class UserOperateLog implements java.io.Serializable {
/** 版本号 */
private static final long serialVersionUID = 3322584679786620277L;
/** */
private Integer id;
/** ⽤户id */
private String custId;
/** ⽤户名 */
private String custName;
/** 真实姓名 */
private String realName;
/** 操作模块 */
private String moduleName;
/** 操作描述 */
private String operateDesc;
/
** 操作IP */
private String ipAddress;
/** 操作时间 */
private Date createTime;
/**
* 获取
*
* @return
*/
@Id
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator", strategy = "native")
@Column(name = "ID", unique = true, nullable = false, length = 10)
public Integer getId() {
return this.id;
}
/**
* 设置
*
* @param id
*/
public void setId(Integer id) {
this.id = id;
this.id = id;
}
/**
* 获取⽤户id
*
* @return ⽤户id
*/
@Column(name = "CUST_ID", nullable = true, length = 255)
public String getCustId() {
return this.custId;
}
/**
* 设置⽤户id
*
* @param custId
* ⽤户id
*/
public void setCustId(String custId) {
this.custId = custId;
}
/**
* 获取⽤户名
*
* @return ⽤户名
*/
@Column(name = "CUST_NAME", nullable = true, length = 255) public String getCustName() {
return this.custName;
}
/**
* 设置⽤户名
*
* @param custName
* ⽤户名
*/
public void setCustName(String custName) {
this.custName = custName;
}
/**
* 获取真实姓名
*
* @return 真实姓名
*/
@Column(name = "REAL_NAME", nullable = true, length = 255) public String getRealName() {
alName;
}
/**
* 设置真实姓名
*
* @param realName
* 真实姓名
*/
public void setRealName(String realName) {
}
/
**
* 获取操作模块
*
*
* @return 操作模块
*/
@Column(name = "MODULE_NAME", nullable = true, length = 255) public String getModuleName() {
duleName;
}
/**
* 设置操作模块
*
* @param moduleName
* 操作模块
*/
public void setModuleName(String moduleName) {
}
/**
* 获取操作描述
*
* @return 操作描述
*/
@Column(name = "OPERATE_DESC", nullable = true, length = 255) public String getOperateDesc() {
return this.operateDesc;
}
/**
* 设置操作描述
*
* @param operateDesc
* 操作描述
*/
public void setOperateDesc(String operateDesc) {
this.operateDesc = operateDesc;
}
/**
* 获取操作IP
*
* @return 操作IP
*/
@Column(name = "IP_ADDRESS", nullable = true, length = 255)
public String getIpAddress() {
return this.ipAddress;
}
/
**
* 设置操作IP
*
* @param ipAddress
* 操作IP
*/
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
/**
* 获取操作时间
*
* @return 操作时间
*/
@Column(name = "CREATE_TIME", nullable = true)
public Date getCreateTime() {
ateTime;
ateTime;
}
/**
* 设置操作时间
*
* @param createTime
* 操作时间
*/
public void setCreateTime(Date createTime) {
}
}
2.⾃定义⽤户操作注解(定义UserOperate注解)spring到底是干啥的
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface UserOperate {
//模块名
String moduleName() default "";
//操作内容
String option() default "";
}
3.spring mvc配置⽂件中配置
<aop:aspectj-autoproxy proxy-target-class="true"/>
spring核⼼控制器配置⽂件配置
<tx:method name="interceptorApplogicA" propagation="REQUIRES_NEW"/>
当注解打在controller层时,注意事务是否有冲突,导致原先可以对数据库进⾏写操作的代码现在⽆法对数据库进⾏写操作。解决⽅法建议修改事务的拦截配置如下:
<!-- 事务拦截配置 -->
<aop:config expose-proxy="true">
<aop:pointcut id="txPointcut"
expression=" !execution(* ller..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
4.spring 切⽚定义(定义LogAspect切⽚)
/**
* 处理⽤户操作⽇志切⾯类
* @author HuYa
*
*/
@Aspect
@Component
public class LogAspect {
static UserInfo anonyUser = new UserInfo();
static {
anonyUser.setCustId("-1");
anonyUser.setRealName("游客");
anonyUser.setCustName("anonymous");
}
@Autowired
private UserOperateLogService userOperateLogService;
@Pointcut("@annotation(com.fhzz.business.annotation.UserOperate)")
public void controllerAspecta(){}
@AfterReturning(value = "controllerAspecta() && @annotation(annotation) &&args(object,..) ", argNames = "annotation,object") public void interceptorApplogicA(UserOperate annotation, Object object) {
HttpServletRequest request = ((ServletRequestAttributes) RequestAttribut
es()).getRequest();
UserInfo userInfo = (Session().getAttribute("USERINFO");
if(userInfo==null){
userInfo = anonyUser;
}
UserOperateLog uol = new UserOperateLog();
String custName = CustName();
String realName = RealName();
String custId = CustId();
String ip = IpAddr(request);
String moduleName = duleName();
String option = annotation.option();
uol.setOperateDesc(option);
uol.setModuleName(moduleName);
uol.setCustId(custId);
uol.setCustName(custName);
uol.setRealName(realName);
uol.setIpAddress(ip);
uol.setCreateTime(new Date());
userOperateLogService.interceptorApplogicA(uol);
}
/**
* 获取IP地址
* @param request
* @return
*/
private String getIpAddr(HttpServletRequest request) {
String ip = Header("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = RemoteAddr();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论