All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.mklaus.framework.extend.AbstractTokenInterceptor Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package cn.mklaus.framework.extend;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * @author Mklaus
 * Created on 2018-01-02 下午1:41
 */
public abstract class AbstractTokenInterceptor extends HandlerInterceptorAdapter {

    private static Logger log = LoggerFactory.getLogger(AbstractTokenInterceptor.class);

    protected List getUriList() {
        return Collections.emptyList();
    }

    protected List getPrefixUriList() {
        return Collections.emptyList();
    }

    protected List getIgnoreUriList() {
        return Collections.emptyList();
    }

    protected List getIgnorePrefixUriList() {
        return Collections.emptyList();
    }

    protected boolean passMe(HttpServletRequest req) {
        return false;
    }

    protected String getName() {
        return "AbstractTokenInterceptor";
    }

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
        String uri = req.getRequestURI();

        if (matchByUriList(getIgnoreUriList(), uri) || matchByPrefixUriList(getIgnorePrefixUriList(), uri)) {
            log.debug("{} ignore: {}", getName(), uri);
            return true;
        }

        if (passMe(req)) {
            log.debug("{} pass me: {}", getName(), uri);
            return true;
        }

        if (matchByUriList(getUriList(), uri) || matchByPrefixUriList(getPrefixUriList(), uri)) {
            log.info("{} intercept: {} {}", getName(), uri, req.getMethod());
            T token = this.extraToken(req);
            return this.handleToken(token, req, resp);
        }

        log.debug("{} not interceptor, default pass : {}", getName(), uri);
        return true;
    }

    private boolean matchByUriList(List urlList, String uri) {
        return urlList.stream().anyMatch(uri::equalsIgnoreCase);
    }

    private boolean matchByPrefixUriList(List urlList, String uri) {
        return urlList.stream().anyMatch(uri::startsWith);
    }

    protected abstract T extraToken(HttpServletRequest req);

    protected abstract boolean handleToken(T token, HttpServletRequest req, HttpServletResponse resp) throws Exception;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy