package com.eshore.gringotts.web.configuration;

import cn.dev33.satoken.interceptor.SaInterceptor;
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;

/**
 * web配置
 *
 * @author wn
 * @since  2024-11-14
 */
@Slf4j
@Configuration
public class SaTokenConfiguration implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
                .addInterceptor(
                        new SaInterceptor(handler -> {
                            SaRouter
                                    .match("/**")
                                    .notMatch("/")
                                    .notMatch("/index.html")
                                    .notMatch("/assets/**")
                                    .notMatch("/pages/**")
                                    .notMatch("/user/**")
                                    .notMatch("/upload/download/**")
                                    .check(r -> {
                                        try {
                                            StpUtil.checkLogin();
                                        } catch (Exception e) {
                                            throw new RuntimeException("账号未登陆", e);
                                        }
                                    });
                        })
                )
                .addPathPatterns("/**");
    }
}
