拦截器preHandle()不会重定向到login.html [英] Interceptor preHandle() not redirecting to login.html

查看:3161
本文介绍了拦截器preHandle()不会重定向到login.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹簧应用程序。我引入了一个sessionInterceptor来阻止对index.jsp的直接访问。如果用户未登录,则无法访问index.jsp,应将其重定向到login.html。代码正在使用preHandle()方法并运行所有代码,但在返回false 后,它不会重定向到login.html。怎么了?有没有大师帮忙?在此先感谢。

I have a spring application. I introduced a sessionInterceptor to prevent direct access to index.jsp. If the user is not logged in it shouldn't be able to access index.jsp and should be redirected to login.html. The code is hitting the preHandle() method and running all the code but after return false it's not redirecting to login.html. What's wrong? Any gurus out there for help? Thanks in advance.

我在SessionInterceptor.java中的preHandle()是:

My preHandle() in SessionInterceptor.java is:

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        // if displaying the home page, make sure the user is reloaded.
        if (request.getRequestURI().endsWith("login.html")) {
            session.removeAttribute("isUserLoggedIn");
        }

        if (session.getAttribute("isUserLoggedIn") == null && !request.getRequestURI().endsWith("login")) {
            response.sendRedirect(request.getContextPath() + "/login.html");
            return false;               
        }
        return true;
    }

我也试过以下但都是徒劳。

I have tried the following as well but all in vain.

RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/login.html");
                dispatcher.forward(request, response);

我的dispatcher-servlet.xml设置为:

My dispatcher-servlet.xml settings are:

<bean id="sessionInterceptor" class="com.xxx.xxx.xxx.SessionInterceptor" />
<bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="sessionInterceptor" />
        </list>
    </property>
</bean>

<!-- Configures Handler Interceptors -->
<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean class="com.xxx.xxx.xxx.SessionInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

web.xml是:

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>


推荐答案

你可以尝试重定向到一个逻辑路径从控制器获取
尝试

you can try redirecting to a logical path that will be catched from a controller Try

response.sendRedirect("/NotLogged");

然后创建一个这样的函数:

And then create a function like this:

@RequestMapping(value = {"/NotLogged"}, method = RequestMethod.GET)
public String notLogged() {
    return "login.html";
}

我希望它适合你!

这篇关于拦截器preHandle()不会重定向到login.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆