我需要在 login.jsp 调用的登录 servlet 之前和之后实现一些逻辑。
所以我为 url /login 编写了一个过滤器来做到这一点。我需要获取一些操作的用户配置文件,所以我创建了这个 LoginFilter 类:
public class LoginFilter implements Filter {
private static Logger logger = Logger.getLogger(LoginFilter.class);
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String username = httpRequest.getParameter("username");
String password = httpRequest.getParameter("password");
chain.doFilter(request, response);
PortalRequestContext context = PortalRequestContext.getCurrentInstance();
if (context == null)
logger.info("PortalRequestContext is NULL");
else {
String userId = context.getRemoteUser();
if (userId == null || userId.equals(""))
logger.info("Login failed, IP:" + httpRequest.getRemoteAddr());
else
logger.info("Login executed, username:" + userId);
}
}
问题是“上下文”(PortalRequestContext)始终为空。我做错了什么?这是正确的方法吗?