| | |
| | | package com.tcm.training.admin.config; |
| | | |
| | | import com.tcm.training.admin.app.auth.support.StpAppUserUtil; |
| | | import cn.dev33.satoken.interceptor.SaInterceptor; |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import com.tcm.training.admin.common.xss.XssParamInterceptor; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import com.tcm.training.admin.upload.LocalUploadProperties; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.core.env.Environment; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| | | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | import java.io.File; |
| | | import java.util.Arrays; |
| | | |
| | | @Configuration |
| | | public class SaTokenWebConfig implements WebMvcConfigurer { |
| | | |
| | | private final XssParamInterceptor xssParamInterceptor; |
| | | private final LocalUploadProperties localUploadProperties; |
| | | private final Environment environment; |
| | | |
| | | @Value("${app.upload.dir:uploads}") |
| | | private String uploadDir; |
| | | |
| | | @Value("${app.upload.path-prefix:/uploads}") |
| | | private String pathPrefix; |
| | | |
| | | public SaTokenWebConfig(XssParamInterceptor xssParamInterceptor) { |
| | | public SaTokenWebConfig(XssParamInterceptor xssParamInterceptor, |
| | | LocalUploadProperties localUploadProperties, |
| | | Environment environment) { |
| | | this.xssParamInterceptor = xssParamInterceptor; |
| | | this.localUploadProperties = localUploadProperties; |
| | | this.environment = environment; |
| | | } |
| | | |
| | | /** |
| | |
| | | registry.addInterceptor(new SaInterceptor(handle -> handleAuth())) |
| | | .addPathPatterns("/api/**", "/api/v1/**") |
| | | .excludePathPatterns( |
| | | pathPrefix + "/**" |
| | | localUploadProperties.getPathPrefix() + "/**" |
| | | ); |
| | | |
| | | registry.addInterceptor(xssParamInterceptor) |
| | |
| | | */ |
| | | private boolean isPublicAdminApi(String requestPath) { |
| | | return "/api/auth/login".equals(requestPath) |
| | | || "/api/auth/captcha".equals(requestPath); |
| | | || "/api/auth/captcha".equals(requestPath) |
| | | || (!isProdProfile() && isJpushManualPushApi(requestPath)) |
| | | || requestPath.startsWith("/api/open/") |
| | | || requestPath.startsWith("/api/v1/open/"); |
| | | } |
| | | |
| | | /** |
| | | * 判断当前后台路由是否属于极光手动推送联调接口。 |
| | | */ |
| | | private boolean isJpushManualPushApi(String requestPath) { |
| | | return "/api/jpush/manual-push".equals(requestPath) |
| | | || "/api/v1/jpush/manual-push".equals(requestPath) |
| | | || requestPath.startsWith("/api/jpush/manual-push/") |
| | | || requestPath.startsWith("/api/v1/jpush/manual-push/"); |
| | | } |
| | | |
| | | /** |
| | | * 判断当前是否为生产环境,生产环境手动推送接口必须校验后台登录态。 |
| | | */ |
| | | private boolean isProdProfile() { |
| | | return Arrays.asList(environment.getActiveProfiles()).contains("prod"); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| | | // 转为绝对路径,确保 file: 协议正确。 |
| | | String absPath = new File(uploadDir).getAbsolutePath(); |
| | | registry.addResourceHandler(pathPrefix + "/**") |
| | | String absPath = new File(localUploadProperties.getDir()).getAbsolutePath(); |
| | | registry.addResourceHandler(localUploadProperties.getPathPrefix() + "/**") |
| | | .addResourceLocations("file:" + absPath + "/"); |
| | | } |
| | | } |