设计自己的Annotation实现方法的鉴权

1、参照 如下文章- 实战篇:设计自己的Annotation 
http://www.iteye.com/topic/36659 

2、 
Java代码 
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5.   
  6. @Target(ElementType.METHOD) //规定注解可以应用的范围  
  7. @Retention(RetentionPolicy.RUNTIME)     //规定注解可以被反射机制读取  
  8. public @interface NeedLogin {  
  9.   
  10. }  
  11.   
  12. mport java.util.Map;  
  13. import java.util.Set;  
  14.   
  15. import javax.servlet.http.HttpSession;  
  16.   
  17. import org.aopalliance.intercept.MethodInterceptor;  
  18. import org.aopalliance.intercept.MethodInvocation;  
  19. import org.apache.struts.Globals;  
  20.   
  21. import com.xasxt.cake.common.WebContext;  
  22.   
  23. public class SafeAsepct implements MethodInterceptor {  
  24.   
  25.     public Object invoke(MethodInvocation mi) throws Throwable {  
  26.         //判读用户身份  
  27.         HttpSession session = WebContext.getSession();  
  28.         if (session.getAttribute("USER_LOGIN")==null) {  
  29.             //如果没有,返回null。如果有,返回注解对象  
  30.             NeedLogin needLogin = mi.getMethod().getAnnotation(NeedLogin.class);  
  31.               
  32.             if (needLogin!=null) {  
  33.                 String uri = WebContext.getRequest().getRequestURI() + "?";  
  34.                 //Key : String, Value : String[]  
  35.                 Map map = WebContext.getRequest().getParameterMap();  
  36.                 Set keys = map.keySet();  
  37.                 for (Object key : keys) {  
  38.                     uri += key + "=";  
  39.                     //简化了问题  
  40.                     uri += ((String[])map.get(key))[0] + "&";  
  41.                 }  
  42.                 System.out.println("uri = " + uri);  
  43.                 WebContext.getSession().setAttribute("LAST_URI", uri);  
  44.                   
  45.                 throw new LoginException("您需要登录才能使用此功能!");  
  46.             }  
  47.         }  
  48.         return mi.proceed();    //chain.doFilter(req, myRes);  
  49.           
  50.     }  
  51.   
  52.     public static void main(String[] args) {  
  53.         WebContext.getSession();  
  54.     }  
  55.   
  56. }  
  57.   
  58.   
  59. <!-- 抽象Bean (模板) -->  
  60.     <bean id="BizTemplate"  
  61.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
  62.         abstract="true">  
  63.         <property name="transactionManager" ref="tm"></property>    <!-- 谁来代理 -->  
  64.         <property name="proxyTargetClass" value="true"></property>  <!-- 代理方式true:cglib, false:动态代理 -->  
  65.         <property name="transactionAttributes">  
  66.             <props>  
  67.                 <!-- 事务的传播行为 -->  
  68.                 <prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>  
  69.                 <prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>  
  70.                 <prop key="*">PROPAGATION_REQUIRED</prop>  
  71.             </props>  
  72.         </property>  
  73.     </bean>  
  74.   
  75.     <bean id="cakeBizTarget" class="com.xasxt.cake.biz.impl.CakeBiz">  
  76.         <property name="cakeDAO" ref="CakeDAO"></property>  
  77.     </bean>  
  78.   
  79.     <bean id="shopBiz" parent="BizTemplate">  
  80.         <property name="target" ref="shopBizTarget"></property> <!-- 代理谁? -->  
  81.     </bean>  
  82.   
  83.     <bean id="cakeBiz2" parent="BizTemplate">  
  84.         <property name="target" ref="cakeBizTarget"></property> <!-- 代理谁? -->  
  85.     </bean>  
  86.   
  87.     <!-- 安全切面 -->  
  88.     <bean id="safe" class="com.xasxt.cake.aspect.SafeAsepct" />  
  89.   
  90.     <!-- 配置代理 -->  
  91.     <bean id="cakeBiz" class="org.springframework.aop.framework.ProxyFactoryBean">  
  92.         <property name="target" ref="cakeBiz2"></property>  
  93.         <property name="interceptorNames" value="safe"></property>  
  94.     </bean>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值