2012-03-22 94 views
0

我正在使用Liferay Portal服务器上的Spring Portlet-MVC框架和Velocity的项目。对于几页我们有要求在安全的连接上为他们服务。对于Portlet来说相当新,我提出了链接到Action-Method并从那里重定向的解决方案。RenderRequest上的协议重定向

@ActionMapping(params = "command=secureRedirect") 
public void actionSecureRedirect(ActionRequest request, ActionResponse response) { 
    HttpServletRequest servletRequest = PortalUtil.getHttpServletRequest(request); 
    String absoluteUrl = servletRequest.getRequestURL().toString(); 
    String[] urlComponents = StringUtils.split(absoluteUrl, '/'); 
    StringBuffer redirectUrl = new StringBuffer("https://"); 
    redirectUrl.append(urlComponents[1]); 
    redirectUrl.append("<specificPath>"); 
    response.sendRedirect(redirectUrl.toString()); 
} 

我的解决方案的作品,但对我来说似乎不是很好。我想知道是否有人可以想到另一种更透明的方法来做到这一点(也许使用拦截器和RenderMappings的注释?)。

任何建议将不胜感激!

回答

2

通过一些页面,你的意思是Liferay页面,或者你只关心当用户点击portlet中的某个链接时生成的url。

如果您希望使portlet的某些链接安全,那么在使用liferay-portlet flavor或renderURL或actionURL时。它有一个名为安全属性,如果将它设置为true,将让你的URL开始以https

如果你正在寻找一些Liferay的页面(例如/网络/客户/我的空间)是安全的,然后它一种黑客,我不会真的向任何人推荐,但如果你没有别的选择,你可以创建一个服务预钩子,并检查你关心的url模式,并重定向到该网址的https版本。

1
write this code in controller 


protected PortletURL getRedirectURL(ActionRequest actionRequest) { 
     ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); 
     String portletName = (String) actionRequest.getAttribute(WebKeys.PORTLET_ID); 
     PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest), portletName, themeDisplay.getLayout().getPlid(), 
       PortletRequest.RENDER_PHASE); 
     return redirectURL; 
    } 

@ActionMapping(params="something") 
public void save(ActionRequest actionRequest, Other parameters){ 



/.....Your code 



.....// 
redirectURL = getRedirectURL(actionRequest); 
actionResponse.sendRedirect(redirectURL.toString()); 
}