import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SessionFilter implements Filter {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SessionFilter.class);
@Value("$(serverurl)")
private String serverurl;
/*
* 封装,不需要过滤的list列表
*/
protected static List<Pattern> patterns = new ArrayList<Pattern>();
@Override
public void init(FilterConfig filterConfig) throws ServletException {
patterns.add(Pattern.compile("loginq.do"));
patterns.add(Pattern.compile("loginh.do"));
patterns.add(Pattern.compile("share.do"));
patterns.add(Pattern.compile("zhuce.do"));
patterns.add(Pattern.compile("huiyuan.do"));
patterns.add(Pattern.compile("login.do"));
patterns.add(Pattern.compile("tiaozhuan.do"));
patterns.add(Pattern.compile("register.do"));
patterns.add(Pattern.compile("loginy.do"));
patterns.add(Pattern.compile("jiali.do"));
patterns.add(Pattern.compile("main/autoFillty_rj_situation.*"));
patterns.add(Pattern.compile("main/post.*"));
patterns.add(Pattern.compile("main/get.*"));
patterns.add(Pattern.compile(".*[(\\.js)||(\\.css)||(\\.png)||(\\.tff)]"));
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
String url = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
if (url.startsWith("/") && url.length() > 1) {
url = url.substring(1);
}
if (isInclude(url)){
chain.doFilter(httpRequest, httpResponse);
return;
} else {
HttpSession session = httpRequest.getSession();
if (session.getAttribute("currentUser") != null){
// session存在
chain.doFilter(httpRequest, httpResponse);
return;
} else {
// session不存在 准备跳转失败
httpResponse.sendRedirect("/loginy.do");
}
}
}
@Override
public void destroy() {
}
/*
* 是否需要过滤
* @param url
* @return
*/
private boolean isInclude(String url) {
for (Pattern pattern : patterns) {
Matcher matcher = pattern.matcher(url);
if (matcher.matches()) {
return true;
}
}
return false;
}
}
filter
在启动类里面添加
/**
* 配置过滤器
* @return
*/
@Bean
public FilterRegistrationBean someFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(sessionFilter());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("sessionFilter");
return registration;
}
/*
* 创建一个bean
* @return
*/
@Bean(name = "sessionFilter")
public Filter sessionFilter() {
return new SessionFilter();
}
registration.addUrlPatterns("/**");过滤所有请求
拦截器:
在启动类里面添加
@Bean//拦截器
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean reg = new ServletRegistrationBean(dispatcherServlet);
reg.getUrlMappings().clear();
reg.addUrlMappings("*.html");
reg.addUrlMappings("*.do");
reg.addUrlMappings("*.js");
reg.addUrlMappings("*.css");
reg.addUrlMappings("*.txt");
reg.addUrlMappings("*.woff");
reg.addUrlMappings("*.ttf");
reg.addUrlMappings("*.png");
reg.addUrlMappings("*.jpg");
return reg;
}