package com.paic.wcm.search.admin.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.cfg.DefualtConfig;
import org.wltea.analyzer.dic.Dictionary;
import com.paic.wcm.search.admin.core.Config;
import com.paic.wcm.search.admin.service.AuthService;
import com.paic.wcm.search.admin.service.quartz.BackupJob;
import com.paic.wcm.search.client.SimpleIndexClient;
import com.paic.wcm.search.service.QuartzService;
import com.paic.wcm.search.service.quartz.LoadDicJob;
/**
* 更新索引校验filter
*/
public class AuthFilter implements Filter {
private static final Logger logger = LoggerFactory
.getLogger(AuthFilter.class);
/**
* Default constructor.
*/
public AuthFilter() {
}
/**
* @see Filter#destroy()
*/
public void destroy() {
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String path = req.getServletPath();
//更新索引的时候校验密砄1�7
if (path.equals("/pingan/update")||path.equals("/update")) {
String password = request.getParameter(SimpleIndexClient.KEY_PA_AUTH);
if (!AuthService.auth(password)) {
((HttpServletResponse) res).sendError(401);
return;
}else{
chain.doFilter(req, res);
return;
}
}
//静态资源不进行权限校验
if(path.contains("static")){
chain.doFilter(req, res);
return;
}
String userName = (String)req.getSession().getAttribute("user");
if(StringUtils.isEmpty(StringUtils.trimToEmpty(userName))){
if(path.contains("/admin")){
if(!(path.contains("login.do") || path.endsWith("login.jsp"))){
String contextPath = req.getContextPath();
res.sendRedirect(contextPath+"/admin/login.jsp");
return;
}
}
}
chain.doFilter(req, res);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// 初始化IK词典
Dictionary.initial(DefualtConfig.getInstance());
LoadDicJob.loadDic();
// 清理备份锁文仄1�7
BackupJob.cleanLockFile();
try {
QuartzService.scheduleJob(BackupJob.class, Config.get()
.getProperty(Config.KEY_CRON_BACKUP), null);
QuartzService.scheduleJob(LoadDicJob.class, Config.get()
.getProperty(Config.KEY_CRON_DIC), null);
} catch (Exception e) {
logger.error("创建定时任务异常");
logger.error(ExceptionUtils.getStackTrace(e));
}
}
}
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.cfg.DefualtConfig;
import org.wltea.analyzer.dic.Dictionary;
import com.paic.wcm.search.admin.core.Config;
import com.paic.wcm.search.admin.service.AuthService;
import com.paic.wcm.search.admin.service.quartz.BackupJob;
import com.paic.wcm.search.client.SimpleIndexClient;
import com.paic.wcm.search.service.QuartzService;
import com.paic.wcm.search.service.quartz.LoadDicJob;
/**
* 更新索引校验filter
*/
public class AuthFilter implements Filter {
private static final Logger logger = LoggerFactory
.getLogger(AuthFilter.class);
/**
* Default constructor.
*/
public AuthFilter() {
}
/**
* @see Filter#destroy()
*/
public void destroy() {
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String path = req.getServletPath();
//更新索引的时候校验密砄1�7
if (path.equals("/pingan/update")||path.equals("/update")) {
String password = request.getParameter(SimpleIndexClient.KEY_PA_AUTH);
if (!AuthService.auth(password)) {
((HttpServletResponse) res).sendError(401);
return;
}else{
chain.doFilter(req, res);
return;
}
}
//静态资源不进行权限校验
if(path.contains("static")){
chain.doFilter(req, res);
return;
}
String userName = (String)req.getSession().getAttribute("user");
if(StringUtils.isEmpty(StringUtils.trimToEmpty(userName))){
if(path.contains("/admin")){
if(!(path.contains("login.do") || path.endsWith("login.jsp"))){
String contextPath = req.getContextPath();
res.sendRedirect(contextPath+"/admin/login.jsp");
return;
}
}
}
chain.doFilter(req, res);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// 初始化IK词典
Dictionary.initial(DefualtConfig.getInstance());
LoadDicJob.loadDic();
// 清理备份锁文仄1�7
BackupJob.cleanLockFile();
try {
QuartzService.scheduleJob(BackupJob.class, Config.get()
.getProperty(Config.KEY_CRON_BACKUP), null);
QuartzService.scheduleJob(LoadDicJob.class, Config.get()
.getProperty(Config.KEY_CRON_DIC), null);
} catch (Exception e) {
logger.error("创建定时任务异常");
logger.error(ExceptionUtils.getStackTrace(e));
}
}
}