Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
a1e80054
Commit
a1e80054
authored
Mar 01, 2021
by
Zheng Jie
Browse files
[代码优化](v2.6):update swagger
parent
8bb800c6
Changes
1
Show whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/config/SwaggerConfig.java
View file @
a1e80054
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
me.zhengjie.config
;
package
me.zhengjie.config
;
import
com.fasterxml.classmate.TypeResolver
;
import
com.fasterxml.classmate.TypeResolver
;
import
com.google.common.base.Predicates
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
lombok.Data
;
...
@@ -33,10 +34,8 @@ import springfox.documentation.spi.DocumentationType;
...
@@ -33,10 +34,8 @@ import springfox.documentation.spi.DocumentationType;
import
springfox.documentation.spi.service.contexts.SecurityContext
;
import
springfox.documentation.spi.service.contexts.SecurityContext
;
import
springfox.documentation.spring.web.plugins.Docket
;
import
springfox.documentation.spring.web.plugins.Docket
;
import
springfox.documentation.swagger2.annotations.EnableSwagger2
;
import
springfox.documentation.swagger2.annotations.EnableSwagger2
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
static
com
.
google
.
common
.
collect
.
Lists
.
newArrayList
;
import
static
com
.
google
.
common
.
collect
.
Lists
.
newArrayList
;
import
static
springfox
.
documentation
.
schema
.
AlternateTypeRules
.
newRule
;
import
static
springfox
.
documentation
.
schema
.
AlternateTypeRules
.
newRule
;
...
@@ -52,32 +51,20 @@ public class SwaggerConfig {
...
@@ -52,32 +51,20 @@ public class SwaggerConfig {
@Value
(
"${jwt.header}"
)
@Value
(
"${jwt.header}"
)
private
String
tokenHeader
;
private
String
tokenHeader
;
@Value
(
"${jwt.token-start-with}"
)
private
String
tokenStartWith
;
@Value
(
"${swagger.enabled}"
)
@Value
(
"${swagger.enabled}"
)
private
Boolean
enabled
;
private
Boolean
enabled
;
@Bean
@Bean
@SuppressWarnings
(
"all"
)
@SuppressWarnings
(
"all"
)
public
Docket
createRestApi
()
{
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());
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
.
enable
(
enabled
)
.
enable
(
enabled
)
.
pathMapping
(
"/"
)
.
apiInfo
(
apiInfo
())
.
apiInfo
(
apiInfo
())
.
select
()
.
select
()
//
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.
paths
(
Predicates
.
not
(
PathSelectors
.
regex
(
"/error.*"
)))
.
paths
(
PathSelectors
.
any
())
.
paths
(
PathSelectors
.
any
())
.
build
()
.
build
()
// .globalOperationParameters(pars)
//添加登陆认证
//添加登陆认证
.
securitySchemes
(
securitySchemes
())
.
securitySchemes
(
securitySchemes
())
.
securityContexts
(
securityContexts
());
.
securityContexts
(
securityContexts
());
...
@@ -87,7 +74,7 @@ public class SwaggerConfig {
...
@@ -87,7 +74,7 @@ public class SwaggerConfig {
return
new
ApiInfoBuilder
()
return
new
ApiInfoBuilder
()
.
description
(
"一个简单且易上手的 Spring boot 后台管理框架"
)
.
description
(
"一个简单且易上手的 Spring boot 后台管理框架"
)
.
title
(
"EL-ADMIN 接口文档"
)
.
title
(
"EL-ADMIN 接口文档"
)
.
version
(
"2.
4
"
)
.
version
(
"2.
6
"
)
.
build
();
.
build
();
}
}
...
@@ -104,14 +91,14 @@ public class SwaggerConfig {
...
@@ -104,14 +91,14 @@ public class SwaggerConfig {
List
<
SecurityContext
>
securityContexts
=
new
ArrayList
<>();
List
<
SecurityContext
>
securityContexts
=
new
ArrayList
<>();
// ^(?!auth).*$ 表示所有包含auth的接口不需要使用securitySchemes即不需要带token
// ^(?!auth).*$ 表示所有包含auth的接口不需要使用securitySchemes即不需要带token
// ^标识开始 ()里是一子表达式 ?!/auth表示匹配不是/auth的位置,匹配上则添加请求头,注意路径已/开头 .表示任意字符 *表示前面的字符匹配多次 $标识结束
// ^标识开始 ()里是一子表达式 ?!/auth表示匹配不是/auth的位置,匹配上则添加请求头,注意路径已/开头 .表示任意字符 *表示前面的字符匹配多次 $标识结束
securityContexts
.
add
(
getContextByPath
(
"^(?!/auth).*$"
));
securityContexts
.
add
(
getContextByPath
());
return
securityContexts
;
return
securityContexts
;
}
}
private
SecurityContext
getContextByPath
(
String
pathRegex
)
{
private
SecurityContext
getContextByPath
()
{
return
SecurityContext
.
builder
()
return
SecurityContext
.
builder
()
.
securityReferences
(
defaultAuth
())
.
securityReferences
(
defaultAuth
())
.
forPaths
(
PathSelectors
.
regex
(
pathRegex
))
.
forPaths
(
PathSelectors
.
regex
(
"^(?!/auth).*$"
))
.
build
();
.
build
();
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment