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
wwwanlingxiao
mall
Commits
cfa9871b
Commit
cfa9871b
authored
Apr 13, 2019
by
macro
Browse files
swagger添加登陆认证
parent
1bf9a4b7
Changes
1
Hide whitespace changes
Inline
Side-by-side
mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java
View file @
cfa9871b
...
...
@@ -6,10 +6,17 @@ import springfox.documentation.builders.ApiInfoBuilder;
import
springfox.documentation.builders.PathSelectors
;
import
springfox.documentation.builders.RequestHandlerSelectors
;
import
springfox.documentation.service.ApiInfo
;
import
springfox.documentation.service.ApiKey
;
import
springfox.documentation.service.AuthorizationScope
;
import
springfox.documentation.service.SecurityReference
;
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
;
/**
* Swagger2API文档的配置
* Created by macro on 2018/4/26.
...
...
@@ -24,7 +31,9 @@ public class Swagger2Config {
.
select
()
.
apis
(
RequestHandlerSelectors
.
basePackage
(
"com.macro.mall.controller"
))
.
paths
(
PathSelectors
.
any
())
.
build
();
.
build
()
.
securitySchemes
(
securitySchemes
())
.
securityContexts
(
securityContexts
());
}
private
ApiInfo
apiInfo
()
{
...
...
@@ -35,4 +44,37 @@ public class Swagger2Config {
.
version
(
"1.0"
)
.
build
();
}
private
List
<
ApiKey
>
securitySchemes
()
{
//设置请求头信息
List
<
ApiKey
>
result
=
new
ArrayList
<>();
ApiKey
apiKey
=
new
ApiKey
(
"Authorization"
,
"Authorization"
,
"header"
);
result
.
add
(
apiKey
);
return
result
;
}
private
List
<
SecurityContext
>
securityContexts
()
{
//设置需要登录认证的路径
List
<
SecurityContext
>
result
=
new
ArrayList
<>();
result
.
add
(
getContextByPath
(
"/brand/.*"
));
result
.
add
(
getContextByPath
(
"/product/.*"
));
result
.
add
(
getContextByPath
(
"/productCategory/.*"
));
return
result
;
}
private
SecurityContext
getContextByPath
(
String
pathRegex
){
return
SecurityContext
.
builder
()
.
securityReferences
(
defaultAuth
())
.
forPaths
(
PathSelectors
.
regex
(
pathRegex
))
.
build
();
}
List
<
SecurityReference
>
defaultAuth
()
{
List
<
SecurityReference
>
result
=
new
ArrayList
<>();
AuthorizationScope
authorizationScope
=
new
AuthorizationScope
(
"global"
,
"accessEverything"
);
AuthorizationScope
[]
authorizationScopes
=
new
AuthorizationScope
[
1
];
authorizationScopes
[
0
]
=
authorizationScope
;
result
.
add
(
new
SecurityReference
(
"Authorization"
,
authorizationScopes
));
return
result
;
}
}
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