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
Show 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 {
...
@@ -66,8 +66,12 @@ public enum GatewayResultCode implements ResultCode {
API_STATUS_EXCEPTION
(
"API_STATUS_EXCEPTION"
,
"API状态异常"
),
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获取
* 初始化保存到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;
...
@@ -16,7 +16,7 @@ import java.util.Map;
*/
*/
public
class
OpenApiRequest
{
public
class
OpenApiRequest
{
private
String
ap
p
Key
;
//apiKey
private
String
ap
i
Key
;
//apiKey
private
String
openApiParams
;
private
String
openApiParams
;
...
@@ -41,11 +41,11 @@ public class OpenApiRequest {
...
@@ -41,11 +41,11 @@ public class OpenApiRequest {
}
}
public
String
getAppKey
()
{
public
String
getAppKey
()
{
return
ap
p
Key
;
return
ap
i
Key
;
}
}
public
void
setAppKey
(
String
appKey
)
{
public
void
setAppKey
(
String
appKey
)
{
this
.
ap
p
Key
=
appKey
;
this
.
ap
i
Key
=
appKey
;
}
}
@SuppressWarnings
(
"unchecked"
)
@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 {
...
@@ -75,7 +75,7 @@ public class AuthFilter extends AbstractFilter {
JSONObject
parameter
=
JSONObject
.
parseObject
(
request
.
getParams
());
JSONObject
parameter
=
JSONObject
.
parseObject
(
request
.
getParams
());
if
(
null
!=
parameter
)
{
if
(
null
!=
parameter
)
{
String
authCode
=
parameter
.
getString
(
"authCode"
);
String
authCode
=
parameter
.
getString
(
"authCode"
);
ApiAuth
authAuth
=
authService
.
getAuthUser
(
authCode
);
ApiAuth
authAuth
=
authService
.
getAuthUser
(
authCode
,
apiInterface
.
getId
()
);
if
(
null
==
authAuth
)
{
if
(
null
==
authAuth
)
{
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_NOT_AUTH
);
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;
...
@@ -13,4 +13,6 @@ import com.jz.dm.models.domian.ApiInterface;
* @Description:
* @Description:
**/
**/
public
interface
ApiInterfaceMapper
extends
BaseMapper
<
ApiInterface
>
{
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;
...
@@ -18,9 +18,9 @@ import java.util.Map;
@Data
@Data
public
class
GatewayRequest
implements
Serializable
{
public
class
GatewayRequest
implements
Serializable
{
@ParamName
(
"ap
p
_key"
)
@ParamName
(
"ap
i
_key"
)
@ApiModelProperty
(
value
=
"appKey API唯一标识"
)
@ApiModelProperty
(
value
=
"appKey API唯一标识"
)
private
String
ap
p
Key
;
private
String
ap
i
Key
;
@ApiModelProperty
(
value
=
"接口名称"
)
@ApiModelProperty
(
value
=
"接口名称"
)
private
String
method
;
private
String
method
;
...
@@ -88,7 +88,7 @@ public class GatewayRequest implements Serializable {
...
@@ -88,7 +88,7 @@ public class GatewayRequest implements Serializable {
*/
*/
@Override
@Override
public
String
toString
()
{
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
+
", signType="
+
signType
+
", sign="
+
sign
+
", timestamp="
+
timestamp
+
", version="
+
version
+
", timestamp="
+
timestamp
+
", version="
+
version
+
", params="
+
params
+
", extAttributes="
+
", 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 {
...
@@ -24,8 +24,15 @@ public interface ApiInterfaceService {
/**
/**
* 获取api信息
* 获取api信息
* @param appKey
* @param apiKey
* @return
*/
ApiInterface
getApiInfo
(
String
apiKey
);
/**
* 获取apikey信息
* @param apiKey
* @return
* @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 {
...
@@ -59,7 +59,8 @@ public interface AuthService {
/**
/**
* 获取API授权信息
* 获取API授权信息
* @param authCode
* @param authCode
* @param apiId
* @return
* @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 {
...
@@ -54,14 +54,22 @@ public class ApiInterfaceServiceImpl implements ApiInterfaceService {
/**
/**
* 获取API信息
* 获取API信息
* @param ap
p
Key
* @param ap
i
Key
* @return
* @return
*/
*/
@Override
@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
<>();
QueryWrapper
<
ApiInterface
>
query
=
new
QueryWrapper
<>();
query
.
eq
(
"api_key"
,
apiKey
);
query
.
eq
(
"is_deleted"
,
0
);
query
.
eq
(
"is_deleted"
,
0
);
query
.
eq
(
"api_key"
,
appKey
);
return
apiInterfaceMapper
.
selectOne
(
query
);
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 {
...
@@ -267,12 +267,14 @@ public class AuthServiceImpl implements AuthService {
/**
/**
* 获取API授权信息
* 获取API授权信息
* @param authCode
* @param authCode
* @param apiId
* @return
* @return
*/
*/
@Override
@Override
public
ApiAuth
getAuthUser
(
String
authCode
)
{
public
ApiAuth
getAuthUser
(
String
authCode
,
Long
apiId
)
{
QueryWrapper
<
ApiAuth
>
query
=
new
QueryWrapper
<>();
QueryWrapper
<
ApiAuth
>
query
=
new
QueryWrapper
<>();
query
.
eq
(
"auth_code"
,
authCode
);
query
.
eq
(
"auth_code"
,
authCode
);
query
.
eq
(
"api_interface_id"
,
apiId
);
query
.
eq
(
"is_deleted"
,
0
);
query
.
eq
(
"is_deleted"
,
0
);
return
apiAuthMapper
.
selectOne
(
query
);
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;
...
@@ -4,9 +4,11 @@ import com.alibaba.fastjson.JSONObject;
import
com.jz.common.utils.HttpsUtils
;
import
com.jz.common.utils.HttpsUtils
;
import
com.jz.common.utils.RedisUtils
;
import
com.jz.common.utils.RedisUtils
;
import
com.jz.dm.common.enums.GatewayResultCode
;
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.OpenApiRequest
;
import
com.jz.dm.common.util.OpenApiResponse
;
import
com.jz.dm.common.util.OpenApiResponse
;
import
com.jz.dm.gateway.OpenApiService
;
import
com.jz.dm.gateway.OpenApiService
;
import
com.jz.dm.models.domian.ApiInterface
;
import
com.jz.dm.service.ApiInterfaceService
;
import
com.jz.dm.service.ApiInterfaceService
;
import
com.jz.dm.web.annotation.AccessLimit
;
import
com.jz.dm.web.annotation.AccessLimit
;
import
com.jz.dm.web.annotation.ApiLogAspect
;
import
com.jz.dm.web.annotation.ApiLogAspect
;
...
@@ -47,53 +49,80 @@ public class ApiQueryService implements OpenApiService {
...
@@ -47,53 +49,80 @@ public class ApiQueryService implements OpenApiService {
private
RedisUtils
redisUtils
;
private
RedisUtils
redisUtils
;
@Autowired
@Autowired
private
HttpsUtils
httpsUtils
;
private
HttpsUtils
httpsUtils
;
@Override
@Override
@ApiLogAspect
(
description
=
"API请求日志"
)
@ApiLogAspect
(
description
=
"API请求日志"
)
@AccessLimit
(
limit
=
10000
,
sec
=
1
)
@AccessLimit
(
limit
=
10000
,
sec
=
1
)
@Transactional
(
rollbackFor
=
Exception
.
class
,
propagation
=
Propagation
.
REQUIRES_NEW
)
@Transactional
(
rollbackFor
=
Exception
.
class
,
propagation
=
Propagation
.
REQUIRES_NEW
)
public
void
doService
(
OpenApiRequest
request
,
OpenApiResponse
response
)
{
public
void
doService
(
OpenApiRequest
request
,
OpenApiResponse
response
)
{
JSONObject
parameter
=
JSONObject
.
parseObject
(
request
.
getOpenApiParams
());
JSONObject
parameter
=
JSONObject
.
parseObject
(
request
.
getOpenApiParams
());
String
apiType
=
parameter
.
getString
(
"apiType"
);
String
selectType
=
parameter
.
getString
(
"selectType"
);
if
(
StringUtils
.
isNotBlank
(
apiType
)){
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
{
try
{
switch
(
apiType
)
{
switch
(
selectType
)
{
case
"QUERY_TAG"
:
//标签查询
case
"QUERY_TAG"
:
//标签查询
//String reqParam = parameter.getString("request_param");
//apiInterfaceService.getTagReqData();
break
;
break
;
case
"BIG_DATA_QUERY"
:
//大数据查询
case
"BIG_DATA_QUERY"
:
//大数据查询
break
;
break
;
case
"CUSTOM_QUERY"
:
//自定义查询(三方查询)
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
;
break
;
case
"DATA_BANK_QUERY"
:
//数据银行查询
case
"DATA_BANK_QUERY"
:
//数据银行查询
String
reqParams
=
parameter
.
getString
(
"reqParams"
);
if
(
null
!=
apiInterface
)
{
//redis中存在
if
(
StringUtils
.
isNotBlank
(
reqParams
)){
String
apiInterface
=
redisUtils
.
get
(
request
.
getAppKey
());
if
(
null
!=
apiInterface
){
//存在
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
apiInterface
);
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
apiInterface
);
String
requestParam
=
jsonObject
.
getString
(
"requestParam"
);
String
targetUrl
=
jsonObject
.
getString
(
"targetUrl"
);
String
targetUrl
=
jsonObject
.
getString
(
"targetUrl"
);
String
outputType
=
jsonObject
.
getString
(
"outputType"
);
Map
param
=
(
Map
)
JSONObject
.
parseObject
(
requestParam
);
rangRequestTarget
(
outputType
,
targetUrl
,
paramMap
,
response
);
String
respResult
=
httpsUtils
.
doGet
(
targetUrl
,
param
);
}
else
{
//不存在查询数据库
response
.
setAttribute
(
respResult
);
ApiInterface
apiReqDto
=
}
else
{
//不存在查询数据库
apiInterfaceService
.
getReqTargetInfo
(
request
.
getAppKey
());
//apiInterfaceService.getReqTargetInfo(request.getAppKey());
if
(
null
==
apiReqDto
)
{
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_INFO_UNEXIST
);
}
}
rangRequestTarget
(
apiReqDto
.
getOutputType
(),
apiReqDto
.
getTargetUrl
(),
paramMap
,
response
);
}
}
break
;
break
;
case
"DATA_BAG_QUERY"
:
//
数据包查询
case
"DATA_BAG_QUERY"
:
//数据银行
数据包查询
break
;
break
;
default
:
default
:
response
.
setCode
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getCode
());
response
.
setCode
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getMsg
());
response
.
setMsg
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getMsg
());
break
;
break
;
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
"~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~"
);
log
.
error
(
"~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~"
);
log
.
error
(
"异常信息:{}"
,
e
.
getMessage
());
log
.
error
(
"异常信息:{}"
,
e
.
getMessage
());
response
.
setCode
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getCode
());
response
.
setCode
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
ILLEGAL_REQUEST
.
getMsg
());
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 @@
...
@@ -9,4 +9,5 @@
update_user, is_deleted
update_user, is_deleted
</sql>
</sql>
</mapper>
</mapper>
\ No newline at end of file
jz-dm-apigateway/src/test/java/com/jz/dm/gateway/orther/TestHttpReq.java
View file @
02d5428f
jz-dm-common/src/main/java/com/jz/common/utils/HttpsUtils.java
View file @
02d5428f
package
com
.
jz
.
common
.
utils
;
package
com
.
jz
.
common
.
utils
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.toolkit.CollectionUtils
;
import
com.baomidou.mybatisplus.core.toolkit.CollectionUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
...
@@ -45,7 +44,7 @@ public class HttpsUtils {
...
@@ -45,7 +44,7 @@ public class HttpsUtils {
* @param headers 请求头
* @param headers 请求头
* @return
* @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
();
CloseableHttpClient
httpClient
=
getHttpClient
();
String
body
=
null
;
String
body
=
null
;
CloseableHttpResponse
response
=
null
;
CloseableHttpResponse
response
=
null
;
...
@@ -57,8 +56,8 @@ public class HttpsUtils {
...
@@ -57,8 +56,8 @@ public class HttpsUtils {
httpPost
.
addHeader
(
e
.
getKey
(),
e
.
getValue
());
httpPost
.
addHeader
(
e
.
getKey
(),
e
.
getValue
());
}
}
}
}
if
(
StringUtils
.
isNotBlank
(
params
.
toString
()
))
{
if
(
StringUtils
.
isNotBlank
(
params
))
{
httpPost
.
setEntity
(
new
StringEntity
(
params
.
toString
()
,
Consts
.
UTF_8
));
httpPost
.
setEntity
(
new
StringEntity
(
params
,
Consts
.
UTF_8
));
}
}
response
=
httpClient
.
execute
(
httpPost
);
response
=
httpClient
.
execute
(
httpPost
);
body
=
getBody
(
response
.
getEntity
());
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