package com.guo.learningprogresstracker.config; import cn.dev33.satoken.interceptor.SaInterceptor; import cn.dev33.satoken.stp.StpUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.Arrays; @Profile({"prod", "uat"}) @Configuration @RequiredArgsConstructor @Slf4j public class WebMvcProdConfig implements WebMvcConfigurer { private final CorsProperties corsProperties; @Override public void addCorsMappings(CorsRegistry registry) { log.info("允许cors的地址配置:{}", Arrays.toString(corsProperties.getAllowedOrigins())); registry.addMapping("/**") .allowedOrigins(corsProperties.getAllowedOrigins()) .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*") .allowCredentials(true); } // 注册拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { // 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验。 registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin())) .addPathPatterns("/**") .excludePathPatterns("/login"); } }