本文介绍了从servlet过滤器重定向Web客户端(通过AJAX的客户端-服务器连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAASRealm身份验证(在tomcat 7中)创建一个Web.这是servlet的过滤器:

I'm doing a web with JAASRealm authentication (in tomcat 7).This is a filter for the servlets:

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中声明了它

And I have declared it in 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时,这一行过滤器不起作用

My problem is when I'm not authenticated and I call the servlet via AJAX, this line of filter doesn't work

httpServletResponse.sendRedirect(loginPage);

因此,我没有收到任何数据,也没有重定向到登录页面.在这种情况下我该怎么办?

So, I haven't receive any data and I'm not redirected to the loggin page. What can I do in this case?

推荐答案

这是我在过滤AJAX请求时发送重定向的方式...

Here is how I send re-directs when Filtering AJAX requests...

AJAX代码(jQuery)

AJAX Code (jQuery)

$.ajax({
    type: 'post',
    url: '/signMeInServlet',
    data: {
        'name': name,
        'pass': passW,
        },
    dataType: 'json',
    success: processSignInReturnMethod,
    error: function (xhr, ajaxOptions, thrownError){
        if (thrownError.redirect.length) {
            window.location.replace(thrownError.redirect);
        } else {
            alert('There was an error processing your request, please try again');
        }
    }
});

相关过滤器代码

// Rest of your Filter processing and logic here, once you know you want
// to redirect, use the below code, which works for both AJAX and regualar
// requests

HttpServletRequest hreq = (HttpServletRequest) request;
String redirectUrl = "/redirected.jsp";

if (hreq.getHeader("x-requested-with") != null &&
        hreq.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {

    // Set up your response here
    HttpServletResponse hres = (HttpServletResponse) response;
    hres.setContentType("text/json; charset=UTF-8");

    PrintWriter out = hres.getWriter();

    String json = "[{\"redirect\":\"" + redirectUrl + "\"}]";

    out.write(json);
    out.flush();
    out.close();

} else {

    ((HttpServletResponse)response).sendRedirect(redirectUrl);
}

这篇关于从servlet过滤器重定向Web客户端(通过AJAX的客户端-服务器连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:39