package com.cnpc.framework.filter;

import com.cnpc.framework.utils.SpringContextUtil;
import com.cnpc.framework.utils.StrUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.config.Ini;
import org.apache.shiro.session.Session;
import org.apache.shiro.util.AntPathMatcher;
import org.apache.shiro.util.PatternMatcher;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;

/**
 * filter过滤器，获取项目路径，设置ajax超时标识
 *
 * @author billJiang QQ:475572229
 */
public class SystemFilter implements Filter {


    private static List<String> anonlist;

    private static PatternMatcher pathMatcher=new AntPathMatcher();


    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException,
            ServletException {


        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println(request.getRequestURL());
        String basePath = request.getContextPath();
        request.setAttribute("basePath", basePath);
        request.setAttribute("isauth",SecurityUtils.getSubject().isAuthenticated());
        if (!SecurityUtils.getSubject().isAuthenticated()) {
            //判断session里是否有用户信息
            if (request.getHeader("x-requested-with") != null
                    && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
                //如果是ajax请求响应头会有，x-requested-with
                //如果是anno的请求，则直接通过
                if (!matchAnonPath(request.getRequestURI(),basePath)) {
                    response.setHeader("session-status", "timeout");//在响应头设置session状态
                    return;
                }
            }
        }
        filterChain.doFilter(request, servletResponse);

    }

    @Override
    public void destroy() {

        // TODO Auto-generated method stub

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

        // TODO Auto-generated method stub

    }


    public List<String> getAnonlist() {
        if (anonlist != null) {
            return anonlist;
        } else {
            Ini.Section section = (Ini.Section) SpringContextUtil.getBean("filterChainDefinitionClass");
            anonlist = new ArrayList<>();
            for (String key : section.keySet()) {
                if (section.get(key).equals("anon")) {
                    anonlist.add(key);
                }
            }
            return anonlist;
        }
    }

    public boolean matchAnonPath(String path,String basePath) {
        path=path.replace(basePath,"");
        List<String> alist = this.getAnonlist();
        boolean match = false;
        for (String s : alist) {
            if (pathMatches(s,path)) {
                match = true;
                break;
            }
        }
        return match;
    }

    protected boolean pathMatches(String pattern, String path) {
        return pathMatcher.matches(pattern, path);
    }
}

@@ -84,49 +84,8 @@
 il;%0A
-import com.cnpc.framework.utils.StrUtil;%0A
 impo
@@ -159,49 +159,8 @@
 ni;%0A
-import org.apache.shiro.session.Session;%0A
 impo
@@ -260,60 +260,28 @@
 org.
-springframework.beans.factory.annotation.Autowired;%0A
+apache.log4j.Logger;
 %0Aimp
@@ -478,68 +478,8 @@
 t;%0A%0A
-import org.apache.shiro.spring.web.ShiroFilterFactoryBean;%0A%0A
 /**%0A
@@ -593,25 +593,24 @@
 s Filter %7B%0A%0A
-%0A
     private 
@@ -639,17 +639,16 @@
 onlist;%0A
-%0A
     priv
@@ -684,17 +684,19 @@
 hMatcher
-=
+ = 
 new AntP
@@ -709,16 +709,83 @@
 cher();%0A
+    static final Logger log = Logger.getLogger(SystemFilter.class);
 %0A%0A    pu
@@ -944,17 +944,16 @@
 tion %7B%0A%0A
-%0A
         
@@ -1001,17 +1001,16 @@
 Request)
- 
 servletR
@@ -1077,17 +1077,16 @@
 esponse)
- 
 servletR
@@ -1094,24 +1094,25 @@
 sponse;%0A
+%0A
         
 System.o
@@ -1107,26 +1107,17 @@
     
-System.out.println
+log.debug
 (req
@@ -1135,24 +1135,25 @@
 uestURL());%0A
+%0A
         Stri
@@ -1188,24 +1188,96 @@
 textPath();%0A
+        boolean isauth = SecurityUtils.getSubject().isAuthenticated();%0A%0A
         requ
@@ -1358,112 +1358,38 @@
 th%22,
-SecurityUtils.getSubject().isAuthenticated());%0A        if (!SecurityUtils.getSubject().isAuthenticated()
+ isauth);%0A        if (! isauth
 ) %7B%0A
@@ -1806,16 +1806,20 @@
 meout%22);
+    
 //%E5%9C%A8%E5%93%8D%E5%BA%94%E5%A4%B4%E8%AE%BE%E7%BD%AE
@@ -1954,17 +1954,16 @@
 ponse);%0A
-%0A
     %7D%0A%0A 
@@ -2187,17 +2187,16 @@
 %0A    %7D%0A%0A
-%0A
     publ

fix some typos