Files
lpt-be/src/main/java/com/guo/learningprogresstracker/config/WebMvcProdConfig.java
T
cat-shark c0fe137c48 fix(后端):@Transactional + WebMvcProdConfig OPTIONS 跳过
- WebMvcProdConfig:SaInterceptor 增加 OPTIONS 预检跳过,
  解决生产环境 CORS 预检 401 问题
- StudySessionsServiceImpl.endedStudySession/continueStudySession:
  增加 @Transactional
- StudyReportFragmentsServiceImpl.createFragments:
  增加 @Transactional
2026-07-04 17:38:00 +08:00

47 lines
1.6 KiB
Java

package com.guo.learningprogresstracker.config;
import cn.dev33.satoken.context.SaHolder;
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) {
registry.addInterceptor(new SaInterceptor(handle -> {
if ("OPTIONS".equalsIgnoreCase(SaHolder.getRequest().getMethod())) {
return;
}
StpUtil.checkLogin();
}))
.addPathPatterns("/**")
.excludePathPatterns("/login");
}
}