简体   繁体   English

会话过期后,Spring MVC会将用户重定向到登录页面

[英]Spring MVC redirect user to login page once session is expired

I have stored a user bean in session as @SessionAttributes({"UserBean"}) in my controller. 我在会话中将用户bean存储为@SessionAttributes({"UserBean"})在我的控制器中。

My aim is to redirect user to login/error page if session is expired. 我的目标是在会话过期时将用户重定向到登录/错误页面。 Following is my code snippet 以下是我的代码段

@RequestMapping(value = "searchOpportunity.htm", method = RequestMethod.GET)
public ModelAndView searchOpportunity(@ModelAttribute("UserBean") UserBean userBean) {
    functionName = "searchOpportunity";
    logger.info("HERE !!! In " + className + " - " + functionName + " ");
    System.out.println("HERE !!! In " + className + " - " + functionName + " ");
    try {
        if (userBean == null) {
            System.out.println(userBean);
            return new ModelAndView("userLogout", "command", null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new ModelAndView("opportunity/opportunitySearch", "command", new SearchOpportunityBean());
}

However when the session is expired i get the following error. 但是,当会话过期时,我收到以下错误。

org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet mintDispatcher threw exception
org.springframework.web.HttpSessionRequiredException: Session attribute 'UserBean' required - not found in session
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:761)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:758)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Unknown Source)

Seems like the controller code is never executed if userbean is expired. 似乎如果userbean过期,则永远不会执行控制器代码。 What is the reason ? 是什么原因 ? What can be a solution ? 什么可以解决方案?

Why don't you try this with Spring Security? 为什么不尝试使用Spring Security?

<sec:session-management invalid-session-url="/login">
        <sec:concurrency-control expired-url="/login" />
</sec:session-management>

This will redirect to /login page when the user's session is expired or invalid. 当用户的会话过期或无效时,这将重定向到/ login页面。

Ref: http://docs.spring.io/spring-security/site/docs/3.0.x/reference/springsecurity.html 参考: http//docs.spring.io/spring-security/site/docs/3.0.x/reference/springsecurity.html

You can do it by the help of HandlerInterceptorAdapter. 你可以在HandlerInterceptorAdapter的帮助下完成它。 The preHandle() method should return true if the execution chain should proceed with the next interceptor or the handler( your controller) itself. 如果执行链应继续执行下一个拦截器或处理程序(您的控制器)本身,则preHandle()方法应该返回true。 Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself. 否则,DispatcherServlet假定这个拦截器已经处理了响应本身。

    public class YourInterceptor extends HandlerInterceptorAdapter {
        public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
            UserBean userBean = (UserBean) WebUtils.getSessionAttribute(request, "UserBean");
            if(userBean == null){
                //whatever you want to do
                return throw new ModelAndViewDefiningException(new ModelAndView("userLogout", "command", null));
            }else{
                return true;
            }
        }
    }

and specify this interceptor in your handler mapping definitions as: 并在处理程序映射定义中指定此拦截器:

    <bean id="yourInterceptor" class="package.YourInterceptor"/>
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="order" value="1" />
            <property name="interceptors">
                <list>
                    <ref bean="yourInterceptor" />
                </list>
            </property>
            <property name="mappings">
                <props>
                    <prop key="/searchOpportunity.htm">YourController</prop>    
                </props>
            </property>
        </bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 会话在Spring MVC中过期 - Session expired in spring MVC 会话过期后如何使用拦截器进行重定向Spring MVC - How to redirect after session has expired using an interceptor Spring mvc 如何在Java Web应用程序中过期会话时重定向到“登录”页面? - How to redirect to Login page when Session is expired in Java web application? 在没有使用spring安全性的情况下,在spring mvc中将会话超时页面重定向到登录页面 - redirect page to login page on session time out in spring mvc without using spring security 在没有Spring安全性的Spring MVC 4.2中会话超时后重定向到“登录”页面 - Redirect to Login page after session time out in spring mvc 4.2 without spring security Spring MVC处理会话已过期 - Spring MVC handling session expired 在Spring MVC中登录后重定向到主页 - Redirect to home page after login in spring mvc Spring MVC-重定向到所有选项卡中的登录页面 - Spring MVC - Redirect to login page in all tabs 在超时时将用户重定向到登录页面Spring Security - Redirect user to login page on timeout Spring Security 在 Wicket 9 中,当用户的会话在某些页面中过期时,他们被重定向到登录页面而不是会话过期页面 - In Wicket 9, when the user's session expires in certain pages, they are redirected to the login page not the Session Expired page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM