Skip to content

Commit

Permalink
Spring Boot版本升级2.2.10,解决in 查询传null时报错。调整Swagger中token设置方式 (elunez#595)
Browse files Browse the repository at this point in the history
* Spring Boot版本调整为2.2.10。在此前的版本,若in 查询传入null,会报错。相关sql 为in ()。在2.2版本修复了此问题,相关sql改为 in (null)。鉴于此,建议进行升级。

* Spring Boot依赖升级后,Sort的设置方式调整。由 new Sort()改为 Sort.by()

* 调整接口文档Swagger中,token的设置方式,由每个接口单独设置,改为设置统一的token,日常测试更加方便
  • Loading branch information
lWoHvYe authored Mar 1, 2021
1 parent 7b0caae commit 8bb800c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
67 changes: 51 additions & 16 deletions eladmin-common/src/main/java/me/zhengjie/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package me.zhengjie.config;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Predicates;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
Expand All @@ -26,18 +25,18 @@
import org.springframework.core.Ordered;
import org.springframework.data.domain.Pageable;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.AlternateTypeRuleConvention;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.schema.AlternateTypeRules.newRule;

Expand All @@ -62,22 +61,26 @@ public class SwaggerConfig {
@Bean
@SuppressWarnings("all")
public Docket createRestApi() {
ParameterBuilder ticketPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
ticketPar.name(tokenHeader).description("token")
.modelRef(new ModelRef("string"))
.parameterType("header")
.defaultValue(tokenStartWith + " ")
.required(true)
.build();
pars.add(ticketPar.build());
// ParameterBuilder ticketPar = new ParameterBuilder();
//// List<Parameter> pars = new ArrayList<>();
//// ticketPar.name(tokenHeader).description("token")
//// .modelRef(new ModelRef("string"))
//// .parameterType("header")
//// .defaultValue(tokenStartWith + " ")
//// .required(true)
//// .build();
// pars.add(ticketPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.enable(enabled)
.apiInfo(apiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/error.*")))
// .paths(Predicates.not(PathSelectors.regex("/error.*")))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars);
// .globalOperationParameters(pars)
//添加登陆认证
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}

private ApiInfo apiInfo() {
Expand All @@ -88,6 +91,38 @@ private ApiInfo apiInfo() {
.build();
}

private List<SecurityScheme> securitySchemes() {
//设置请求头信息
List<SecurityScheme> securitySchemes = new ArrayList<>();
ApiKey apiKey = new ApiKey(tokenHeader, tokenHeader, "header");
securitySchemes.add(apiKey);
return securitySchemes;
}

private List<SecurityContext> securityContexts() {
//设置需要登录认证的路径
List<SecurityContext> securityContexts = new ArrayList<>();
// ^(?!auth).*$ 表示所有包含auth的接口不需要使用securitySchemes即不需要带token
// ^标识开始 ()里是一子表达式 ?!/auth表示匹配不是/auth的位置,匹配上则添加请求头,注意路径已/开头 .表示任意字符 *表示前面的字符匹配多次 $标识结束
securityContexts.add(getContextByPath("^(?!/auth).*$"));
return securityContexts;
}

private SecurityContext getContextByPath(String pathRegex) {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex(pathRegex))
.build();
}

private List<SecurityReference> defaultAuth() {
List<SecurityReference> securityReferences = new ArrayList<>();
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
securityReferences.add(new SecurityReference(tokenHeader, authorizationScopes));
return securityReferences;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class DeptServiceImpl implements DeptService {

@Override
public List<DeptDto> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception {
Sort sort = new Sort(Sort.Direction.ASC, "deptSort");
Sort sort = Sort.by(Sort.Direction.ASC, "deptSort");
String dataScopeType = SecurityUtils.getDataScopeType();
if (isQuery) {
if(dataScopeType.equals(DataScopeEnum.ALL.getValue())){
Expand Down Expand Up @@ -278,4 +278,4 @@ public void delCaches(Long id){
redisUtils.delByKeys(CacheKey.DATA_USER, users.stream().map(User::getId).collect(Collectors.toSet()));
redisUtils.del(CacheKey.DEPT_ID + id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class MenuServiceImpl implements MenuService {

@Override
public List<MenuDto> queryAll(MenuQueryCriteria criteria, Boolean isQuery) throws Exception {
Sort sort = new Sort(Sort.Direction.ASC, "menuSort");
Sort sort = Sort.by(Sort.Direction.ASC, "menuSort");
if(isQuery){
criteria.setPidIsNull(true);
List<Field> fields = QueryHelp.getAllFields(criteria.getClass(), new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class RoleServiceImpl implements RoleService {

@Override
public List<RoleDto> queryAll() {
Sort sort = new Sort(Sort.Direction.ASC, "level");
Sort sort = Sort.by(Sort.Direction.ASC, "level");
return roleMapper.toDto(roleRepository.findAll(sort));
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<version>2.2.10.RELEASE</version>
</parent>

<properties>
Expand Down

0 comments on commit 8bb800c

Please sign in to comment.