日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

spring4.x aop拦截spring mvc controller

發(fā)布時(shí)間:2024/1/23 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring4.x aop拦截spring mvc controller 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 ?在spring-mvc.xml中,配置信息

<!-- 啟動(dòng)對(duì)@AspectJ注解的支持 --> <aop:aspectj-autoproxy/> <aop:aspectj-autoproxy proxy-target-class="true"/>
2 添加切面

import java.sql.Timestamp; import java.util.Date; import java.util.Iterator; import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.shiro.SecurityUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; 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 com.nufront.euht.service.web.service.WebSystemLogService; import com.nufront.euht.web.common.util.MyPropertiesUtil;/*** 系統(tǒng)日志AOP切面* */ @Component @Aspect public class SystemLogAspect { @Autowiredprivate WebSystemLogService webSystemLogService;//設(shè)置切入點(diǎn),匹配所有controller的所有方法public static final String CONTROLLER_PONINT_CUT="execution(* com.nufront.euht.controller..*.*(..))";@After(CONTROLLER_PONINT_CUT)public void after(JoinPoint joinpoint){// 解析請(qǐng)求信息HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String userName = (String) SecurityUtils.getSubject().getPrincipal();String ip = request.getRemoteAddr();String url = request.getRequestURI();Map parameter= request.getParameterMap();String paramStr = this.parseMap(parameter);Timestamp time = new Timestamp(new Date().getTime());String resourceName = MyPropertiesUtil.getProp("systemLog.properties", url);//將日志保存在數(shù)據(jù)庫(kù)中.SystemLog systemLog = new SystemLog(resourceName, url, paramStr, userName, ip, time);webSystemLogService.addOneSystemLog(systemLog);}private String parseMap(Map parameter){StringBuffer stringBuffer = new StringBuffer(); Iterator<Map.Entry<Object, Object>> it = parameter.entrySet().iterator();while (it.hasNext()) {Map.Entry<Object, Object> entry = it.next();String key = entry.getKey().toString();Object value = entry.getValue();StringBuffer valSb = new StringBuffer(); if("[Ljava.lang.String".equals(value.toString().split(";")[0])){ //為數(shù)組Object[] array = (Object[])value;for(int i = 0 ; i <array.length ; i++ ){valSb.append(array[i]+",");}String valSt = valSb.toString();if(valSt.length() >= 1 ){valSt = valSt.substring(0,valSt.length() -1); }stringBuffer.append("[key= " + key + " , value= {" + valSt+"}],"); }else{stringBuffer.append("[key= " + key + " , value= " + value+"],"); }}String result = stringBuffer.toString();if(result.length() >= 1 ){result = result.substring(0,result.length() -1); }return result;} }
3 自動(dòng)掃描切面

<context:component-scan base-package="com.nufront.euht.web.aop"/>


4 一個(gè)controller代碼

import java.io.IOException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Map;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.nufront.euht.model.CapEbu; import com.nufront.euht.scanDevice.snmp.SnmpData; import com.nufront.euht.service.web.charts.WebEbuService; import com.nufront.euht.service.web.service.WebEbuLogService; import com.nufront.euht.web.common.snmp.ParentSnmp; import com.nufront.euht.web.common.util.MyTimeUtil; import com.nufront.euht.web.deviceUpgrade.DeviceUpgradeThread; import com.nufront.euht.web.ebu.getLog.model.EbuLog;@Controller @RequestMapping("/web/ebu/ebuLogController") public class EbuLogController {@Autowiredprivate WebEbuLogService webEbuLogService;@Autowiredprivate WebEbuService webEbuService;private final String Get_EBU_LOG_OID = "1.3.6.1.4.1.59418.1.1.1.1.2.1.13.0";@RequestMapping("/forwardToEbuLog")public String forwardToEbuLog (HttpServletRequest request,HttpServletResponse response) throws Exception {String ids = request.getParameter("ids");request.setAttribute("ids",ids );EbuLog ebuLog = this.webEbuLogService.getLastOneEbuLog();request.setAttribute("ebuLog", ebuLog);return "/web/ebu/getLog/getLog";}@RequestMapping("/getEbuLog")public void getEbuLog (HttpServletRequest request,HttpServletResponse response) throws Exception {String ftpAddr = request.getParameter("ftpAddr");String user = request.getParameter("userName");String passwd = request.getParameter("passwd");String file_src = request.getParameter("file_src");String ids = request.getParameter("ids");EbuLog ebuLogDB = this.webEbuLogService.getLastOneEbuLog();boolean isChange = this.ebuLogIsChange(ftpAddr, user, passwd, file_src, ebuLogDB);if(isChange){EbuLog newEbuLog = this.createEbuLog(ftpAddr, user, passwd, file_src, request);this.webEbuLogService.addOneEbuLog(newEbuLog);ebuLogDB = newEbuLog;}String jsonResult = this.setEbuLogPDU(ids, ebuLogDB);response.setHeader("Content-type", "text/html;charset=UTF-8"); response.getWriter().write(jsonResult);}}


創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的spring4.x aop拦截spring mvc controller的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。