update
This commit is contained in:
@@ -2,7 +2,7 @@ package com.cool.store.config;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.cool.store.config.datasource.DynamicDataSourceServiceImpl;
|
||||
import com.cool.store.model.dto.DatasourceInfoDTO;
|
||||
import com.cool.store.dto.DatasourceInfoDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -15,7 +15,7 @@ import java.util.List;
|
||||
* @author zhangchenbiao
|
||||
* @FileName: CommonBeanConfig
|
||||
* @Description:
|
||||
* @date 2022-01-25 18:41
|
||||
* @date 2023-05-19 18:41
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.cool.store.config;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.cool.store.enums.ErrorCodeEnum;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
import com.cool.store.service.context.CurrentUser;
|
||||
import com.cool.store.service.context.UserContext;
|
||||
import com.cool.store.service.utils.DataSourceHelper;
|
||||
import com.cool.store.utils.RedisUtilPool;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author ydw
|
||||
* @Description 权限校验
|
||||
* @date 2020/1/15
|
||||
*/
|
||||
@Component
|
||||
@Order(3)
|
||||
@Slf4j
|
||||
public class TokenValidateFilter implements Filter {
|
||||
|
||||
@Autowired
|
||||
private RedisUtilPool redisUtilPool;
|
||||
|
||||
private static AntPathMatcher matcher = new AntPathMatcher();
|
||||
|
||||
private static List<String> patternList =
|
||||
|
||||
Lists.newArrayList("/web/check/ok","/check/ok", "/partner/doc.html",
|
||||
"/**/swagger*/**",
|
||||
"/**/webjars/**");
|
||||
|
||||
|
||||
/**
|
||||
* @param uri
|
||||
* @return boolean
|
||||
* @throws
|
||||
* @Title excludePath
|
||||
* @Description 是否是放行的请求
|
||||
*/
|
||||
private boolean excludePath(String uri) {
|
||||
for (String pattern : patternList) {
|
||||
if (matcher.match(pattern, uri)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
HttpServletResponse response = (HttpServletResponse) servletResponse;
|
||||
HttpServletRequest reqs = (HttpServletRequest) servletRequest;
|
||||
String uri = reqs.getRequestURI();
|
||||
String method = reqs.getMethod();
|
||||
String userStr = "";
|
||||
CurrentUser currentUser = null;
|
||||
boolean isInWhiteList = excludePath(uri);
|
||||
String accessToken = reqs.getParameter("access_token");
|
||||
String key = "access_token:" + accessToken;
|
||||
if(StringUtils.isNotBlank(accessToken)){
|
||||
userStr = redisUtilPool.getString(key);
|
||||
if(StringUtils.isNotBlank(userStr)){
|
||||
currentUser = JSON.parseObject(userStr, CurrentUser.class);
|
||||
}
|
||||
}
|
||||
log.info("url:{}", uri);
|
||||
if ( !isInWhiteList && !method.equals("OPTIONS")) {
|
||||
if (StringUtils.isEmpty(accessToken)) {
|
||||
response.setStatus(HttpStatus.OK.value());
|
||||
response.getWriter().write(JSON.toJSONString(
|
||||
ResponseResult.fail(ErrorCodeEnum.ACCESS_TOKEN_INVALID)));
|
||||
return;
|
||||
}
|
||||
if (Objects.isNull(currentUser)) {
|
||||
response.setStatus(HttpStatus.OK.value());
|
||||
response.getWriter().write(JSON.toJSONString(
|
||||
ResponseResult.fail(ErrorCodeEnum.ACCESS_TOKEN_INVALID)));
|
||||
return;
|
||||
}
|
||||
log.info("url:{}, access_token:{}, userId:{}, username:{}, enterpriseId:{}", uri, accessToken, currentUser.getUserId(), currentUser.getName(), currentUser.getEnterpriseId());
|
||||
}
|
||||
if(StringUtils.isBlank(userStr) && !isInWhiteList){
|
||||
response.setStatus(HttpStatus.OK.value());
|
||||
response.getWriter().write(JSON.toJSONString(
|
||||
ResponseResult.fail(ErrorCodeEnum.ACCESS_TOKEN_INVALID)));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
UserContext.setUser(userStr);
|
||||
DataSourceHelper.changeToMy();
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
} finally {
|
||||
UserContext.removeUser();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.cool.store.config.datasource;
|
||||
|
||||
import com.cool.store.model.constants.CommonConstants;
|
||||
import com.cool.store.constants.CommonConstants;
|
||||
import com.cool.store.service.context.DataSourceContext;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.cool.store.config.datasource;
|
||||
|
||||
import com.cool.store.model.constants.CommonConstants;
|
||||
import com.cool.store.model.dto.DatasourceInfoDTO;
|
||||
import com.cool.store.model.entity.EnterpriseConfigDO;
|
||||
import com.cool.store.model.utils.RedisUtilPool;
|
||||
import com.cool.store.constants.CommonConstants;
|
||||
import com.cool.store.dto.DatasourceInfoDTO;
|
||||
import com.cool.store.entity.EnterpriseConfigDO;
|
||||
import com.cool.store.utils.RedisUtilPool;
|
||||
import com.cool.store.service.EnterpriseConfigService;
|
||||
import com.cool.store.service.context.DataSourceContext;
|
||||
import com.github.pagehelper.Page;
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.cool.store.config.swagger;
|
||||
|
||||
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import springfox.documentation.RequestHandler;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.ParameterBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.schema.ModelRef;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.service.Parameter;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zhangchenbiao
|
||||
* @description:
|
||||
* @date 2023/05/15 02:52
|
||||
*/
|
||||
@Profile({"local", "dev", "ab", "test"})
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
@EnableKnife4j
|
||||
public class Swagger2Config {
|
||||
|
||||
/**
|
||||
* 扫描接口地址的包名
|
||||
*/
|
||||
public static final String BASE_PACKAGE = "com.cool.store.controller";
|
||||
|
||||
private ApiInfo getApiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title("coolstore-partner-manage")
|
||||
.description("接口文档")
|
||||
.version("1.0")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Docket createDocket (String groupName, String... packages){
|
||||
List<Parameter> pars = getParameters();
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.enable(Boolean.TRUE)
|
||||
.apiInfo(this.getApiInfo())
|
||||
.groupName(groupName)
|
||||
.select()
|
||||
.apis(this.scanBasePackage(packages))
|
||||
.build()
|
||||
.globalOperationParameters(pars);
|
||||
}
|
||||
|
||||
private Docket createDocketByPath (String groupName, String... paths){
|
||||
List<Parameter> pars = getParameters();
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.enable(Boolean.TRUE)
|
||||
.apiInfo(this.getApiInfo())
|
||||
.groupName(groupName)
|
||||
.select()
|
||||
.paths(this.scanPath(paths))
|
||||
.build()
|
||||
.globalOperationParameters(pars);
|
||||
}
|
||||
|
||||
private List<Parameter> getParameters() {
|
||||
List<Parameter> pars = new ArrayList<>();
|
||||
pars.add(new ParameterBuilder().name("access_token").description("令牌").required(true)
|
||||
.modelRef(new ModelRef("string"))
|
||||
.defaultValue("{{access_token}}")
|
||||
.parameterType("query").build());
|
||||
pars.add(new ParameterBuilder().name("enterprise-id").required(true)
|
||||
.modelRef(new ModelRef("string"))
|
||||
.defaultValue("45f92210375346858b6b6694967f44de")
|
||||
.parameterType("path").build());
|
||||
return pars;
|
||||
}
|
||||
|
||||
private Predicate<RequestHandler> scanBasePackage(final String... controllerPack) {
|
||||
Predicate<RequestHandler> predicate = null;
|
||||
for (String strBasePackage : controllerPack) {
|
||||
if(StringUtils.isNotBlank(strBasePackage)){
|
||||
Predicate<RequestHandler> tempPredicate = RequestHandlerSelectors.basePackage(strBasePackage);
|
||||
predicate = predicate == null ? tempPredicate : Predicates.or(tempPredicate,predicate);
|
||||
}
|
||||
}
|
||||
return predicate;
|
||||
}
|
||||
|
||||
private Predicate<String> scanPath(final String... paths) {
|
||||
Predicate<String> predicate = null;
|
||||
for (String path : paths) {
|
||||
if(StringUtils.isNotBlank(path)){
|
||||
Predicate<String> tempPredicate = PathSelectors.ant(path);
|
||||
predicate = predicate == null ? tempPredicate : Predicates.or(tempPredicate,predicate);
|
||||
}
|
||||
}
|
||||
return predicate;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public Docket allApi() {
|
||||
return this.createDocket("全部", BASE_PACKAGE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user