今天看了一段代码,里面有这个登录权限检查:
public class AuthorityInterceptor implements MethodInterceptor  
06.{  
07.    public Object invoke(MethodInvocation invocation) throws Throwable  
08. {  
09.        HttpServletRequest request = null;  
10.        ActionMapping mapping = null;  
11.        Object[] args = invocation.getArguments();  
12.        for (int i = 0 ; i < args.length ; i++ )  
13.        {  
14.            if (args[i] instanceof HttpServletRequest) request = (HttpServletRequest)args[i];  
15.            if (args[i] instanceof ActionMapping) mapping = (ActionMapping)args[i];  
16.        }  
17.  Object admin = request.getSession().getAttribute("admin");  
18.        if ( admin != null && ((String)admin).equals("admin"))  
19.        {  
20.            return invocation.proceed();  
21.        }  
22.        else  
23.        {  
24.     request.setAttribute("msg" , "您还没有登录,请先登录");  
25.            return mapping.findForward("admin");  
26.        }  
27.    }  
28.}  我想问一下在第17行request.getSession().getAttribute("admin")这一句,admin是什么时候设到Session里面去的?为什么这里可以直接拿来用啊?

解决方案 »

  1.   

    登录的时候放入session的建议去看看aop原理,面向切面是如何运行的
      

  2.   

    还有以下的配置文件:
    32.
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
    33.     <property name="beanNames">  
    34.            <list>  
    35.                <value>/processAddStudent</value>  
    36.                <value>/listStudent</value>  
    37.                <value>/delStudent</value>  
    38.                <value>/processAddTest</value>  
    39.                <value>/addQuestion</value>  
    40.                <value>/delQuestion</value>      
    41.                <value>/processAddQuestion</value>  
    42.            </list>  
    43.     </property>  
    44.        <property name="interceptorNames">  
    45.            <list>  
    46.                <value>authorityInterceptor</value>  
    47.            </list>  
    48.        </property>  
    49.    </bean>  
    50. <bean id="authorityInterceptor" class="org.yeeku.action.authority.AuthorityInterceptor"/> 请问第35行到第41行的<value>标签里面的内容是什么啊?这些类也是要用户自己去定义的么?谢谢
      

  3.   


    依照你发出来的看,通过<property name="beanNames"> (bean名字) 就是以下的:
    <list>   
    35. <value>/processAddStudent</value>   
    36. <value>/listStudent</value>   
    37. <value>/delStudent</value>   
    38. <value>/processAddTest</value>   
    39. <value>/addQuestion</value>   
    40. <value>/delQuestion</value>   
    41. <value>/processAddQuestion</value>   
    42. </list>   bean名字同list组里面value相同的名字使用aop功能这些类一般是需要自己去定义的,基本上都是设置在service层
      

  4.   


    大概意思是不是登录进来的用户想进行上面list里面的操作时,自己的AOP就会执行check工作,如果是admin用户,就放行,要是不是,就提示“权限不足”?
      

  5.   

    对的!java 面向切面,动态代理是精髓,很多开源框架都是基于此开发的,要多多了解