package top.awinx.stu01.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.awinx.stu01.interceptor.JwtInterceptor;

/**
 * SpringMVC配置：注册JWT拦截器+跨域配置（适配Spring Boot 3.x）
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 注册JWT拦截器Bean
     */
    @Bean
    public JwtInterceptor jwtInterceptor() {
        return new JwtInterceptor();
    }

    /**
     * 配置拦截规则：放行登录接口，拦截其他/api/**接口
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor())
                .addPathPatterns("/**") // 拦截所有/api开头的接口
                .excludePathPatterns("/login"); // 放行登录接口
    }
}
