zkddkz2010 2015-06-25 08:22 采纳率: 0%
浏览 1164

记录每个用户的操作 记录每个用户的操作

1、JoinPoint和ProceedingJoinPoint(aspectjrt-1.6.11.jar包):采用AOP的方法,记录每个用户的操作。
(1)记录用户操作主要记录用户对系统进行操作时,对后台controller中的方法进行调用进行记录。
首先对要记录的方法进行配置(针对controller中的方法调用进行调用),即:

aop:config







/aop:aspect
/aop:config
这样,当调用com.paic.mercury.vigilant.controller下的类中的方法时,就会执行doAround方法,进而记录用户操作。
(2)doAround方法如下
如执行Spring会自动传入doAround的参数,如pjp:execution(ModelAndView com.paic.mercury.vigilant.controller.HomeController.login(HttpServletRequest));execution(List

com.paic.mercury.vigilant.controller.HomeController.getAvailableServices(HttpServletRequest))。
public Object doAround2(ProceedingJoinPoint pjp) throws Throwable {
Object retVal = pjp.proceed(); //proceed方法相当于invoke方法,调用目标类的目标方法。
String userName = "NULL";
Object user = ServletUtil.getResourceUser();
if (user != null && user instanceof ResourceUser) {
userName = ((ResourceUser) user).getUsername();
}
StringBuffer sb = new StringBuffer();
//得到执行的类及调用的方法
sb.append("User(").append(userName).append(") invoke ").append(pjp.getTarget().getClass().getSimpleName()).append(".").append(pjp.getSignature().getName());
if (retVal instanceof UIResult) {
UIResult result = (UIResult) retVal;
if (!result.isFlag()) {
sb.append(" ERROR! code:").append(result.getErrorCode()).append(" message:").append(result.getMessage());
}
}
LOG.info(sb.toString());
//输出:2015-06-25 16:09:42,279 INFO User(XUXIN109) invoke HomeController.showHome
Object[] args = pjp.getArgs(); //得到调用方法时,传入的参数。
if (args != null && args.length > 1) { //输出参数
LOG.info("PRINTING ARGS. ===================");
StringBuffer argSb = new StringBuffer();
for (int index = 0; index < args.length - 1; index++) {
Object arg = args[index];
if (arg != null) {
argSb.append("arg type:").append(args[index].getClass().getSimpleName()).append(" value:").append(args[index] + "");
}
LOG.info(argSb.toString()); }
LOG.info("PRINTING ARGS END. ==============");
}
return retVal;
}
(3)login()方法如下:
@RequestMapping(value = "/home/login")
public ModelAndView login(HttpServletRequest request) {
ModelAndView view = new ModelAndView("login");
AuthenticationException exception = (AuthenticationException) request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
if (exception != null) {
if (exception instanceof BadCredentialsException) {
view.addObject("ERROR_MSG", "BadCredentials");
} else if (exception instanceof UsernameNotFoundException) {
view.addObject("ERROR_MSG", "UserNotFound");
}
request.getSession().setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, null);
}
return view;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
    • ¥15 fluent的在模拟压强时使用希望得到一些建议
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样
    • ¥15 java的GUI的运用
    • ¥15 Web.config连不上数据库
    • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
    • ¥15 怎么配置广告联盟瀑布流