如何从Filter类获取liferay远程用户

时间:2013-10-09 08:26:26

标签: java liferay liferay-6

我想从过滤器类中访问liferay远程用户ID。

@Override
public void doFilter(ServletRequest request, 
                     ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {
    try {
        HttpServletRequest httpServletRequest =  (HttpServletRequest) request;
        User user = (User)request.getAttribute(WebKeys.USER);

        System.out.println("User ID :" + user.getUserId());
        System.out.println("User ID :" + httpServletRequest.getRemoteUser());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输出

User :null
java.lang.NullPointerException

请告诉我如何获取HttpServletRequest的远程用户ID?

1 个答案:

答案 0 :(得分:3)

您可以使用com.liferay.portal.util.PortalUtil.getUser(HttpServletRequest request)来获取Liferay用户。

public void doFilter(ServletRequest request, 
            ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
    try {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;   
        User user = PortalUtil.getUser(request);    
        System.out.println("User ID :" + user.getUserId());
    } catch (Exception e) {
        e.printStackTrace();
    }
}