Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Pig
提交
1799facc
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,发现更多精彩内容 >>
提交
1799facc
编写于
5月 18, 2022
作者:
E
edgar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
swagger 开启basic 认证模式
上级
2fe41aee
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
204 addition
and
14 deletion
+204
-14
pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GatewayConfiguration.java
...om/pig4cloud/pig/gateway/config/GatewayConfiguration.java
+8
-0
pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SpringDocConfiguration.java
.../pig4cloud/pig/gateway/config/SpringDocConfiguration.java
+2
-14
pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SwaggerProperties.java
...a/com/pig4cloud/pig/gateway/config/SwaggerProperties.java
+118
-0
pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/SwaggerBasicGatewayFilter.java
...g4cloud/pig/gateway/filter/SwaggerBasicGatewayFilter.java
+76
-0
未找到文件。
pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GatewayConfiguration.java
浏览文件 @
1799facc
...
...
@@ -3,9 +3,11 @@ package com.pig4cloud.pig.gateway.config;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.pig4cloud.pig.gateway.filter.PasswordDecoderFilter
;
import
com.pig4cloud.pig.gateway.filter.PigRequestGlobalFilter
;
import
com.pig4cloud.pig.gateway.filter.SwaggerBasicGatewayFilter
;
import
com.pig4cloud.pig.gateway.filter.ValidateCodeGatewayFilter
;
import
com.pig4cloud.pig.gateway.handler.GlobalExceptionHandler
;
import
com.pig4cloud.pig.gateway.handler.ImageCodeHandler
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -30,6 +32,12 @@ public class GatewayConfiguration {
return
new
PigRequestGlobalFilter
();
}
@Bean
@ConditionalOnProperty
(
name
=
"swagger.basic.enable"
)
public
SwaggerBasicGatewayFilter
swaggerBasicGatewayFilter
(
SwaggerProperties
swaggerProperties
){
return
new
SwaggerBasicGatewayFilter
(
swaggerProperties
);
}
@Bean
public
ValidateCodeGatewayFilter
validateCodeGatewayFilter
(
GatewayConfigProperties
configProperties
,
ObjectMapper
objectMapper
,
RedisTemplate
redisTemplate
)
{
...
...
pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SpringDocConfiguration.java
浏览文件 @
1799facc
package
com.pig4cloud.pig.gateway.config
;
import
lombok.Data
;
import
org.springdoc.core.GroupedOpenApi
;
import
org.springdoc.core.SwaggerUiConfigParameters
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Lazy
;
import
org.springframework.stereotype.Component
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author lengleng
...
...
@@ -27,21 +23,13 @@ public class SpringDocConfiguration {
@Lazy
(
false
)
@ConditionalOnProperty
(
name
=
"springdoc.api-docs.enabled"
,
matchIfMissing
=
true
)
public
List
<
GroupedOpenApi
>
apis
(
SwaggerUiConfigParameters
swaggerUiConfigParameters
,
SwaggerProperties
p
roperties
)
{
SwaggerProperties
swaggerP
roperties
)
{
List
<
GroupedOpenApi
>
groups
=
new
ArrayList
<>();
for
(
String
value
:
properties
.
services
.
values
())
{
for
(
String
value
:
swaggerProperties
.
getServices
()
.
values
())
{
swaggerUiConfigParameters
.
addGroup
(
value
);
}
return
groups
;
}
@Data
@Component
@ConfigurationProperties
(
"swagger"
)
class
SwaggerProperties
{
private
Map
<
String
,
String
>
services
;
}
}
pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SwaggerProperties.java
0 → 100644
浏览文件 @
1799facc
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package
com.pig4cloud.pig.gateway.config
;
import
lombok.Data
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
/**
* SwaggerProperties
*
* swagger:
* basic:
* enable: true
* username: admin
* password: admin
*
* @author edgar
*/
@Data
@Component
@ConfigurationProperties
(
"swagger"
)
public
class
SwaggerProperties
{
/**
* 是否开启swagger
*/
private
Boolean
enabled
=
true
;
/**
* swagger会解析的包路径
**/
private
String
basePackage
=
""
;
/**
* swagger会解析的url规则
**/
private
List
<
String
>
basePath
=
new
ArrayList
<>();
/**
* 在basePath基础上需要排除的url规则
**/
private
List
<
String
>
excludePath
=
new
ArrayList
<>();
/**
* 需要排除的服务
*/
private
List
<
String
>
ignoreProviders
=
new
ArrayList
<>();
/**
* 标题
**/
private
String
title
=
""
;
/**
* 网关
*/
private
String
gateway
;
/**
* 获取token
*/
private
String
tokenUrl
;
/**
* 作用域
*/
private
String
scope
;
private
Map
<
String
,
String
>
services
;
/**
* 认证参数
*/
private
SwaggerBasic
basic
=
new
SwaggerBasic
();
@Data
public
static
class
SwaggerBasic
{
/**
* 是否开启 basic 认证
*/
private
Boolean
enabled
;
/**
* 用户名
*/
private
String
username
;
/**
* 密码
*/
private
String
password
;
}
}
pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/SwaggerBasicGatewayFilter.java
0 → 100644
浏览文件 @
1799facc
package
com.pig4cloud.pig.gateway.filter
;
import
com.pig4cloud.pig.gateway.config.SwaggerProperties
;
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 edgar
* @date 2022/5/8
*/
@Slf4j
@RequiredArgsConstructor
public
class
SwaggerBasicGatewayFilter
implements
GlobalFilter
{
private
static
final
String
API_URI
=
"/v3/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
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录