Wednesday, July 22, 2009

Example with SessionValidationInterceptor

SessionValidationInterceptor.java ---> this interseptor used for the session validation and bit secured way to check the session means we specified the some request URIs as String Array.
preHandle() -----> true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.










package com.optrasystems.mvc.interceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class SessionValidationInterceptor extends HandlerInterceptorAdapter {
//here keep the controller urls which do not want session
private static final String [] IGNORE_URI ={"login", "authenticateUser", "showThumnNail"};

// true if the execution chain should proceed with the next interceptor or the handler itself. Else, //DispatcherServlet assumes that this interceptor has already dealt with the response itself means it //display the same page
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI();

for (String uri : SessionValidationInterceptor.IGNORE_URI) {
if(requestURI.endsWith(uri)){
return true;

}
}
User user = (User) request.getSession().getAttribute("user");
if (user == null) {
response.sendRedirect("login");
return false;
}
return true;
}
}

No comments: