Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Pig
提交
f8e70d4e
P
Pig
项目概览
int
/
Pig
上一次同步 大约 1 年
通知
8
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Pig
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f8e70d4e
编写于
8月 08, 2021
作者:
pig_冷冷
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
Introducing new features. closed #I4313X 【新功能】Swagger-ui 添加账号密码
上级
d3839397
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
111 addition
and
2 deletion
+111
-2
pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/GatewaySwaggerAutoConfiguration.java
...ommon/swagger/config/GatewaySwaggerAutoConfiguration.java
+11
-1
pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerBasicGatewayFilter.java
...pig/common/swagger/support/SwaggerBasicGatewayFilter.java
+75
-0
pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProperties.java
...g4cloud/pig/common/swagger/support/SwaggerProperties.java
+25
-1
未找到文件。
pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/GatewaySwaggerAutoConfiguration.java
浏览文件 @
f8e70d4e
...
...
@@ -2,6 +2,7 @@ package com.pig4cloud.pig.common.swagger.config;
import
com.pig4cloud.pig.common.swagger.support.*
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
;
import
org.springframework.cloud.gateway.config.GatewayProperties
;
import
org.springframework.context.annotation.Bean
;
...
...
@@ -10,7 +11,10 @@ import org.springframework.web.reactive.function.server.RequestPredicates;
import
org.springframework.web.reactive.function.server.RouterFunction
;
import
org.springframework.web.reactive.function.server.RouterFunctions
;
import
org.springframework.web.reactive.function.server.ServerResponse
;
import
springfox.documentation.swagger.web.*
;
import
springfox.documentation.swagger.web.SecurityConfiguration
;
import
springfox.documentation.swagger.web.SecurityConfigurationBuilder
;
import
springfox.documentation.swagger.web.UiConfiguration
;
import
springfox.documentation.swagger.web.UiConfigurationBuilder
;
/**
* 网关swagger 配置类,仅在webflux 环境生效哦
...
...
@@ -36,6 +40,12 @@ public class GatewaySwaggerAutoConfiguration {
return
new
WebFluxSwaggerConfiguration
();
}
@Bean
@ConditionalOnProperty
(
value
=
"swagger.basic.enabled"
,
havingValue
=
"true"
)
public
SwaggerBasicGatewayFilter
swaggerBasicGatewayFilter
(
SwaggerProperties
swaggerProperties
)
{
return
new
SwaggerBasicGatewayFilter
(
swaggerProperties
);
}
@Bean
public
SwaggerSecurityHandler
swaggerSecurityHandler
(
ObjectProvider
<
SecurityConfiguration
>
securityConfigurationObjectProvider
)
{
...
...
pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerBasicGatewayFilter.java
0 → 100644
浏览文件 @
f8e70d4e
package
com.pig4cloud.pig.common.swagger.support
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.cloud.gateway.filter.GatewayFilterChain
;
import
org.springframework.cloud.gateway.filter.GlobalFilter
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.util.Base64Utils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.server.ServerWebExchange
;
import
reactor.core.publisher.Mono
;
import
java.nio.charset.StandardCharsets
;
/**
* swagger 开启basic 认证
*
* @author Lht
* @date 2021/8/8
*/
@Slf4j
@RequiredArgsConstructor
public
class
SwaggerBasicGatewayFilter
implements
GlobalFilter
{
private
static
final
String
API_URI
=
"/v2/api-docs"
;
private
static
final
String
BASIC_PREFIX
=
"Basic "
;
private
final
SwaggerProperties
swaggerProperties
;
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
ServerHttpRequest
request
=
exchange
.
getRequest
();
if
(!
request
.
getURI
().
getPath
().
contains
(
API_URI
))
{
return
chain
.
filter
(
exchange
);
}
if
(
hasAuth
(
exchange
))
{
return
chain
.
filter
(
exchange
);
}
else
{
ServerHttpResponse
response
=
exchange
.
getResponse
();
response
.
setStatusCode
(
HttpStatus
.
UNAUTHORIZED
);
response
.
getHeaders
().
add
(
HttpHeaders
.
WWW_AUTHENTICATE
,
"Basic Realm=\"pig\""
);
return
response
.
setComplete
();
}
}
/**
* 简单的basic认证
* @param exchange 上下文
* @return 是否有权限
*/
private
boolean
hasAuth
(
ServerWebExchange
exchange
)
{
ServerHttpRequest
request
=
exchange
.
getRequest
();
String
auth
=
request
.
getHeaders
().
getFirst
(
HttpHeaders
.
AUTHORIZATION
);
log
.
info
(
"Basic认证信息为:{}"
,
auth
);
if
(!
StringUtils
.
hasText
(
auth
)
||
!
auth
.
startsWith
(
BASIC_PREFIX
))
{
return
Boolean
.
FALSE
;
}
String
username
=
swaggerProperties
.
getBasic
().
getUsername
();
String
password
=
swaggerProperties
.
getBasic
().
getPassword
();
String
encodeToString
=
Base64Utils
.
encodeToString
((
username
+
":"
+
password
).
getBytes
(
StandardCharsets
.
UTF_8
));
return
auth
.
equals
(
BASIC_PREFIX
+
encodeToString
);
}
}
pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProperties.java
浏览文件 @
f8e70d4e
...
...
@@ -19,7 +19,6 @@ package com.pig4cloud.pig.common.swagger.support;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -104,6 +103,11 @@ public class SwaggerProperties {
**/
private
Authorization
authorization
=
new
Authorization
();
/**
* 认证参数
*/
private
SwaggerBasic
basic
=
new
SwaggerBasic
();
@Data
@NoArgsConstructor
public
static
class
Contact
{
...
...
@@ -164,4 +168,24 @@ public class SwaggerProperties {
}
@Data
public
static
class
SwaggerBasic
{
/**
* 是否开启 basic 认证
*/
private
Boolean
enabled
;
/**
* 用户名
*/
private
String
username
;
/**
* 密码
*/
private
String
password
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录