1. 程式人生 > 其它 >用Spring AOP攔截頁面請求

用Spring AOP攔截頁面請求

技術標籤:javaspringaopspringmvc

注:開發環境為Spring+Spring MVC+myBatis 均為註解開發

從登陸頁面發起的請求,與正常的請求無誤
//登陸請求。

function login() {
		var layerIndex = layer.load();log();
		$("#formLogin").ajaxSubmit(function(data) {
			if (typeof data == "string") {
				data = JSON.parse(data);
			}
			layer.close
(layerIndex); if (data.state == true) { showToastr('登陸驗證', '登入成功</br>即將進行跳轉!', 2, 2000, '', true, 5); setTimeout(function() { window.location.href = "${ctx}/mainController/main.com"; }, 2000); } else { resetIdentity(); /* layer.msg(data.msg); *///5預設1; 1:右上,2:右下,3:左下,4:左上,5:頂部全寬,6:底部全寬,7:頂部居中,8:底部居中
showToastr('登陸驗證', data.msg, 4, 2000, true, true, 5);//標題-提示內容-樣式(1冒泡藍2√綠3!黃4!紅)-時間-關閉按鈕-進度條-位置 } }); }
//定義切面類
public class MyVerifyAopAspect {
	//Spring注入
	@Autowired
	private ILogService iLogService;
	
	public MyVerifyAopAspect() {
		System.err.println("驗證切面類初始化");
	}
	//定義切點
	//execution(返回任意型別* 方法所屬的位置com.ss.web.*.* (任意引數..))
@Pointcut("execution (* com.ss.web.loginController.login(..))") private void myPointCut() { } // 前置通知。在目標方法執行前執行該方法 @Before(value="myPointCut()") private void myBefor(JoinPoint joinpoint) { System.out.println("驗證前置通知"); System.err.print("目標:"+joinpoint.getTarget() ); System.out.print("方法名稱:"+joinpoint.getSignature().getName()); System.out.println("引數名:"+((CodeSignature) joinpoint.getSignature()).getParameterNames()[0]); System.out.println("引數名:"+((Object[])joinpoint.getArgs())[0]); } }

重點:在applicationContext.xml中配置啟動AspectJ自動代理由於Spring與SpringMVC的容器關係,將攔截不到頁面發起的請求。以此需要將配置啟動AspectJ自動代理配置到spring-mvc.xml中。即將以下程式碼加入到spring-mvc.xml中

<!-- 啟動AspectJ自動代理 proxy-target-class="true"沒有or是or否共三種狀態-->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

在這裡插入圖片描述