我想从过滤器类中访问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?
答案 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();
}
}