package com.jwipc.security.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.jwipc.security.prop.SecurityProp;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Configuration
@RequiredArgsConstructor
public class SaTokenConfig implements WebMvcConfigurer {
    private final SecurityProp securityProp;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        final String[] authUrls = securityProp.getAuthUrls();
        final String[] ignoreUrls = securityProp.getIgnoreUrls();
        registry.addInterceptor(new SaInterceptor(handler -> {
            // 指定一条 match 规则

            SaRouter
                    .match(authUrls)    // 拦截的 path 列表，可以写多个 */
                    .notMatch(ignoreUrls)        // 排除掉的 path 列表，可以写多个
                    .check(r -> StpUtil.checkLogin());        // 要执行的校验动作，可以写完整的 lambda 表达式
        })).addPathPatterns("/**").excludePathPatterns("/static/**");
    }
}
