//package com.sunxung.factoring.component.filter;
//
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.HashSet;
//import java.util.Set;
//
//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 com.sunxung.factoring.component.constance.AuthConstance;
//import com.sunxung.factoring.component.exception.BusinessException;
//import com.sunxung.factoring.component.exception.ServiceException;
//import com.sunxung.factoring.component.exception.ValidatorException;
//import com.sunxung.factoring.component.util.*;
//import com.sunxung.factoring.entity.sys.User;
//import com.sunxung.factoring.service.sys.AuthorityService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
///**
// * <p>
// * 是否登录验证过滤器，如果用户已经登录则设置用户相关信息到线程辅助对象，线程辅助对象请查阅{@link ThreadLocalUtil}
// * </p>
// */
//@Component
//public class LoginFilter implements Filter {
//
//    public static final String PARAM_NAME_URLS = "excludeUrls";
//    public static final String PARAM_NAME_PREFIXS = "excludePrefixs";
//    public static final String PARAM_NAME_SUFFIXS = "excludeSuffixs";
//    private static Set<String> urlsPattern = new HashSet<String>();
//    private static Set<String> prefixsPattern = new HashSet<String>();
//    private static Set<String> suffixsPattern = new HashSet<String>();
//
//    private  AuthorityService  authorityService;
//
//
//    public LoginFilter(AuthorityService authorityService){
//        this.authorityService = authorityService;
//    }
//
//    @Override
//    public void init(FilterConfig config) throws ServletException {
//        String urls = config.getInitParameter(PARAM_NAME_URLS);
//        if (!StringUtil.isEmpty(urls)) {
//            urlsPattern = new HashSet<String>(Arrays.asList(urls.split(",")));
//        }
//        String prefixs = config.getInitParameter(PARAM_NAME_PREFIXS);
//        if (!StringUtil.isEmpty(urls)) {
//            prefixsPattern = new HashSet<String>(Arrays.asList(prefixs.split(",")));
//        }
//        String suffixs = config.getInitParameter(PARAM_NAME_SUFFIXS);
//        if (!StringUtil.isEmpty(suffixs)) {
//            suffixsPattern = new HashSet<String>(Arrays.asList(suffixs.split(",")));
//        }
//    }
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
//            throws IOException, ServletException {
//        HttpServletRequest httpRequest = (HttpServletRequest) request;
//        HttpServletResponse httpResponse = (HttpServletResponse) response;
//        ThreadLocalUtil.clearThreadVariable();
//        String requestURI = httpRequest.getRequestURI();
//        //String sid = CookieUtil.getSid(httpRequest);
//       // String sessionIdString = (String) httpRequest.getSession().getAttribute(SystemVariable.SESSION_KEY);
////        Long sessionId = null;
////        if (StringUtil.isNotEmpty(sessionIdString)) {
////            sessionId = Long.valueOf(sessionIdString);
////        }
////        if (null == sessionId && StringUtil.isNotBlank(sid)) {
////            String sessionIdStr = String.valueOf(sessionId);
////            sessionIdStr = sid;
////            httpRequest.getSession().setAttribute(SystemVariable.SESSION_KEY, sessionIdStr);
////        }
//        if (isExclusion(requestURI)) {
//            chain.doFilter(request, response);
//            return;
//        }
//        String token  = httpRequest.getHeader(AuthConstance.X_AUTH_ACCESS);
//        if(StringUtil.isEmpty(token)){
//            throw new ServiceException(401,"认证信息已过期，起重新登录");
//        }
//        User user = authorityService.getUserByToken(token);
//        if(user==null){
//            throw new ServiceException(401,"认证信息已过期，起重新登录");
//        }
//        ThreadLocalUtil.setUser(user);
//    }
//
//    private AuthorityService getAuthorityService() {
//        if (authorityService == null) {
//            authorityService = SpringUtil.getBeanByType(AuthorityService.class);
//        }
//        return authorityService;
//    }
//
//    /**
//     * 过滤不需要验证的url
//     *
//     * @param requestURI 请求url
//     */
//    private boolean isExclusion(String requestURI) {
//        for (String prefix : urlsPattern) {
//            if (requestURI.equals(prefix)) {
//                return true;
//            }
//        }
//        for (String prefix : prefixsPattern) {
//            if (requestURI.startsWith(prefix)) {
//                return true;
//            }
//        }
//        for (String suffix : suffixsPattern) {
//            if (requestURI.endsWith(suffix)) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//    @Override
//    public void destroy() {
//        // TODO Auto-generated method stub
//    }
//}
