Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
dm_project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
姚本章
dm_project
Commits
02d5428f
Commit
02d5428f
authored
Jan 05, 2021
by
zhangc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit 添加api请求相关类
parent
7ecbcfde
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
117 additions
and
64 deletions
+117
-64
GatewayResultCode.java
...c/main/java/com/jz/dm/common/enums/GatewayResultCode.java
+5
-1
OpenApiRequest.java
...y/src/main/java/com/jz/dm/common/util/OpenApiRequest.java
+3
-3
AuthFilter.java
...apigateway/src/main/java/com/jz/dm/filter/AuthFilter.java
+1
-1
ApiInterfaceMapper.java
...ay/src/main/java/com/jz/dm/mapper/ApiInterfaceMapper.java
+2
-0
GatewayRequest.java
.../src/main/java/com/jz/dm/models/enity/GatewayRequest.java
+3
-3
ApiInterfaceService.java
.../src/main/java/com/jz/dm/service/ApiInterfaceService.java
+9
-2
AuthService.java
...igateway/src/main/java/com/jz/dm/service/AuthService.java
+2
-1
ApiInterfaceServiceImpl.java
.../java/com/jz/dm/service/impl/ApiInterfaceServiceImpl.java
+11
-3
AuthServiceImpl.java
...src/main/java/com/jz/dm/service/impl/AuthServiceImpl.java
+3
-1
ApiQueryService.java
.../main/java/com/jz/dm/service/request/ApiQueryService.java
+73
-44
ApiInterfaceMapper.xml
...igateway/src/main/resources/mapper/ApiInterfaceMapper.xml
+1
-0
TestHttpReq.java
...y/src/test/java/com/jz/dm/gateway/orther/TestHttpReq.java
+1
-1
HttpsUtils.java
...-common/src/main/java/com/jz/common/utils/HttpsUtils.java
+3
-4
No files found.
jz-dm-apigateway/src/main/java/com/jz/dm/common/enums/GatewayResultCode.java
View file @
02d5428f
...
...
@@ -66,8 +66,12 @@ public enum GatewayResultCode implements ResultCode {
API_STATUS_EXCEPTION
(
"API_STATUS_EXCEPTION"
,
"API状态异常"
),
/** 请求组织状态异常 */
ORG_STATE_EXCEPTION
(
"ORG_STATE_EXCEPTION"
,
"请求组织状态异常"
);
ORG_STATE_EXCEPTION
(
"ORG_STATE_EXCEPTION"
,
"请求组织状态异常"
),
/** 请求信息不存在 */
REQUEST_INFO_UNEXIST
(
"REQUEST_INFO_UNEXIST"
,
"请求信息不存在"
),
/** 输出类型错误 */
OUTPUT_TYPE_EXCEPTION
(
"OUTPUT_TYPE_EXCEPTION"
,
"输出类型错误!"
);
/**
* 初始化保存到map里方便根据code获取
*/
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/common/util/OpenApiRequest.java
View file @
02d5428f
...
...
@@ -16,7 +16,7 @@ import java.util.Map;
*/
public
class
OpenApiRequest
{
private
String
ap
p
Key
;
//apiKey
private
String
ap
i
Key
;
//apiKey
private
String
openApiParams
;
...
...
@@ -41,11 +41,11 @@ public class OpenApiRequest {
}
public
String
getAppKey
()
{
return
ap
p
Key
;
return
ap
i
Key
;
}
public
void
setAppKey
(
String
appKey
)
{
this
.
ap
p
Key
=
appKey
;
this
.
ap
i
Key
=
appKey
;
}
@SuppressWarnings
(
"unchecked"
)
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/filter/AuthFilter.java
View file @
02d5428f
...
...
@@ -75,7 +75,7 @@ public class AuthFilter extends AbstractFilter {
JSONObject
parameter
=
JSONObject
.
parseObject
(
request
.
getParams
());
if
(
null
!=
parameter
)
{
String
authCode
=
parameter
.
getString
(
"authCode"
);
ApiAuth
authAuth
=
authService
.
getAuthUser
(
authCode
);
ApiAuth
authAuth
=
authService
.
getAuthUser
(
authCode
,
apiInterface
.
getId
()
);
if
(
null
==
authAuth
)
{
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_NOT_AUTH
);
}
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/mapper/ApiInterfaceMapper.java
View file @
02d5428f
...
...
@@ -13,4 +13,6 @@ import com.jz.dm.models.domian.ApiInterface;
* @Description:
**/
public
interface
ApiInterfaceMapper
extends
BaseMapper
<
ApiInterface
>
{
}
jz-dm-apigateway/src/main/java/com/jz/dm/models/enity/GatewayRequest.java
View file @
02d5428f
...
...
@@ -18,9 +18,9 @@ import java.util.Map;
@Data
public
class
GatewayRequest
implements
Serializable
{
@ParamName
(
"ap
p
_key"
)
@ParamName
(
"ap
i
_key"
)
@ApiModelProperty
(
value
=
"appKey API唯一标识"
)
private
String
ap
p
Key
;
private
String
ap
i
Key
;
@ApiModelProperty
(
value
=
"接口名称"
)
private
String
method
;
...
...
@@ -88,7 +88,7 @@ public class GatewayRequest implements Serializable {
*/
@Override
public
String
toString
()
{
return
"GatewayRequest [appKey="
+
ap
p
Key
+
", method="
+
method
+
", format="
+
format
return
"GatewayRequest [appKey="
+
ap
i
Key
+
", method="
+
method
+
", format="
+
format
+
", signType="
+
signType
+
", sign="
+
sign
+
", timestamp="
+
timestamp
+
", version="
+
version
+
", params="
+
params
+
", extAttributes="
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/service/ApiInterfaceService.java
View file @
02d5428f
...
...
@@ -24,8 +24,15 @@ public interface ApiInterfaceService {
/**
* 获取api信息
* @param appKey
* @param apiKey
* @return
*/
ApiInterface
getApiInfo
(
String
apiKey
);
/**
* 获取apikey信息
* @param apiKey
* @return
*/
ApiInterface
get
ApiInfo
(
String
app
Key
);
ApiInterface
get
ReqTargetInfo
(
String
api
Key
);
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/AuthService.java
View file @
02d5428f
...
...
@@ -59,7 +59,8 @@ public interface AuthService {
/**
* 获取API授权信息
* @param authCode
* @param apiId
* @return
*/
ApiAuth
getAuthUser
(
String
authCode
);
ApiAuth
getAuthUser
(
String
authCode
,
Long
apiId
);
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/impl/ApiInterfaceServiceImpl.java
View file @
02d5428f
...
...
@@ -54,14 +54,22 @@ public class ApiInterfaceServiceImpl implements ApiInterfaceService {
/**
* 获取API信息
* @param ap
p
Key
* @param ap
i
Key
* @return
*/
@Override
public
ApiInterface
getApiInfo
(
String
appKey
)
{
public
ApiInterface
getApiInfo
(
String
apiKey
)
{
QueryWrapper
<
ApiInterface
>
query
=
new
QueryWrapper
<>();
query
.
eq
(
"is_deleted"
,
0
);
query
.
eq
(
"api_key"
,
apiKey
);
return
apiInterfaceMapper
.
selectOne
(
query
);
}
@Override
public
ApiInterface
getReqTargetInfo
(
String
apiKey
)
{
QueryWrapper
<
ApiInterface
>
query
=
new
QueryWrapper
<>();
query
.
eq
(
"api_key"
,
apiKey
);
query
.
eq
(
"is_deleted"
,
0
);
query
.
eq
(
"api_key"
,
appKey
);
return
apiInterfaceMapper
.
selectOne
(
query
);
}
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/impl/AuthServiceImpl.java
View file @
02d5428f
...
...
@@ -267,12 +267,14 @@ public class AuthServiceImpl implements AuthService {
/**
* 获取API授权信息
* @param authCode
* @param apiId
* @return
*/
@Override
public
ApiAuth
getAuthUser
(
String
authCode
)
{
public
ApiAuth
getAuthUser
(
String
authCode
,
Long
apiId
)
{
QueryWrapper
<
ApiAuth
>
query
=
new
QueryWrapper
<>();
query
.
eq
(
"auth_code"
,
authCode
);
query
.
eq
(
"api_interface_id"
,
apiId
);
query
.
eq
(
"is_deleted"
,
0
);
return
apiAuthMapper
.
selectOne
(
query
);
}
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/ApiQueryService.java
View file @
02d5428f
...
...
@@ -4,9 +4,11 @@ import com.alibaba.fastjson.JSONObject;
import
com.jz.common.utils.HttpsUtils
;
import
com.jz.common.utils.RedisUtils
;
import
com.jz.dm.common.enums.GatewayResultCode
;
import
com.jz.dm.common.exception.GatewayException
;
import
com.jz.dm.common.util.OpenApiRequest
;
import
com.jz.dm.common.util.OpenApiResponse
;
import
com.jz.dm.gateway.OpenApiService
;
import
com.jz.dm.models.domian.ApiInterface
;
import
com.jz.dm.service.ApiInterfaceService
;
import
com.jz.dm.web.annotation.AccessLimit
;
import
com.jz.dm.web.annotation.ApiLogAspect
;
...
...
@@ -47,53 +49,80 @@ public class ApiQueryService implements OpenApiService {
private
RedisUtils
redisUtils
;
@Autowired
private
HttpsUtils
httpsUtils
;
@Override
@ApiLogAspect
(
description
=
"API请求日志"
)
@AccessLimit
(
limit
=
10000
,
sec
=
1
)
@Transactional
(
rollbackFor
=
Exception
.
class
,
propagation
=
Propagation
.
REQUIRES_NEW
)
@AccessLimit
(
limit
=
10000
,
sec
=
1
)
@Transactional
(
rollbackFor
=
Exception
.
class
,
propagation
=
Propagation
.
REQUIRES_NEW
)
public
void
doService
(
OpenApiRequest
request
,
OpenApiResponse
response
)
{
JSONObject
parameter
=
JSONObject
.
parseObject
(
request
.
getOpenApiParams
());
String
apiType
=
parameter
.
getString
(
"apiType"
);
if
(
StringUtils
.
isNotBlank
(
apiType
)){
try
{
switch
(
apiType
){
case
"QUERY_TAG"
:
//标签查询
//String reqParam = parameter.getString("request_param");
//apiInterfaceService.getTagReqData();
break
;
case
"BIG_DATA_QUERY"
:
//大数据查询
break
;
case
"CUSTOM_QUERY"
:
//自定义查询(三方查询)
break
;
case
"DATA_BANK_QUERY"
:
//数据银行查询
String
reqParams
=
parameter
.
getString
(
"reqParams"
);
if
(
StringUtils
.
isNotBlank
(
reqParams
)){
String
apiInterface
=
redisUtils
.
get
(
request
.
getAppKey
());
if
(
null
!=
apiInterface
){
//存在
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
apiInterface
);
String
requestParam
=
jsonObject
.
getString
(
"requestParam"
);
String
targetUrl
=
jsonObject
.
getString
(
"targetUrl"
);
Map
param
=
(
Map
)
JSONObject
.
parseObject
(
requestParam
);
String
respResult
=
httpsUtils
.
doGet
(
targetUrl
,
param
);
response
.
setAttribute
(
respResult
);
}
else
{
//不存在查询数据库
//apiInterfaceService.getReqTargetInfo(request.getAppKey());
}
}
break
;
case
"DATA_BAG_QUERY"
:
//数据包查询
break
;
default
:
response
.
setCode
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getMsg
());
break
;
}
}
catch
(
Exception
e
)
{
log
.
error
(
"~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~"
);
log
.
error
(
"异常信息:{}"
,
e
.
getMessage
());
response
.
setCode
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getMsg
());
}
}
String
selectType
=
parameter
.
getString
(
"selectType"
);
String
reqParams
=
parameter
.
getString
(
"reqParams"
);
Map
paramMap
=
null
;
if
(
StringUtils
.
isNotBlank
(
reqParams
))
{
paramMap
=
(
Map
)
JSONObject
.
parseObject
(
reqParams
);
}
String
apiInterface
=
redisUtils
.
get
(
request
.
getAppKey
());
if
(
StringUtils
.
isNotBlank
(
selectType
))
{
try
{
switch
(
selectType
)
{
case
"QUERY_TAG"
:
//标签查询
break
;
case
"BIG_DATA_QUERY"
:
//大数据查询
break
;
case
"CUSTOM_QUERY"
:
//自定义查询(三方查询)
if
(
StringUtils
.
isNotBlank
(
apiInterface
)){
//redis中存在
JSONObject
jsonCustom
=
JSONObject
.
parseObject
(
apiInterface
);
String
targetUrl
=
jsonCustom
.
getString
(
"targetUrl"
);
rangRequestTarget
(
"json"
,
targetUrl
,
null
,
response
);
}
else
{
//redis中不存在,查询数据库
ApiInterface
apiReqDto
=
apiInterfaceService
.
getReqTargetInfo
(
request
.
getAppKey
());
if
(
null
==
apiReqDto
)
{
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_INFO_UNEXIST
);
}
rangRequestTarget
(
"json"
,
apiReqDto
.
getTargetUrl
(),
null
,
response
);
}
break
;
case
"DATA_BANK_QUERY"
:
//数据银行查询
if
(
null
!=
apiInterface
)
{
//redis中存在
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
apiInterface
);
String
targetUrl
=
jsonObject
.
getString
(
"targetUrl"
);
String
outputType
=
jsonObject
.
getString
(
"outputType"
);
rangRequestTarget
(
outputType
,
targetUrl
,
paramMap
,
response
);
}
else
{
//不存在查询数据库
ApiInterface
apiReqDto
=
apiInterfaceService
.
getReqTargetInfo
(
request
.
getAppKey
());
if
(
null
==
apiReqDto
)
{
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_INFO_UNEXIST
);
}
rangRequestTarget
(
apiReqDto
.
getOutputType
(),
apiReqDto
.
getTargetUrl
(),
paramMap
,
response
);
}
break
;
case
"DATA_BAG_QUERY"
:
//数据银行数据包查询
break
;
default
:
response
.
setCode
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getMsg
());
break
;
}
}
catch
(
Exception
e
)
{
log
.
error
(
"~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~"
);
log
.
error
(
"异常信息:{}"
,
e
.
getMessage
());
response
.
setCode
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getMsg
());
}
}
}
private
void
rangRequestTarget
(
String
outputType
,
String
targetUrl
,
Map
<
String
,
String
>
param
,
OpenApiResponse
response
)
{
if
(
"flow"
.
equals
(
outputType
))
{
//文件流形式请求
// httpsUtils.doGetImg()TODO 流形式未实现
}
else
if
(
"json"
.
equals
(
outputType
))
{
String
respResult
=
httpsUtils
.
doGet
(
targetUrl
,
param
);
response
.
setAttribute
(
respResult
);
}
else
{
throw
new
GatewayException
(
GatewayResultCode
.
OUTPUT_TYPE_EXCEPTION
);
}
}
}
jz-dm-apigateway/src/main/resources/mapper/ApiInterfaceMapper.xml
View file @
02d5428f
...
...
@@ -9,4 +9,5 @@
update_user, is_deleted
</sql>
</mapper>
\ No newline at end of file
jz-dm-apigateway/src/test/java/com/jz/dm/gateway/orther/TestHttpReq.java
View file @
02d5428f
...
...
@@ -39,7 +39,7 @@ public class TestHttpReq extends SpringTestCase {
headers
.
put
(
"Charsert"
,
"UTF-8"
);
headers
.
put
(
"Accept"
,
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
);
String
response
=
httpsUtils
.
submitPost
(
baseUrlPost
,
jsonObject
,
headers
);
System
.
out
.
println
(
response
);
}
}
jz-dm-common/src/main/java/com/jz/common/utils/HttpsUtils.java
View file @
02d5428f
package
com
.
jz
.
common
.
utils
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.toolkit.CollectionUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
...
...
@@ -45,7 +44,7 @@ public class HttpsUtils {
* @param headers 请求头
* @return
*/
public
String
submitPost
(
String
url
,
JSONObject
params
,
Map
<
String
,
String
>
headers
)
{
public
String
submitPost
(
String
url
,
String
params
,
Map
<
String
,
String
>
headers
)
{
CloseableHttpClient
httpClient
=
getHttpClient
();
String
body
=
null
;
CloseableHttpResponse
response
=
null
;
...
...
@@ -57,8 +56,8 @@ public class HttpsUtils {
httpPost
.
addHeader
(
e
.
getKey
(),
e
.
getValue
());
}
}
if
(
StringUtils
.
isNotBlank
(
params
.
toString
()
))
{
httpPost
.
setEntity
(
new
StringEntity
(
params
.
toString
()
,
Consts
.
UTF_8
));
if
(
StringUtils
.
isNotBlank
(
params
))
{
httpPost
.
setEntity
(
new
StringEntity
(
params
,
Consts
.
UTF_8
));
}
response
=
httpClient
.
execute
(
httpPost
);
body
=
getBody
(
response
.
getEntity
());
...
...
Write
Preview
Markdown
is supported
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