policeSecurity/policeSecurityServer/src/main/java/com/changhu/config/WebConfig.java

88 lines
3.5 KiB
Java
Raw Normal View History

2024-08-29 17:06:00 +08:00
package com.changhu.config;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.changhu.support.interceptor.JsonBodyInterceptor;
2024-10-18 17:25:19 +08:00
import com.changhu.support.interceptor.OpenApiInterceptor;
import com.changhu.support.interceptor.UserTypeInterceptor;
2024-08-29 17:06:00 +08:00
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Configuration;
2024-10-18 17:25:19 +08:00
import org.springframework.web.servlet.config.annotation.CorsRegistry;
2024-08-29 17:06:00 +08:00
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.util.ArrayList;
import java.util.List;
/**
* author: luozhun
* desc: WebConfig
* createTime: 2023/8/18 10:56
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final List<String> whiteList = new ArrayList<>();
public WebConfig() {
whiteList.add("/common/**");
2024-10-18 17:25:19 +08:00
whiteList.add("/open/**");
2024-08-29 17:06:00 +08:00
whiteList.add("/test/**");
whiteList.add("/login");
whiteList.add("/logout");
whiteList.add("/favicon.ico");
//druid console
whiteList.add("/druid/**");
//knife4j
whiteList.add("/doc.html/**");
whiteList.add("/static/**");
whiteList.add("/swagger-resources");
whiteList.add("/**webjars/**");
whiteList.add("/v3/**");
//获取企业注册审核状态
whiteList.add("/management/getCheckStatus");
//微信小程序注册
whiteList.add("/miniProgramUser/register");
2024-09-20 14:51:27 +08:00
//二维码表单录入保安人员
whiteList.add("/miniProgramUser/qrCodeFormInputSecurityUser");
2024-08-29 17:06:00 +08:00
}
@Override
public void addInterceptors(@NotNull InterceptorRegistry registry) {
// 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验。
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
.addPathPatterns("/**")
.excludePathPatterns(whiteList);
// 注册jsonBody 拦截器 用于标识是否需要JsonResult返回
registry.addInterceptor(new JsonBodyInterceptor());
// 注册clientType 拦截器 用于校验当前用户是否匹配操作客户端
2024-09-06 14:54:14 +08:00
registry.addInterceptor(new UserTypeInterceptor());
2024-10-18 17:25:19 +08:00
// 注册开放接口 拦截器 用于校验第三方是否携带指定apiKey
registry.addInterceptor(new OpenApiInterceptor())
.addPathPatterns("/open/**");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "OPTION", "PUT", "DELETE")
.allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method",
"Access-Control-Request-Headers", "Authorization","Token","*")
.allowCredentials(true)
.maxAge(3600);
2024-08-29 17:06:00 +08:00
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}