143 lines
5.5 KiB
Plaintext
143 lines
5.5 KiB
Plaintext
package th.co.muangthai.endrprint.controller.interceptor;
|
|
|
|
import org.apache.log4j.Logger;
|
|
import org.hibernate.SQLQuery;
|
|
import org.hibernate.Session;
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
import th.co.muangthai.endrprint.model.bean.form.master.EndrUserLoginForm;
|
|
import th.co.muangthai.endrprint.model.hibernate.HibernateUtil;
|
|
import th.co.muangthai.endrprint.util.DateUtil;
|
|
import th.co.muangthai.endrprint.util.SessionUtil;
|
|
import th.co.muangthai.endrprint.util.TextContent;
|
|
import th.co.muangthai.endrprint.util.VSMUtil;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpSession;
|
|
import java.text.ParseException;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
public class Interceptor extends HandlerInterceptorAdapter {
|
|
// public static final String URL_LOGIN = "../login.jsp";
|
|
private static Logger log = Logger.getLogger(th.co.muangthai.endrprint.controller.interceptor.Interceptor.class);
|
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
|
HttpSession session = request.getSession(true);
|
|
// HttpServletRequest req = (HttpServletRequest) request;
|
|
// HttpServletResponse res = (HttpServletResponse) response;
|
|
|
|
// HttpSession session = req.getSession(true);
|
|
// HttpSession session = req.getSession();
|
|
// String loginurl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
|
|
|
try {
|
|
if(VSMUtil.isEmpty(TextContent.DEPLOYMENT_VERSION)){
|
|
TextContent.DEPLOYMENT_VERSION = DateUtil.toFormatString(new Date(), "yyyyMMdd-HHmmss");
|
|
// SessionUtil.getSession().setAttribute("dv", TextContent.DEPLOYMENT_VERSION);
|
|
session.setAttribute("dv", TextContent.DEPLOYMENT_VERSION);
|
|
}
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
//String tokenId = request.getParameter("appId");
|
|
|
|
// log.info("Interceptor loginurl > " + loginurl);
|
|
|
|
// VSMUtil.SystemPrintDebugMode("Interceptor >>>>>> ");
|
|
// String url = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
|
/// Test Begin
|
|
Object obj = session.getAttribute(TextContent.sessionLogin);
|
|
if( null != obj ){
|
|
EndrUserLoginForm userBeans = (EndrUserLoginForm)obj;
|
|
//VSMUtil.SystemPrintDebugMode(userBeans.getFlagLogin());
|
|
if("1".equals( userBeans.getFlagLogin() ) ){
|
|
return true;
|
|
}
|
|
}
|
|
// String url = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
|
/// Test end
|
|
//String tokenId = request.getParameter("appId");
|
|
|
|
// log.info("Interceptor url > " + url);
|
|
|
|
|
|
// VSMUtil.SystemPrintDebugMode("Interceptor >>>>>> "+url + "<>" + tokenId);
|
|
/*
|
|
UserLoginBean userLoginBean = (UserLoginBean) session.getAttribute(VSMUtil.USER_SESSION);
|
|
|
|
request.getSession().getAttribute("USER_SESSION");
|
|
|
|
String tokenId = request.getParameter("appId");
|
|
String url = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
|
|
|
// log.info("0. page " + url + request.getServletPath());
|
|
// log.info("1. userLoginBean " + userLoginBean);
|
|
// log.info("2. tokenId " + tokenId);
|
|
|
|
String authen = request.getParameter("authen");
|
|
|
|
if (userLoginBean != null) {
|
|
|
|
// log.info("3. userLoginBean not null");
|
|
if(VSMUtil.isNotEmpty(tokenId) && VSMUtil.isEmpty(session.getAttribute("workflowFlag"))){
|
|
response.sendRedirect(URL_LOGIN+"?warn=3");
|
|
// log.info("4. redirect ");
|
|
return false;
|
|
}
|
|
|
|
// log.info("5. true ");
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// log.info("6. else ");
|
|
if (null != tokenId && tokenId.length() > 0) {
|
|
|
|
// log.info("7. tokenId exists ");
|
|
//set session workflow
|
|
session.setAttribute("workflowFlag","1");
|
|
|
|
UserService userService = new UserService();
|
|
UserLoginBean sessionLogin = userService.get(getUserIdFromToken(tokenId));
|
|
if (sessionLogin != null) {
|
|
log.info("[Workflow] User " + sessionLogin.getUserId() + " logged on.");
|
|
session.setAttribute(VSMUtil.USER_SESSION, sessionLogin);
|
|
// log.info("8. setting session ");
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (VSMUtil.isNotEmpty(authen)) {
|
|
UserService userService = new UserService();
|
|
UserLoginBean sessionLogin = userService.get(authen);
|
|
if (sessionLogin != null) {
|
|
session.setAttribute(VSMUtil.USER_SESSION, sessionLogin);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
// log.info("9. redirect login ");*/
|
|
// if(1 == 1) return true;
|
|
|
|
return false;
|
|
|
|
// try {
|
|
// response.sendRedirect(url+"/index.jsp#/");
|
|
//// response.sendRedirect(URL_LOGIN+"?warn=1");
|
|
// //response.sendRedirect("/logout"); // url
|
|
// //response.sendRedirect(url+"/index.jsp#/logout"); // url
|
|
// response.flushBuffer();
|
|
// }finally {
|
|
// //fc.doFilter(request, response);
|
|
// }
|
|
//
|
|
// return false;
|
|
}
|
|
}
|