Skip to content

Instantly share code, notes, and snippets.

@SergioLarios
Created November 27, 2014 14:41
Show Gist options
  • Save SergioLarios/f5d8d812db28cf31c3d1 to your computer and use it in GitHub Desktop.
Save SergioLarios/f5d8d812db28cf31c3d1 to your computer and use it in GitHub Desktop.
package com.hibu.chile.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.hibu.chile.constant.Mappings;
import com.hibu.chile.constant.MsgManProps;
import com.hibu.chile.util.Validator;
import com.hibu.chile.web.UrlUtils;
/**
* Login Interceptor
*
* @author SANTIAGO\sergio.larios
*/
@Component
public class SecurityInterceptor implements HandlerInterceptor {
/* *******************************************************
* ********************** Pre Handle *********************
* *******************************************************/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (!forceLoging) {
return true;
}
HttpSession session = request.getSession();
if (Validator.isNotNull(session)) {
String userName = (String) session.getAttribute(USER_NAME);
if (Validator.isBlank(userName)) {
String lastPath = UrlUtils.getUri(request);
session.setAttribute(LAST_PATH, lastPath);
}
else {
request.setAttribute(USER_NAME, userName);
return true;
}
}
String ctxPath = request.getContextPath();
if (Validator.isBlank(ctxPath)) {
response.sendRedirect(Mappings.LOGIN);
}
else {
response.sendRedirect(ctxPath + Mappings.LOGIN);
}
return false;
}
/* *******************************************************
* ********************* Post Handle *********************
* *******************************************************/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
}
/* *******************************************************
* ******************* After Completion ******************
* *******************************************************/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
}
/* *******************************
******* Injected Variables ******
******************************* */
@Value(MsgManProps.FORCE_LOGIN)
boolean forceLoging;
/* *******************************
******** Public Constants *******
******************************* */
public static final String USER_NAME = "authUserName";
public static final String LAST_PATH = "authlastPath";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment