我正在使用JAASRealm身份驗證(在tomcat 7中)執行Web。 這是servlet的一個過濾器:從servlet過濾器(通過AJAX客戶端 - 服務器連接)重定向Web客戶端
private String loginPage = "welcome.jsp";
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
if (httpServletRequest.getUserPrincipal() == null) {
// User is not logged in, redirect to login page.
httpServletRequest.setAttribute("from", httpServletRequest.getRequestURI());
httpServletResponse.sendRedirect(loginPage);
}
else {
filterChain.doFilter(request, response);
}
}
}
而且我已經在web.xml中聲明它
<filter>
<filter-name>login-filter</filter-name>
<filter-class>LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login-filter</filter-name>
<url-pattern>/sampleServlet</url-pattern>
</filter-mapping>
我的問題是,當我沒有授權,但我通過AJAX調用servlet,這過濾器行不起作用
httpServletResponse.sendRedirect(loginPage);
所以,我沒有收到任何數據,我沒有重定向到登錄頁面。在這種情況下我能做些什麼?
你能告訴我們你的AJAX請求?我想我知道你的問題是什麼,但直到我看到代碼時才能確定。我認爲它與請求的內容類型有關。 – oberger