Tuesday, January 21, 2014

[JAVA] Login Interceptor Example

Login Interceptor Example
On Spring Framework

package com.kaudo.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Service
public class LoginCheckInterceptor extends HandlerInterceptorAdapter {

 private static final Logger logger = Logger.getLogger(SecurityInterceptor.class);

  @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  HttpSession session=request.getSession(false);
  @SuppressWarnings("unused")
  String referer=request.getHeader("referer");
  //logger.info(referer);
  if(session==null){
   logger.info("Intercepted by session: "+session);
   response.sendRedirect("/interceptor?url=/");
   return false;
  }
  String userid=(String)session.getAttribute("userid");
  String usertype=(String)session.getAttribute("usertype");
  if(userid==null || usertype==null || ("").equals(userid) || ("").equals(usertype)){
   logger.info("Intercepted by userid: "+userid+", usertype: "+usertype);
   response.sendRedirect("/interceptor?url=/");
   return false;
  }
  return true;
 }
}

No comments:

Post a Comment