简体   繁体   中英

Protocol redirection upon RenderRequest

I'm working on a project that uses the Spring Portlet-MVC framework and Velocity on a Liferay Portal server. For a few pages we have the requirement to serve them on a secure connection. Being fairly new to Portlets I came up with the solution of linking to an Action-Method and redirecting from there.

@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());
}

My solution works but to me it doesn't seem really nice. I was wondering if somebody could think of another, more transparent way to do this (using Interceptors and Annotations on RenderMappings maybe?).

Any suggestions will be greatly appreciated!

By some pages, do you mean Liferay pages or are you just concerned about the url that gets generated when a user clicks on some link from the portlet.

If you want to make some link of the portlet secure, then when use liferay-portlet flavour or renderURL or actionURL. It has an attribute called secure, which if set to true will make your url starting with https

If you are looking for some liferay page(for example /web/guest/mypage) to be secure then its kind of a hack and I wouldnt really suggest this to anyone, but if you have got no other option, you can create a service pre hook and check for url patterns you are concerned about and redirect to an https version of that url.

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM