package com.bootdo.system.shiro;


import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.ExecutionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.mgt.FilterChainManager;
import org.apache.shiro.web.filter.mgt.FilterChainResolver;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.mgt.WebSecurityManager;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.BeanInitializationException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 刷新策略自定义
 *
 * @author Mr.xiBel
 * @date 2022/4/15
 **/

public class SystemShiroFilterFactoryBean extends ShiroFilterFactoryBean {
    private List<String> WHITELIST;

    /**
     * 重写 filter
     *
     * @return
     * @throws Exception
     */
    @Override
    protected AbstractShiroFilter createInstance() throws Exception {


        SecurityManager securityManager = getSecurityManager();
        if (securityManager == null) {
            String msg = "SecurityManager property must be set.";
            throw new BeanInitializationException(msg);
        }

        if (!(securityManager instanceof WebSecurityManager)) {
            String msg = "The security manager does not implement the WebSecurityManager interface.";
            throw new BeanInitializationException(msg);
        }

        FilterChainManager manager = createFilterChainManager();

        //Expose the constructed FilterChainManager by first wrapping it in a
        // FilterChainResolver implementation. The AbstractShiroFilter implementations
        // do not know about FilterChainManagers - only resolvers:
        PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
        chainResolver.setFilterChainManager(manager);

        //Now create a concrete ShiroFilter instance and apply the acquired SecurityManager and built
        //FilterChainResolver.  It doesn't matter that the instance is an anonymous inner class
        //here - we're just using it because it is a concrete AbstractShiroFilter instance that accepts
        //injection of the SecurityManager and FilterChainResolver:
        return new PersonSpringShiroFilter((WebSecurityManager) securityManager, chainResolver, WHITELIST.toArray(new String[WHITELIST.size()]));
    }

    public List<String> getWHITELIST() {
        return WHITELIST;
    }

    public void setWHITELIST(List<String> wHITELIST) {
        WHITELIST = wHITELIST;
    }


    /**
     * 自定义filter
     */
    private static final class PersonSpringShiroFilter extends AbstractShiroFilter {
        private String[] WHITELIST;

        protected PersonSpringShiroFilter(WebSecurityManager webSecurityManager, FilterChainResolver resolver, String[] WHITELIST) {
            super();
            this.WHITELIST = WHITELIST;
            if (webSecurityManager == null) {
                throw new IllegalArgumentException("WebSecurityManager property cannot be null.");
            }
            setSecurityManager(webSecurityManager);
            if (resolver != null) {
                setFilterChainResolver(resolver);
            }
        }

        @SuppressWarnings({"unchecked", "rawtypes"})
        @Override
        protected void doFilterInternal(ServletRequest servletRequest, ServletResponse servletResponse, final FilterChain chain)
                throws ServletException, IOException {

            Throwable t = null;

            try {
                final ServletRequest request = prepareServletRequest(servletRequest, servletResponse, chain);
                final ServletResponse response = prepareServletResponse(request, servletResponse, chain);

                final Subject subject = createSubject(request, response);
                HttpServletRequest httpServletRequest = (HttpServletRequest) request;
                String servletPath = httpServletRequest.getServletPath();

                subject.execute((Callable) () -> {
                    // 更新不在白名单中的url 刷新session
                    if (!this.checkWhiteList(servletPath, WHITELIST)) {
                        updateSessionLastAccessTime(request, response);
                    }
                    executeChain(request, response, chain);
                    return null;
                });
            } catch (ExecutionException ex) {
                t = ex.getCause();
            } catch (Throwable throwable) {
                t = throwable;
            }

            if (t != null) {
                if (t instanceof ServletException) {
                    throw (ServletException) t;
                }
                if (t instanceof IOException) {
                    throw (IOException) t;
                }
                //otherwise it's not one of the two exceptions expected by the filter method signature - wrap it in one:
                String msg = "Filtered request failed.";
                throw new ServletException(msg, t);
            }
        }

        /**
         * 检查是否在白名单
         *
         * @param servletPath
         * @param whitelist
         * @return
         */
        private boolean checkWhiteList(String servletPath, String[] whitelist) {
            for (String s : whitelist) {
                if (s.equals(servletPath)) {
                    return true;
                }
            }
            return false;
        }
    }

}
