在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:
- /**
- *
- * This handler interceptor will intercept the request before/after the flow.
- * Since it is configured in envprovisioning-config.xml ,so it will intercept request to envprovision flow
- * We can have some pre/post handling work such as initialize the variables
- *@author cwang58
- *@created date: Feb 5, 2013
- */
- public class EnvProvisionHandlerInterceptor extends HandlerInterceptorAdapter {
- private static final SimpleLogger LOGGER = new SimpleLogger(EnvProvisionHandlerInterceptor.class);
- /**
- * This method will pre handle before rendering the first page in the flow
- * Here it get the current logined user information from the ThemeDisplay
- * Then get the role that this user belongs to ,we make sure whether this user belongs to AER Admin-AERAdmin
- * if user belongs to this ,then set isAdministrator attribute to request scope so that it can be used in every page of this flow.
- */
- @Override
- public boolean preHandleRender(RenderRequest request,
- RenderResponse response, Object handler) {
- User user = EnvProUtil.getUserObj(request);
- if(user == null){
- return false;
- }
- String userName= user.getScreenName();
- LOGGER.debug("Login user name:"+userName);
- boolean isAdministrator = false;
- try{
- List<Role> roles = user.getRoles();
- for(int i= 0;i<roles.size();i++) {
- Role role =roles.get(i);
- if("AER Admin-AERAdmin".equals(role.getName()) || "Administrator".equals(role.getName())){
- isAdministrator=true;
- LOGGER.debug("Login user has role:"+role.getName());
- break;
- }
- }
- request.setAttribute("userId", userName);
- request.setAttribute("isAdministrator", isAdministrator);
- return true;
- }catch(SystemException ex){
- LOGGER.error("SystemException occurs when get current user role",ex);
- return false;
- }
- }
- }
比如以上的例子中,我们从ThemeDisplay中获取当前用户的权限和用户名,然后把它设置到RenderRequest的request scope上。
为了让这个HandlerInterceptor生效,我们必须配置这个interceptor在Spring应用上下文中:
- <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
- <property name="portletModeMap">
- <map>
- <entry key="view">
- <bean class="xx.xx.xx.envprovisioning.handlers.EnvProvisionFlowHandler" />
- </entry>
- </map>
- </property>
- <property name="interceptors">
- <list>
- <ref bean="envprovisionhandlerinterceptor"/>
- </list>
- </property>
- </bean>
- <!-- add interceptor to get user and privilege -->
- <bean id="envprovisionhandlerinterceptor" class="xx.xx.xx.envprovisioning.interceptors.EnvProvisionHandlerInterceptor"/>
这样,页面上就可以用el表达式来获取这些信息了。
本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/1159367,如需转载请自行联系原作者