关于Spring Portlet开发中的HandlerInterceptor

 在Spring Portlet开发中,我们可以用HandlerInterceptor 来实现对于portlet的拦截,主要的需求场景比如授权处理。它可以让我们来自定义处理器执行链。

其实很类似Web开发中的Filter,但是不同的在于Filter是Servlet范畴的概念,它可以用来操作ServletRequest 和ServletResponse,并且配置在web.xml中,而Portlet HandlerInterceptor是Portlet范畴的概念,它可以用来操作PortletRequest和PortletResponse,并且配置在Spring应用上下文中。

在HandlerInterceptor中定义了若干主要方法:具体参见

http://supercharles888.blog.51cto.com/609344/845170

 

为了让我们的应用使用HandlerInterceptor,一个常见的设计模式是使用Adaptor模式,让我们的自定义HandlerInterceptor类继承自HandlerInterceptorAdaptor类,然后进行覆写。 下面是个例子.

 

例子:

a.需求,我们需要在Portlet的任何页面上都可以用到当前登录的用户的用户名和所属权限,那么我们的可以开发一个HandlerInterceptor:

 
  
  1. /** 
  2.  *  
  3.  * This handler interceptor will intercept the request before/after the flow. 
  4.  * Since it is configured in envprovisioning-config.xml ,so it will intercept request to envprovision flow 
  5.  * We can have some pre/post handling work such as initialize the variables 
  6.  *@author cwang58 
  7.  *@created date: Feb 5, 2013 
  8.  */ 
  9. public class EnvProvisionHandlerInterceptor extends HandlerInterceptorAdapter { 
  10.      
  11.     private static final SimpleLogger LOGGER = new SimpleLogger(EnvProvisionHandlerInterceptor.class); 
  12.  
  13.      
  14.     /** 
  15.      * This method will pre handle before rendering the first page in the flow 
  16.      * Here it get the current logined user information from the ThemeDisplay 
  17.      * Then get the role that this user belongs to ,we make sure whether this user belongs to AER Admin-AERAdmin 
  18.      * if user belongs to this ,then set isAdministrator attribute to request scope so that it can be used in every page of this flow. 
  19.      */ 
  20.     @Override 
  21.     public boolean preHandleRender(RenderRequest request, 
  22.             RenderResponse response, Object handler)  { 
  23.         User user = EnvProUtil.getUserObj(request); 
  24.          
  25.         if(user == null){ 
  26.             return false
  27.         } 
  28.         String userName= user.getScreenName(); 
  29.         LOGGER.debug("Login user name:"+userName); 
  30.          
  31.         boolean isAdministrator = false
  32.          
  33.         try
  34.          
  35.         List<Role> roles = user.getRoles(); 
  36.         for(int i= 0;i<roles.size();i++) { 
  37.          
  38.             Role role =roles.get(i); 
  39.             if("AER Admin-AERAdmin".equals(role.getName()) || "Administrator".equals(role.getName())){ 
  40.                 isAdministrator=true
  41.                 LOGGER.debug("Login user has role:"+role.getName()); 
  42.                 break
  43.             } 
  44.         } 
  45.          
  46.  
  47.         request.setAttribute("userId", userName); 
  48.         request.setAttribute("isAdministrator", isAdministrator); 
  49.          
  50.         return true
  51.          
  52.         }catch(SystemException ex){ 
  53.             LOGGER.error("SystemException occurs when get current user role",ex); 
  54.             return false
  55.         } 
  56.     } 
  57.  
  58.      
  59.      
  60.      
  61.      
  62.  

比如以上的例子中,我们从ThemeDisplay中获取当前用户的权限和用户名,然后把它设置到RenderRequest的request scope上。

 

为了让这个HandlerInterceptor生效,我们必须配置这个interceptor在Spring应用上下文中:

 
  
  1. <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping"> 
  2.         <property name="portletModeMap"> 
  3.             <map> 
  4.                 <entry key="view"> 
  5.                     <bean class="xx.xx.xx.envprovisioning.handlers.EnvProvisionFlowHandler" /> 
  6.                 </entry> 
  7.             </map> 
  8.         </property> 
  9.         <property name="interceptors">   
  10.             <list>   
  11.                 <ref bean="envprovisionhandlerinterceptor"/> 
  12.             </list>   
  13.         </property> 
  14.     </bean> 
  15.     <!-- add interceptor to get user and privilege --> 
  16.     <bean id="envprovisionhandlerinterceptor" class="xx.xx.xx.envprovisioning.interceptors.EnvProvisionHandlerInterceptor"/> 

这样,页面上就可以用el表达式来获取这些信息了。





本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/1159367,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值