Java 拦截器 preHandle() 不重定向到 login.html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22990378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:50:01  来源:igfitidea点击:

Interceptor preHandle() not redirecting to login.html

javaspringjspspring-securityurl-redirection

提问by King Khan

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 falseit's not redirecting to login.html. What's wrong? Any gurus out there for help? Thanks in advance.

我有一个春季应用程序。我引入了一个 sessionInterceptor 来防止直接访问 index.jsp。如果用户没有登录,它就不能访问 index.jsp 并且应该被重定向到 login.html。代码命中 preHandle() 方法并运行所有代码,但在return false它没有重定向到 login.html 之后。怎么了?有高手帮忙吗?提前致谢。

My preHandle() in SessionInterceptor.java is:

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

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);

My dispatcher-servlet.xml settings are:

我的 dispatcher-servlet.xml 设置是:

<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>

The web.xml is:

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>

回答by Alessandro Bellone

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";
}

I hope it will work for you!

我希望它对你有用!