ITeye博客

spring aop控制权限,想要细到method级别

anson_xu · 2008-11-26 12:38 · spring · Spring · JSP · AOP · Bean · Servlet · Markdown

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 < 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"); } }

评论(2)

  1. #2 楼 · anson_xu · 2009-01-09 18:05

    没传request过来

  2. #1 楼 · %小樱桃% · 2009-01-07 17:27

    import javax.servlet.http.HttpServletRequest;

    import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.struts.action.ActionMapping;

    public class AuthorityInterceptor implements MethodInterceptor { // private PermissionManager permissionManager;

    public Object invoke(MethodInvocation arg0) throws Throwable {
    	HttpServletRequest request = null;      
        ActionMapping mapping = null;      
        Object args = arg0.getArguments();      
        for (int i = 0 ; i < args.length ; i++ ){ 
            if (args* instanceof HttpServletRequest) request = (HttpServletRequest)args*;      
            if (args* instanceof ActionMapping) mapping = (ActionMapping)args*;      
        } 
    

    // 从session中得到用户的级别
    String level = (String)request.getSession().getAttribute("level"); //如是经理或员工级别则继续,否则,回到登陆页面
    if ( level != null && (level.equals("emp") || level.equals("mgr"))){
    return arg0.proceed();
    }else{
    return mapping.findForward("login");
    } }
    }

    在invoke()方法里面为什么得不到Request对象?? 希望能及时给我个回答·

    谢谢

评论功能维护中,当前仅展示历史评论。