---
title: "spring aop控制权限，想要细到method级别"
author: "anson_xu"
date: "2008-11-26T12:38:47Z"
tags: ["AOP", "Bean", "JSP", "Servlet", "Spring"]
slug: "spring-aop-method"
draft: false
lastmod: "2008-11-26T12:38:47Z"
id: 279014
url: "https://helloxuweifu.iteye.com/blog/279014"
---

spring aop控制权限，想要细到method级别，但是advisor唔用。不明白！！ 这是我的一个spring bean配置 method index org.springframework.web.servlet.view.JstlView true loginAction /WEB-INF/jsp/default.jsp list test /manager* aroundAdvice advice是对权限检测，判断是否有权限调用方法 package net.kingbit.advice; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.web.servlet.ModelAndView; //@Aspect public class YkAroundAdvice implements MethodInterceptor { //@Before("execution(\*net.kingbit.actions.Manger.list(HttpServletRequest,HttpServletResponse))") public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("ljkjljljklj"); Object\[] arg = invocation.getArguments(); System.out.println("length---" + arg.length); int length=arg.length; if(length!=0) {HttpServletRequest request = null; String name = invocation.getMethod().getDeclaringClass().getName() + invocation.getMethod(); System.out.println("method name---" + name); request = (HttpServletRequest) arg\[0]; for (int i = 0; i &lt; arg.length; i++) { System.out.println("arg\[" + i + "]---" + arg\[i]); if (arg\[i] instanceof HttpServletRequest) { request = (HttpServletRequest) arg\[i]; } } HttpSession session = request.getSession(); String permission = (String) session.getAttribute("permission"); if (permission.equals("admin")) { return invocation.proceed(); } return new ModelAndView("/WEB-INF/jsp/login.jsp")；//没权限跳转到登入页面 } return null; } } Manager: package net.kingbit.actions; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class Manager extends MultiActionController { public ModelAndView test(HttpServletRequest request,HttpServletResponse response) { return new ModelAndView("/WEB-INF/jsp/admin.jsp"); } public ModelAndView list(HttpServletRequest request,HttpServletResponse response) { return new ModelAndView("/index.jsp"); } }