Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 当会话过期时,如何在Spring MVC globaly中转发sessionExpired.jsp_Spring Mvc - Fatal编程技术网

Spring mvc 当会话过期时,如何在Spring MVC globaly中转发sessionExpired.jsp

Spring mvc 当会话过期时,如何在Spring MVC globaly中转发sessionExpired.jsp,spring-mvc,Spring Mvc,我是SpringMVC新手。我不知道如何在SpringMVC全局中转发sessionExpired.jsp,当会话过期时。 我在谷歌上搜索了很多次,但没有找到任何解决方案。我得到的结果是,他们使用的是spring安全性。在我们的应用程序中,我们没有使用spring安全性 请提供一些示例代码。如果您添加此代码,应该可以解决您的问题 <security:session-management invalid-session-url="SessionExpired.jsp" session-aut

我是SpringMVC新手。我不知道如何在SpringMVC全局中转发sessionExpired.jsp,当会话过期时。 我在谷歌上搜索了很多次,但没有找到任何解决方案。我得到的结果是,他们使用的是spring安全性。在我们的应用程序中,我们没有使用spring安全性


请提供一些示例代码。

如果您添加此代码,应该可以解决您的问题

<security:session-management invalid-session-url="SessionExpired.jsp" session-authentication-error-url="AlreadyLoggedIn.jsp">
<security:concurrency-control max-sessions="1" expired-url="SessionIsDuplicated.jsp" error-if-maximum-exceeded="false" />
</security:session-management>
在不使用安全性的情况下,SpringMVC中有两种方法可以在servlet中使用HandlerInterceptorAdapter或过滤器

使用HandlerInterceptorAdapter

在springContext.xml中

<mvc:interceptors>
<bean class="com.nalashaa.interceptor.SessionValidatorInterceptor" />
</mvc:interceptors>

package com.nalashaa.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class SessionValidatorInterceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getServletPath().equals("/login.htm")) {  //for Login Page
        return true;
    }
if(request.getSession().getAttribute("userName")!=null){
return true;
}else{
response.sendRedirect("login.htm");
return false;
} 
}
}
使用过滤器

在web.xml中

<filter>  
<filter-name>SessionValidatorFilter</filter-name>  
 <filter-class>com.nalashaa.filter.SessionValidatorFilteFilter</filter-class>  
</filter>  
<filter-mapping>  
 <filter-name>SessionValidatorFilter</filter-name>  
 <url-pattern>/*</url-pattern>  
</filter-mapping>   

package com.nalashaa.filter;  

import java.io.IOException;  
import javax.servlet.Filter;  
import javax.servlet.FilterChain;  
import javax.servlet.FilterConfig;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.RequestDispatcher;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  

public class SessionValidatorFilter implements Filter {  

public void init(FilterConfig filterConfig) throws ServletException {  
// initialization 
} 


public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
 if(((HttpServletRequest)request).getRequestedSessionId() != null && ((HttpServletRequest)request).isRequestedSessionIdValid() == false) {  
  RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/login.jsp");  
  requestDispatcher.forward(request, response);  
 }else {  
  chain.doFilter(request, response);  
 }  
}  

public void destroy() {  
//Destroy code
}  

}

谢谢你的回答,但是我没有使用Spring安全性。我想你发布的任何东西都是实现安全性的。在上面编辑了答案嘿。。。请接受答案。这将结束问题,并使人们更容易在以后搜索答案!