|
|
|
@ -1,6 +1,10 @@ |
|
|
|
package apelet.tenantadmin.config; |
|
|
|
package apelet.tenantadmin.config; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import apelet.common.openapi.config.OpenApiProperties; |
|
|
|
|
|
|
|
import apelet.common.openapi.interceptor.OpenApiAuthInterceptor; |
|
|
|
import apelet.tenantadmin.interceptor.AuthenticationInterceptor; |
|
|
|
import apelet.tenantadmin.interceptor.AuthenticationInterceptor; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.ObjectProvider; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
|
|
@ -14,8 +18,27 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
public class InterceptorConfig implements WebMvcConfigurer { |
|
|
|
public class InterceptorConfig implements WebMvcConfigurer { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
private ObjectProvider<OpenApiAuthInterceptor> openApiAuthInterceptorProvider; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
private OpenApiProperties openApiProperties; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void addInterceptors(InterceptorRegistry registry) { |
|
|
|
public void addInterceptors(InterceptorRegistry registry) { |
|
|
|
registry.addInterceptor(new AuthenticationInterceptor()).addPathPatterns("/tenantadmin/**").addPathPatterns("/botp/**"); |
|
|
|
String openApiPrefix = openApiProperties.getUrlPrefix(); |
|
|
|
|
|
|
|
registry.addInterceptor(new AuthenticationInterceptor()) |
|
|
|
|
|
|
|
.addPathPatterns("/tenantadmin/**").addPathPatterns("/botp/**") |
|
|
|
|
|
|
|
// OpenAPI 对外开放部分不依赖应用登录态:{prefix}/v1/** 由 OpenApiAuthInterceptor 校验 accessToken,
|
|
|
|
|
|
|
|
// {prefix}/auth/token 是公开的签发接口。这里一并排除,避免调用方误带 Authorization 头时
|
|
|
|
|
|
|
|
// 被登录态拦截器抢先拦下(它注册在前,会先执行)。
|
|
|
|
|
|
|
|
.excludePathPatterns(openApiPrefix + "/v1/**") |
|
|
|
|
|
|
|
.excludePathPatterns(openApiPrefix + "/auth/**"); |
|
|
|
|
|
|
|
// OpenAPI 对外开放路由鉴权({prefix}/v1/**):校验 accessToken 并把 TokenData 注入请求上下文。
|
|
|
|
|
|
|
|
// 注意 HandlerInterceptor 即使标了 @Component 也不会被 Spring Boot 自动注册,必须在这里显式挂上;
|
|
|
|
|
|
|
|
// common-openapi.operationEnabled=false 时该 bean 不存在,getIfAvailable() 返回 null,跳过注册即可。
|
|
|
|
|
|
|
|
OpenApiAuthInterceptor openApiAuthInterceptor = openApiAuthInterceptorProvider.getIfAvailable(); |
|
|
|
|
|
|
|
if (openApiAuthInterceptor != null) { |
|
|
|
|
|
|
|
registry.addInterceptor(openApiAuthInterceptor).addPathPatterns(openApiPrefix + "/v1/**"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|