我需要在login.jsp調用的登錄servlet之前和之後實現一些邏輯。GateIn:用於登錄servlet的過濾器
所以我寫了一個URL /登錄過濾器來做到這一點。我需要對某些操作的用戶配置文件,所以我創造了這個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)總是空。我做錯了什麼?這是正確的方法嗎?