spring切面切点@Aspect的使用

spring aspect是指在指定的方法前后或者环绕式的加上另外的处理,当指定的方法被使用的时候aspect配置的方法会被调用和执行。就像切面一样,从一个切入点开始往这个切面嵌入别的处理流程

比如,在登陆的时候配置了登陆的切面,切面的功能是检查用户的ip地址,如果ip地址不符合我们要求的ip地址,则做相应的处理,处理完成后继续登陆方法的执行。

下面是一个简单的aspect的代码:

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.heiman.smarthome.exception.CustomException;
import com.heiman.smarthome.po.custom.Config;
import com.heiman.smarthome.po.custom.EnterpriseMemberCustom;
import com.heiman.smarthome.po.enumpo.LogLevel;
import com.heiman.smarthome.po.enumpo.OperationType;
import com.heiman.smarthome.po.system.SecurityAudit;
import com.heiman.smarthome.service.SecurityAuditService;

@Aspect
@Component
public class SecurityAuditAspectj {

	@Autowired
	private HttpSession httpSession;

	@Autowired
	private HttpServletRequest httpServletRequest;

	@Autowired
	private Config config;

	// 切点<增删改查和登陆
	@Pointcut("execution(* com.heiman.xx.service..*.*update*(..)) ||  "
			+ "execution(* com.heiman.xx.service..*.*delete*(..)) || "
			+ "execution(* com.heiman.xx.service..*.*insert*(..)) || "
			+ "execution(* com.heiman.xx.service..*.*add*(..)) || "
			+ "execution(* com.heiman.xx.service..*.*save*(..)) || "
			+ "execution(* com.heiman.xx.service..*.*modify*(..)) || "
			+ "execution(* com.heiman.xx.service..*.*edit*(..)) || "
			+ "execution(* com.heiman.xx.service..*.*remove*(..)) || "
			+ "execution(* com.heiman.xx.service..*.*repair*(..)) || "
			+ "execution(* com.heiman.xx.controller..*.*login*(..))")
	public void pointcut() {
	}

	/**
	 * 切点之后
	 * 
	 * @param jp
	 */
	@After("pointcut()")
	public void after(JoinPoint jp) {

		SecurityAudit audit = new SecurityAudit();
		String detail = null; // 详细信息

		String methodName = jp.getSignature().getName();// 方法名

		String className = jp.getTarget().getClass().getSimpleName(); // 获取类名

		EnterpriseMemberCustom custom = (EnterpriseMemberCustom) httpSession.getAttribute("memberMsg");
		if (custom == null)
			return;
		String userName = custom.getMembername();// 登陆用户的名字
		Integer enterpriseMemberId = custom.getId();

		if (methodName.equals("login")) {
			detail = userName + "登陆了";
		} else {
			String s = className.replaceAll("ServiceImpl", "").replaceAll("Service", "");
			detail = userName + "对 " + s + " 执行了操作 " + methodName + " :" + together(jp);
		}

		if (methodName.contains("update") || methodName.contains("modify") || methodName.contains("edit")
				|| methodName.contains("repair")) {
			// 修改
			audit.setOperationtype(OperationType.UPDATE.getIndex());
		} else if (methodName.contains("delete") || methodName.contains("remove")) {
			// 删除
			audit.setOperationtype(OperationType.DELETE.getIndex());
		} else if (methodName.contains("save") || methodName.contains("insert") || methodName.contains("add")) {
			// 增加
			audit.setOperationtype(OperationType.ADD.getIndex());
		} else if (methodName.equals("login")) {
			// 登陆
			audit.setOperationtype(OperationType.LOGIN.getIndex());
		}

		audit.setOperatorip(httpServletRequest.getLocalAddr());// 操作者ip
		audit.setOperationtime(new Date());// 时间
		audit.setLoglevel(LogLevel.INFO.getIndex());// 日志级别
		audit.setEnterprisememberid(enterpriseMemberId); // 操作者id
		audit.setLogsummary(detail);// 详情


	}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值