package com.yinlihupo.enlish.service.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class SaTokenConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> {
            log.info("Sa-Token 拦截器： {}", handler);


            SaRouter.match(SaHttpMethod.OPTIONS)
                    .free(r -> System.out.println("--------OPTIONS预检请求，不做处理"))
                    .back();

            SaRouter.match("/**")
                    .notMatch("/login/**")
                    .notMatch("/plan/word/voice")
                    .notMatch("/plan/word/voice/tts")
                    .check(r -> StpUtil.checkLogin());

            SaRouter.match("/admin/**")
                    .notMatch("/plan/word/voice")
                    .notMatch("/plan/word/voice/tts")
                    .check(r -> StpUtil.checkRole("root"));

        }))
        .addPathPatterns("/**")
        .excludePathPatterns("/error");
    }
}
