当前位置:   article > 正文

java 不拦截_Java 过滤器Filter,Java Filter 不拦截某些请求 Java 过滤器支持Ajax请求...

filter无法拦截png

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.HashSet;importjava.util.Set;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.commons.lang.StringUtils;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;public class SessionFilter implementsFilter {protected final Log logger = LogFactory.getLog(SessionFilter.class);private Set unFilterSet = new HashSet();

@Overridepublic void init(FilterConfig config) throwsServletException {

InputStream in= null;

BufferedReader reader= null;try{

in= SessionFilter.class.getClassLoader().getResourceAsStream("sessionUnFilter.properties");if(in != null){

reader= new BufferedReader(newInputStreamReader(in));

String lineText= null;

logger.info("=====不拦截的匹配规则有:");while((lineText = reader.readLine()) != null){if(!StringUtils.isBlank(lineText) && (!lineText.trim().startsWith("#"))){//过滤掉空行和注释行

logger.info("=====" +lineText);

unFilterSet.add(lineText);

}

}

}

}catch(Exception e) {

e.printStackTrace();

}finally{if(reader != null){try{

reader.close();

}catch(IOException e) {

e.printStackTrace();

}

}if(in != null){try{

in.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

logger.info("SessionFilter init()");

}

@Overridepublic voiddestroy() {

logger.info("SessionFilter destroy()");

}/*** 如果请求链接符合不拦截的匹配,返回true

*@paramunFilterSet

*@paramrequestURI

*@return

*/

public boolean isPass(SetunFilterSet, String requestURI){

logger.info("=====requestURI = "+requestURI);if(unFilterSet != null && unFilterSet.size() > 0){for(String unFilterUri : unFilterSet) {if(!StringUtils.isBlank(unFilterUri)){

unFilterUri=unFilterUri.trim();if(unFilterUri.equals(requestURI)){return true;

}else if(unFilterUri.startsWith("*") && unFilterUri.length() > 1 && unFilterUri.endsWith("*")){

String text= unFilterUri.substring(1, (unFilterUri.length() - 1));//logger.info("=====contains text = " + text);

if(requestURI.contains(text)){return true;

}

}else if(unFilterUri.startsWith("*") && !unFilterUri.endsWith("*")){

String text= unFilterUri.substring(1, (unFilterUri.length()));//logger.info("=====endsWith text = " + text);

if(requestURI.endsWith(text)){return true;

}

}else if(!unFilterUri.startsWith("*") && unFilterUri.endsWith("*")){

String text= unFilterUri.substring(0, (unFilterUri.length() - 1));//logger.info("=====startsWith text = " + text);

if(requestURI.startsWith(text)){return true;

}

}

}

}

}return false;

}

@Overridepublic voiddoFilter(ServletRequest servletRequest, ServletResponse servletResponse,

FilterChain chain)throwsIOException, ServletException {

HttpServletRequest req=(HttpServletRequest) servletRequest;

HttpServletResponse res=(HttpServletResponse) servletResponse;boolean isAjaxRequest = false;//判断是否Ajax请求

if(!StringUtils.isBlank(req.getHeader("x-requested-with")) && req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){

isAjaxRequest= true;

}

UserInfo userInfo= null;try{

userInfo=SecurityExtApi.getUserInfo(req);

}catch(GeneralFailureException e) {

e.printStackTrace();

}if(userInfo != null && !StringUtils.isBlank(userInfo.getUserID())){

chain.doFilter(req, res);

}else{

String requestURI=req.getRequestURI();//logger.info("=====requestURI = "+requestURI);

if(requestURI.endsWith(".js") || requestURI.endsWith(".css") || requestURI.endsWith(".png")|| requestURI.endsWith(".jpg") || requestURI.endsWith(".jpeg") || requestURI.endsWith(".gif")|| requestURI.endsWith(".ico")){

chain.doFilter(req, res);return;

}else if(isPass(unFilterSet, requestURI)){

chain.doFilter(req, res);return;

}else{

String msg= "登录已失效,请刷新页面或重新登录";

logger.info("=====" +msg);if(isAjaxRequest){//Ajax请求结果处理

res.setContentType("application/json;charset=GBK");

res.setCharacterEncoding("GBK");

res.setHeader("error_code", "-999");

res.setHeader("error_msg", "The login is timeout, please login again!");throw new RuntimeException(msg);//需要增加Ajax异常处理js全局配置文件ajax.config.js

}else{

res.sendRedirect("/");

}

}

}

}

}

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号