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

cn.bootx.platform.starter.auth.handler.SaRouteHandler Maven / Gradle / Ivy

There is a newer version: 1.3.6.2
Show newest version
package cn.bootx.platform.starter.auth.handler;

import cn.bootx.platform.starter.auth.authentication.RouterCheck;
import cn.bootx.platform.starter.auth.exception.RouterCheckException;
import cn.bootx.platform.common.spring.util.WebServletUtil;
import cn.dev33.satoken.fun.SaFunction;
import cn.dev33.satoken.router.SaRouter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;

/**
 * 鉴权路由配置类
 *
 * @author xxm
 * @since 2021/8/2
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SaRouteHandler implements InitializingBean {

    private final List routerChecks;

    @Override
    public void afterPropertiesSet() {
        // 排序
        routerChecks.sort(Comparator.comparing(RouterCheck::sortNo));
    }

    /**
     * 路由检查处理方法
     */
    public SaFunction check(Object handler) {
        return () -> {
            boolean check = routerChecks.stream().anyMatch(routerCheck -> routerCheck.check(handler));
            if (check) {
                SaRouter.stop();
            }
            else {
                log.warn("{} 没有对应的权限", WebServletUtil.getPath());
                throw new RouterCheckException();
            }
        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy