二十四 前后端分离应用的跨域接口调用

设计和实现运营后台的接口,以及在前后端分离项目中如何处理跨域接口访问的问题。

设计

在使用前后端分离的方式构建运营后台应用系统以后,会遇到一个非常常见的问题,就是跨域访问。那么什么是跨域访问呢?

跨域访问是指当一个网页从一个域名(或端口)请求另一个域名(或端口)的资源时,由于浏览器的同源策略限制,请求会被拒绝。跨域访问是一种常见的安全限制,用于防止网页在不受信任的域中访问敏感信息。

实现

【infrastructure层】OperationRequest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cn.ray.gateway.center.infrastructure.common;

import org.apache.commons.lang3.StringUtils;

/**
* @author Ray
* @date 2023/6/1 16:09
* @description
*/
public class OperationRequest<T> {

private int pageStart = 0; //开始 limit 第一个参数
private int pageEnd = 0; //结束 limit 第二个参数
private int pageIndex; //页数
private int pageSize; //行数
private T data;

public OperationRequest() {
}

public OperationRequest(String page, String rows) {
this.pageIndex = StringUtils.isEmpty(page) ? 1 : Integer.parseInt(page);
this.pageSize = StringUtils.isEmpty(page) ? 10 : Integer.parseInt(rows);
if (0 == this.pageIndex) {
this.pageIndex = 1;
}
this.pageStart = (this.pageIndex - 1) * this.pageSize;
this.pageEnd = this.pageSize;
}

public OperationRequest(int page, int rows) {
this.pageIndex = 0 == page ? 1 : page;
this.pageSize = 0 == rows ? 10 : rows;
this.pageStart = (this.pageIndex - 1) * this.pageSize;
this.pageEnd = this.pageSize;
}

public void setPage(String page, String rows) {
this.pageIndex = StringUtils.isEmpty(page) ? 1 : Integer.parseInt(page);
this.pageSize = StringUtils.isEmpty(page) ? 10 : Integer.parseInt(rows);
if (0 == this.pageIndex) {
this.pageIndex = 1;
}
this.pageStart = (this.pageIndex - 1) * this.pageSize;
this.pageEnd = this.pageSize;
}

public int getPageStart() {
return pageStart;
}

public void setPageStart(int pageStart) {
this.pageStart = pageStart;
}

public int getPageEnd() {
return pageEnd;
}

public void setPageEnd(int pageEnd) {
this.pageEnd = pageEnd;
}

public T getData() {
return data;
}

public void setData(T data) {
if (data instanceof String) {
String str = (String) data;
if (StringUtils.isEmpty(str)){
data = null;
}
}
this.data = data;
}
}

【infrastructure层】OperationResult

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cn.ray.gateway.center.infrastructure.common;

import java.util.List;

/**
* @author Ray
* @date 2023/6/1 16:10
* @description
*/
public class OperationResult<T> {

private int pageTotal;
private List<T> list;

public OperationResult() {
}

public OperationResult(int pageTotal, List<T> list) {
this.pageTotal = pageTotal;
this.list = list;
}

public int getPageTotal() {
return pageTotal;
}

public void setPageTotal(int pageTotal) {
this.pageTotal = pageTotal;
}

public List<T> getList() {
return list;
}

public void setList(List<T> list) {
this.list = list;
}

}

【infrastructure层】DataOperationManageRepository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package cn.ray.gateway.center.infrastructure.repository;

import cn.ray.gateway.center.domain.operation.model.vo.*;
import cn.ray.gateway.center.domain.operation.repository.IDataOperationManageRepository;
import cn.ray.gateway.center.infrastructure.common.OperationRequest;
import cn.ray.gateway.center.infrastructure.dao.*;
import cn.ray.gateway.center.infrastructure.pojo.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author Ray
* @date 2023/6/1 16:15
* @description 运营数据查询仓储服务
*/
@Repository
public class DataOperationManageRepository implements IDataOperationManageRepository {

@Resource
private IGatewayServerDao gatewayServerDao;
@Resource
private IGatewayServerDetailDao gatewayServerDetailDao;
@Resource
private IGatewayDistributionDao gatewayDistributionDao;
@Resource
private IApplicationSystemDao applicationSystemDao;
@Resource
private IApplicationInterfaceDao applicationInterfaceDao;
@Resource
private IApplicationInterfaceMethodDao applicationInterfaceMethodDao;

@Override
public List<GatewayServerDataVO> queryGatewayServerListByPage(OperationRequest<String> request) {
List<GatewayServer> gatewayServers = gatewayServerDao.queryGatewayServerListByPage(request);
List<GatewayServerDataVO> gatewayServerVOList = new ArrayList<>(gatewayServers.size());
for (GatewayServer gatewayServer : gatewayServers) {
// 可以按照 IDEA 插件 vo2dto 方便转换
GatewayServerDataVO gatewayServerVO = new GatewayServerDataVO();
gatewayServerVO.setId(gatewayServer.getId());
gatewayServerVO.setGroupId(gatewayServer.getGroupId());
gatewayServerVO.setGroupName(gatewayServer.getGroupName());
gatewayServerVOList.add(gatewayServerVO);
}
return gatewayServerVOList;
}

@Override
public int queryGatewayServerListCountByPage(OperationRequest<String> request) {
return gatewayServerDao.queryGatewayServerListCountByPage(request);
}

@Override
public List<ApplicationSystemDataVO> queryApplicationSystemListByPage(OperationRequest<ApplicationSystemDataVO> request) {
List<ApplicationSystem> applicationSystems = applicationSystemDao.queryApplicationSystemListByPage(request);
List<ApplicationSystemDataVO> applicationSystemDataVOList = new ArrayList<>(applicationSystems.size());
for (ApplicationSystem applicationSystem : applicationSystems) {
ApplicationSystemDataVO applicationSystemDataVO = new ApplicationSystemDataVO();
applicationSystemDataVO.setSystemId(applicationSystem.getSystemId());
applicationSystemDataVO.setSystemName(applicationSystem.getSystemName());
applicationSystemDataVO.setSystemType(applicationSystem.getSystemType());
applicationSystemDataVO.setSystemRegistry(applicationSystem.getSystemRegistry());
applicationSystemDataVOList.add(applicationSystemDataVO);
}
return applicationSystemDataVOList;
}

@Override
public int queryApplicationSystemListCountByPage(OperationRequest<ApplicationSystemDataVO> request) {
return applicationSystemDao.queryApplicationSystemListCountByPage(request);
}

@Override
public List<ApplicationInterfaceDataVO> queryApplicationInterfaceListByPage(OperationRequest<ApplicationInterfaceDataVO> request) {
List<ApplicationInterface> applicationInterfaces = applicationInterfaceDao.queryApplicationInterfaceListByPage(request);
List<ApplicationInterfaceDataVO> applicationInterfaceDataVOList = new ArrayList<>(applicationInterfaces.size());
for (ApplicationInterface applicationInterface : applicationInterfaces) {
ApplicationInterfaceDataVO applicationInterfaceDataVO = new ApplicationInterfaceDataVO();
applicationInterfaceDataVO.setSystemId(applicationInterface.getSystemId());
applicationInterfaceDataVO.setInterfaceId(applicationInterface.getInterfaceId());
applicationInterfaceDataVO.setInterfaceName(applicationInterface.getInterfaceName());
applicationInterfaceDataVO.setInterfaceVersion(applicationInterface.getInterfaceVersion());
applicationInterfaceDataVOList.add(applicationInterfaceDataVO);
}
return applicationInterfaceDataVOList;
}

@Override
public int queryApplicationInterfaceListCountByPage(OperationRequest<ApplicationInterfaceDataVO> request) {
return applicationInterfaceDao.queryApplicationInterfaceListCountByPage(request);
}

@Override
public List<ApplicationInterfaceMethodDataVO> queryApplicationInterfaceMethodListByPage(OperationRequest<ApplicationInterfaceMethodDataVO> request) {
List<ApplicationInterfaceMethod> applicationInterfaceMethods = applicationInterfaceMethodDao.queryApplicationInterfaceMethodListByPage(request);
List<ApplicationInterfaceMethodDataVO> applicationInterfaceMethodDataVOList = new ArrayList<>(applicationInterfaceMethods.size());
for (ApplicationInterfaceMethod applicationInterfaceMethod : applicationInterfaceMethods) {
ApplicationInterfaceMethodDataVO applicationInterfaceMethodDataVO = new ApplicationInterfaceMethodDataVO();
applicationInterfaceMethodDataVO.setSystemId(applicationInterfaceMethod.getSystemId());
applicationInterfaceMethodDataVO.setInterfaceId(applicationInterfaceMethod.getInterfaceId());
applicationInterfaceMethodDataVO.setMethodId(applicationInterfaceMethod.getMethodId());
applicationInterfaceMethodDataVO.setMethodName(applicationInterfaceMethod.getMethodName());
applicationInterfaceMethodDataVO.setParameterType(applicationInterfaceMethod.getParameterType());
applicationInterfaceMethodDataVO.setUri(applicationInterfaceMethod.getUri());
applicationInterfaceMethodDataVO.setHttpCommandType(applicationInterfaceMethod.getHttpCommandType());
applicationInterfaceMethodDataVO.setAuth(applicationInterfaceMethod.getAuth());
applicationInterfaceMethodDataVOList.add(applicationInterfaceMethodDataVO);
}
return applicationInterfaceMethodDataVOList;
}

@Override
public int queryApplicationInterfaceMethodListCountByPage(OperationRequest<ApplicationInterfaceMethodDataVO> request) {
return applicationInterfaceMethodDao.queryApplicationInterfaceMethodListCountByPage(request);
}

@Override
public List<GatewayServerDetailDataVO> queryGatewayServerDetailListByPage(OperationRequest<GatewayServerDetailDataVO> request) {
List<GatewayServerDetail> applicationInterfaceMethods = gatewayServerDetailDao.queryGatewayServerDetailListByPage(request);
List<GatewayServerDetailDataVO> gatewayServerDetailDataVOList = new ArrayList<>(applicationInterfaceMethods.size());
for (GatewayServerDetail gatewayServerDetail : applicationInterfaceMethods) {
GatewayServerDetailDataVO gatewayServerDetailDataVO = new GatewayServerDetailDataVO();
gatewayServerDetailDataVO.setId(gatewayServerDetail.getId());
gatewayServerDetailDataVO.setGroupId(gatewayServerDetail.getGroupId());
gatewayServerDetailDataVO.setGatewayId(gatewayServerDetail.getGatewayId());
gatewayServerDetailDataVO.setGatewayName(gatewayServerDetail.getGatewayName());
gatewayServerDetailDataVO.setGatewayAddress(gatewayServerDetail.getGatewayAddress());
gatewayServerDetailDataVO.setStatus(gatewayServerDetail.getStatus());
gatewayServerDetailDataVO.setCreateTime(gatewayServerDetail.getCreateTime());
gatewayServerDetailDataVO.setUpdateTime(gatewayServerDetail.getUpdateTime());
gatewayServerDetailDataVOList.add(gatewayServerDetailDataVO);
}
return gatewayServerDetailDataVOList;
}

@Override
public int queryGatewayServerDetailListCountByPage(OperationRequest<GatewayServerDetailDataVO> request) {
return gatewayServerDetailDao.queryGatewayServerDetailListCountByPage(request);
}

@Override
public List<GatewayDistributionDataVO> queryGatewayDistributionListByPage(OperationRequest<GatewayDistributionDataVO> request) {
List<GatewayDistribution> gatewayDistributions = gatewayDistributionDao.queryGatewayDistributionListByPage(request);
List<GatewayDistributionDataVO> gatewayServerDetailDataVOList = new ArrayList<>(gatewayDistributions.size());
for (GatewayDistribution gatewayDistribution : gatewayDistributions) {
GatewayDistributionDataVO gatewayDistributionDataVO = new GatewayDistributionDataVO();
gatewayDistributionDataVO.setId(gatewayDistribution.getId());
gatewayDistributionDataVO.setGroupId(gatewayDistribution.getGroupId());
gatewayDistributionDataVO.setGatewayId(gatewayDistribution.getGatewayId());
gatewayDistributionDataVO.setSystemId(gatewayDistribution.getSystemId());
gatewayDistributionDataVO.setSystemName(gatewayDistribution.getSystemName());
gatewayDistributionDataVO.setCreateTime(gatewayDistribution.getCreateTime());
gatewayDistributionDataVO.setUpdateTime(gatewayDistribution.getUpdateTime());
gatewayServerDetailDataVOList.add(gatewayDistributionDataVO);
}
return gatewayServerDetailDataVOList;
}

@Override
public int queryGatewayDistributionListCountByPage(OperationRequest<GatewayDistributionDataVO> request) {
return gatewayDistributionDao.queryGatewayDistributionListCountByPage(request);
}
}

【domain层】DataOperationManageService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.ray.gateway.center.domain.operation.service;

import cn.ray.gateway.center.application.IDataOperationManageService;
import cn.ray.gateway.center.domain.operation.model.vo.*;
import cn.ray.gateway.center.domain.operation.repository.IDataOperationManageRepository;
import cn.ray.gateway.center.infrastructure.common.OperationRequest;
import cn.ray.gateway.center.infrastructure.common.OperationResult;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Ray
* @date 2023/6/1 16:11
* @description 网关运营数据管理服务
*/
@Service
public class DataOperationManageService implements IDataOperationManageService {

@Resource
private IDataOperationManageRepository repository;

@Override
public OperationResult<GatewayServerDataVO> queryGatewayServer(OperationRequest<String> request) {
List<GatewayServerDataVO> list = repository.queryGatewayServerListByPage(request);
int count = repository.queryGatewayServerListCountByPage(request);
return new OperationResult<>(count, list);
}

@Override
public OperationResult<ApplicationSystemDataVO> queryApplicationSystem(OperationRequest<ApplicationSystemDataVO> request) {
List<ApplicationSystemDataVO> list = repository.queryApplicationSystemListByPage(request);
int count = repository.queryApplicationSystemListCountByPage(request);
return new OperationResult<>(count, list);
}

@Override
public OperationResult<ApplicationInterfaceDataVO> queryApplicationInterface(OperationRequest<ApplicationInterfaceDataVO> request) {
List<ApplicationInterfaceDataVO> list = repository.queryApplicationInterfaceListByPage(request);
int count = repository.queryApplicationInterfaceListCountByPage(request);
return new OperationResult<>(count, list);
}

@Override
public OperationResult<ApplicationInterfaceMethodDataVO> queryApplicationInterfaceMethod(OperationRequest<ApplicationInterfaceMethodDataVO> request) {
List<ApplicationInterfaceMethodDataVO> list = repository.queryApplicationInterfaceMethodListByPage(request);
int count = repository.queryApplicationInterfaceMethodListCountByPage(request);
return new OperationResult<>(count, list);
}

@Override
public OperationResult<GatewayServerDetailDataVO> queryGatewayServerDetail(OperationRequest<GatewayServerDetailDataVO> request) {
List<GatewayServerDetailDataVO> list = repository.queryGatewayServerDetailListByPage(request);
int count = repository.queryGatewayServerDetailListCountByPage(request);
return new OperationResult<>(count, list);
}

@Override
public OperationResult<GatewayDistributionDataVO> queryGatewayDistribution(OperationRequest<GatewayDistributionDataVO> request) {
List<GatewayDistributionDataVO> list = repository.queryGatewayDistributionListByPage(request);
int count = repository.queryGatewayDistributionListCountByPage(request);
return new OperationResult<>(count, list);
}
}

【interfaces层】DataOperationManage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cn.ray.gateway.center.interfaces;

import cn.ray.gateway.center.application.IDataOperationManageService;
import cn.ray.gateway.center.domain.operation.model.vo.*;
import cn.ray.gateway.center.infrastructure.common.OperationRequest;
import cn.ray.gateway.center.infrastructure.common.OperationResult;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @author Ray
* @date 2023/6/1 15:55
* @description 网关数据操作处理
*/
@CrossOrigin
@RestController
@RequestMapping("/wg/admin/data")
public class DataOperationManage {

private Logger logger = LoggerFactory.getLogger(DataOperationManage.class);

@Resource
private IDataOperationManageService dataOperationManageService;

@GetMapping(value = "queryGatewayServer", produces = "application/json;charset=utf-8")
public OperationResult<GatewayServerDataVO> queryGatewayServer(@RequestParam String groupId,
@RequestParam String page,
@RequestParam String limit) {
try {
logger.info("查询网关服务数据开始 groupId:{} page:{} limit:{}", groupId, page, limit);
OperationRequest<String> req = new OperationRequest<>(page, limit);
req.setData(groupId);
OperationResult<GatewayServerDataVO> operationResult = dataOperationManageService.queryGatewayServer(req);
logger.info("查询网关服务数据完成 operationResult:{}", JSON.toJSONString(operationResult));
return operationResult;
} catch (Exception e) {
logger.error("查询网关服务数据异常 groupId:{}", groupId, e);
return new OperationResult<>(0, null);
}
}

@GetMapping(value = "queryGatewayServerDetail", produces = "application/json;charset=utf-8")
public OperationResult<GatewayServerDetailDataVO> queryGatewayServerDetail(@RequestParam String groupId,
@RequestParam String gatewayId,
@RequestParam String page,
@RequestParam String limit) {
try {
logger.info("查询网关服务详情数据开始 groupId:{} gatewayId:{} page:{} limit:{}", groupId, gatewayId, page, limit);
OperationRequest<GatewayServerDetailDataVO> req = new OperationRequest<>(page, limit);
req.setData(new GatewayServerDetailDataVO(groupId, gatewayId));
OperationResult<GatewayServerDetailDataVO> operationResult = dataOperationManageService.queryGatewayServerDetail(req);
logger.info("查询网关服务详情数据完成 operationResult:{}", JSON.toJSONString(operationResult));
return operationResult;
} catch (Exception e) {
logger.error("查询网关服务详情数据异常 groupId:{}", groupId, e);
return new OperationResult<>(0, null);
}
}

@GetMapping(value = "queryGatewayDistribution", produces = "application/json;charset=utf-8")
public OperationResult<GatewayDistributionDataVO> queryGatewayDistribution(@RequestParam String groupId,
@RequestParam String gatewayId,
@RequestParam String page,
@RequestParam String limit) {
try {
logger.info("查询网关分配数据开始 groupId:{} gatewayId:{} page:{} limit:{}", groupId, gatewayId, page, limit);
OperationRequest<GatewayDistributionDataVO> req = new OperationRequest<>(page, limit);
req.setData(new GatewayDistributionDataVO(groupId, gatewayId));
OperationResult<GatewayDistributionDataVO> operationResult = dataOperationManageService.queryGatewayDistribution(req);
logger.info("查询网关分配数据完成 operationResult:{}", JSON.toJSONString(operationResult));
return operationResult;
} catch (Exception e) {
logger.error("查询网关分配数据异常 groupId:{}", groupId, e);
return new OperationResult<>(0, null);
}
}

@GetMapping(value = "queryApplicationSystem", produces = "application/json;charset=utf-8")
public OperationResult<ApplicationSystemDataVO> queryApplicationSystem(@RequestParam String systemId,
@RequestParam String systemName,
@RequestParam String page,
@RequestParam String limit) {
try {
logger.info("查询应用系统信息开始 systemId:{} systemName:{} page:{} limit:{}", systemId, systemName, page, limit);
OperationRequest<ApplicationSystemDataVO> req = new OperationRequest<>(page, limit);
req.setData(new ApplicationSystemDataVO(systemId, systemName));
OperationResult<ApplicationSystemDataVO> operationResult = dataOperationManageService.queryApplicationSystem(req);
logger.info("查询应用系统信息完成 operationResult:{}", JSON.toJSONString(operationResult));
return operationResult;
} catch (Exception e) {
logger.error("查询应用系统信息异常 systemId:{} systemName:{}", systemId, systemId, e);
return new OperationResult<>(0, null);
}
}

@GetMapping(value = "queryApplicationInterface", produces = "application/json;charset=utf-8")
public OperationResult<ApplicationInterfaceDataVO> queryApplicationInterface(@RequestParam String systemId,
@RequestParam String interfaceId,
@RequestParam String page,
@RequestParam String limit) {
try {
logger.info("查询应用接口信息开始 systemId:{} interfaceId:{} page:{} limit:{}", systemId, interfaceId, page, limit);
OperationRequest<ApplicationInterfaceDataVO> req = new OperationRequest<>(page, limit);
req.setData(new ApplicationInterfaceDataVO(systemId, interfaceId));
OperationResult<ApplicationInterfaceDataVO> operationResult = dataOperationManageService.queryApplicationInterface(req);
logger.info("查询应用接口信息完成 operationResult:{}", JSON.toJSONString(operationResult));
return operationResult;
} catch (Exception e) {
logger.error("查询应用接口信息异常 systemId:{} interfaceId:{}", systemId, interfaceId, e);
return new OperationResult<>(0, null);
}
}

@GetMapping(value = "queryApplicationInterfaceMethodList", produces = "application/json;charset=utf-8")
public OperationResult<ApplicationInterfaceMethodDataVO> queryApplicationInterfaceMethodList(@RequestParam String systemId,
@RequestParam String interfaceId,
@RequestParam String page,
@RequestParam String limit) {
try {
logger.info("查询应用接口方法信息开始 systemId:{} interfaceId:{} page:{} limit:{}", systemId, interfaceId, page, limit);
OperationRequest<ApplicationInterfaceMethodDataVO> req = new OperationRequest<>(page, limit);
req.setData(new ApplicationInterfaceMethodDataVO(systemId, interfaceId));
OperationResult<ApplicationInterfaceMethodDataVO> operationResult = dataOperationManageService.queryApplicationInterfaceMethod(req);
logger.info("查询应用接口方法信息完成 operationResult:{}", JSON.toJSONString(operationResult));
return operationResult;
} catch (Exception e) {
logger.error("查询应用接口方法信息异常 systemId:{} interfaceId:{}", systemId, interfaceId, e);
return new OperationResult<>(0, null);
}
}

}

测试

前端页面

网关分组

24-测试-1

算力节点

24-测试-2

网关映射(分配)

24-测试-3

应用服务

24-测试-4

应用接口

24-测试-5

接口方法

24-测试-6

后台日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
2023-06-01 19:32:41.849  INFO 1693 --- [p-nio-80-exec-1] c.r.g.c.interfaces.DataOperationManage   : 查询网关服务数据开始 groupId: page:1 limit:10
2023-06-01 19:32:41.887 INFO 1693 --- [p-nio-80-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-06-01 19:32:42.464 INFO 1693 --- [p-nio-80-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-06-01 19:32:42.604 INFO 1693 --- [p-nio-80-exec-1] c.r.g.c.interfaces.DataOperationManage : 查询网关服务数据完成 operationResult:{"list":[{"groupId":"10001","groupName":"缺省的","id":1}],"pageTotal":1}
2023-06-01 19:33:10.541 INFO 1693 --- [p-nio-80-exec-2] c.r.g.c.interfaces.DataOperationManage : 查询网关服务详情数据开始 groupId: gatewayId: page:1 limit:10
2023-06-01 19:33:10.676 INFO 1693 --- [p-nio-80-exec-2] c.r.g.c.interfaces.DataOperationManage : 查询网关服务详情数据完成 operationResult:{"list":[{"createTime":1685504198000,"gatewayAddress":"10.17.69.148:7397","gatewayId":"api-gateway-g4","gatewayName":"电商配送网关","groupId":"10001","id":21,"status":1,"updateTime":1685504198000},{"createTime":1685413348000,"gatewayAddress":"10.17.167.105:7397","gatewayId":"api-gateway-g4","gatewayName":"电商配送网关","groupId":"10001","id":20,"status":1,"updateTime":1685413348000},{"createTime":1670678507000,"gatewayAddress":"/0:0:0:0:0:0:0:0:7399","gatewayId":"api-gateway-g4","gatewayName":"电商配送网关","groupId":"10001","id":19,"status":1,"updateTime":1670678507000},{"createTime":1670674984000,"gatewayAddress":"192.168.1.105:8099","gatewayId":"api-gateway-g4","gatewayName":"电商配送网关","groupId":"10001","id":18,"status":1,"updateTime":1670674984000},{"createTime":1670671031000,"gatewayAddress":"192.168.1.105:7397","gatewayId":"api-gateway-g4","gatewayName":"电商配送网关","groupId":"10001","id":17,"status":1,"updateTime":1670671031000},{"createTime":1670081779000,"gatewayAddress":"127.0.0.1:7397","gatewayId":"api-gateway-g4","gatewayName":"电商配送网关","groupId":"10001","id":16,"status":1,"updateTime":1670081779000},{"createTime":1667748199000,"gatewayAddress":"127.0.0.198","gatewayId":"api-gateway-g3","gatewayName":"电商配送网关","groupId":"10001","id":15,"status":1,"updateTime":1667748199000},{"createTime":1667748131000,"gatewayAddress":"127.0.0.197","gatewayId":"api-gateway-g2","gatewayName":"电商支付网关","groupId":"10001","id":14,"status":1,"updateTime":1667748131000},{"createTime":1667748131000,"gatewayAddress":"127.0.0.196","gatewayId":"api-gateway-g1","gatewayName":"电商支付网关","groupId":"10001","id":13,"status":1,"updateTime":1667748131000}],"pageTotal":9}
2023-06-01 19:33:36.837 INFO 1693 --- [p-nio-80-exec-3] c.r.g.c.interfaces.DataOperationManage : 查询网关分配数据开始 groupId: gatewayId: page:1 limit:10
2023-06-01 19:33:36.962 INFO 1693 --- [p-nio-80-exec-3] c.r.g.c.interfaces.DataOperationManage : 查询网关分配数据完成 operationResult:{"list":[{"createTime":1669468380000,"gatewayId":"api-gateway-g4","groupId":"10001","id":1,"systemId":"api-gateway-test","systemName":"测试工程","updateTime":1669468380000}],"pageTotal":1}
2023-06-01 19:33:55.371 INFO 1693 --- [p-nio-80-exec-4] c.r.g.c.interfaces.DataOperationManage : 查询应用系统信息开始 systemId: systemName: page:1 limit:10
2023-06-01 19:33:55.494 INFO 1693 --- [p-nio-80-exec-4] c.r.g.c.interfaces.DataOperationManage : 查询应用系统信息完成 operationResult:{"list":[{"systemId":"api-gateway-test","systemName":"网关SDK测试工程","systemRegistry":"zookeeper://10.17.69.148:2181","systemType":"RPC"}],"pageTotal":1}
2023-06-01 19:34:04.695 INFO 1693 --- [p-nio-80-exec-5] c.r.g.c.interfaces.DataOperationManage : 查询应用接口信息开始 systemId: interfaceId: page:1 limit:10
2023-06-01 19:34:04.786 INFO 1693 --- [p-nio-80-exec-5] c.r.g.c.interfaces.DataOperationManage : 查询应用接口信息完成 operationResult:{"list":[{"interfaceId":"cn.ray.gateway.rpc.IActivityBooth","interfaceName":"活动服务","interfaceVersion":"1.0.0","systemId":"api-gateway-test"}],"pageTotal":1}
2023-06-01 19:34:12.000 INFO 1693 --- [p-nio-80-exec-6] c.r.g.c.interfaces.DataOperationManage : 查询应用接口方法信息开始 systemId: interfaceId: page:1 limit:10
2023-06-01 19:34:12.096 INFO 1693 --- [p-nio-80-exec-6] c.r.g.c.interfaces.DataOperationManage : 查询应用接口方法信息完成 operationResult:{"list":[{"auth":0,"httpCommandType":"POST","interfaceId":"cn.ray.gateway.rpc.IActivityBooth","methodId":"test","methodName":"测试方法","parameterType":"java.lang.String,cn.ray.gateway.rpc.dto.XReq","systemId":"api-gateway-test","uri":"/wg/activity/test"},{"auth":1,"httpCommandType":"POST","interfaceId":"cn.ray.gateway.rpc.IActivityBooth","methodId":"insert","methodName":"插入方法","parameterType":"cn.ray.gateway.rpc.dto.XReq","systemId":"api-gateway-test","uri":"/wg/activity/insert"},{"auth":0,"httpCommandType":"GET","interfaceId":"cn.ray.gateway.rpc.IActivityBooth","methodId":"sayHi","methodName":"探活方法","parameterType":"java.lang.String","systemId":"api-gateway-test","uri":"/wg/activity/sayHi"}],"pageTotal":3}

思考

跨域的几种解决方案

Java 可以通过以下几种方式解决跨域问题:

  • 使用 CORS(Cross-Origin Resource Sharing 跨域资源共享):CORS 是一种机制,它使用额外的 HTTP 头来告诉浏览器,哪些源被允许访问该资源。在 Java 中,可以通过配置 Filter 或 Interceptor 来实现 CORS。

    在 Java 中,可以通过配置 Filter 或 Interceptor 来实现 CORS。下面是一个使用 Filter 的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@WebFilter(filterName = "corsFilter", urlPatterns = "/*")
public class CorsFilter implements Filter {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpServletRequest httpRequest = (HttpServletRequest) request;

// 允许的域名
String allowedOrigin = "http://example.com";

// 允许的请求方法
String allowedMethods = "GET, POST, PUT, DELETE, OPTIONS";

// 允许的请求头字段
String allowedHeaders = "Content-Type, Authorization";

// 允许的响应头字段
String exposedHeaders = "Authorization";

// 允许发送凭据(如 cookie)
boolean allowCredentials = true;

// 缓存时间(单位:秒)
int maxAge = 3600;

httpResponse.setHeader("Access-Control-Allow-Origin", allowedOrigin);
httpResponse.setHeader("Access-Control-Allow-Methods", allowedMethods);
httpResponse.setHeader("Access-Control-Allow-Headers", allowedHeaders);
httpResponse.setHeader("Access-Control-Expose-Headers", exposedHeaders);
httpResponse.setHeader("Access-Control-Allow-Credentials", String.valueOf(allowCredentials));
httpResponse.setHeader("Access-Control-Max-Age", String.valueOf(maxAge));

if ("OPTIONS".equals(httpRequest.getMethod())) {
httpResponse.setStatus(HttpServletResponse.SC_OK);
return;
}

chain.doFilter(request, response);
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void destroy() {
}
}

在上面的示例中,我们定义了一个名为 CorsFilter 的 Filter,并使用 @WebFilter 注解标记它。在 doFilter 方法中,我们设置了允许的域名、请求方法、请求头字段、响应头字段、发送凭据和缓存时间,并将这些信息设置到响应头中。如果请求方法为 OPTIONS,则直接返回,否则调用 chain.doFilter 方法继续处理请求。

@WebFilter 是 Java Servlet 3.0 规范中的一种注解,用于定义过滤器。过滤器是一种可以在请求到达 Servlet 之前或响应离开 Servlet 之后对请求和响应进行预处理的组件。@WebFilter 注解可以用于指定需要拦截的 URL 模式、拦截器执行的顺序、拦截器的名称等信息。

通过使用 @WebFilter 注解,可以方便地创建一个过滤器,并将其应用到指定的 URL 上,以实现对请求和响应的处理和过滤。例如,可以使用 @WebFilter 注解来实现跨域请求的处理,将需要的响应头信息添加到响应中,以允许跨域请求。

需要注意的是,以上示例中的配置仅适用于单个域名,如果需要允许多个域名访问,则需要使用逗号分隔它们,并将其设置到 Access-Control-Allow-Origin 头中。另外,如果需要动态获取允许的域名,也可以在代码中进行配置。

  • 使用 JSONP(JSON with Padding):JSONP 是一种跨域请求的解决方案,它利用了浏览器允许跨域请求加载 JavaScript 文件的特性。在 Java 中,可以通过返回 JSONP 格式的数据来实现跨域请求。

    JSONP(JSON with Padding)是一种跨域请求的解决方案,它的原理是通过动态创建 <script> 标签,以 GET 方式向跨域的服务器发送请求,服务器返回一段 JavaScript 代码,该代码会被自动执行,从而实现跨域请求。

    具体实现步骤如下:

    1. 在客户端页面中定义一个回调函数,该函数用于接收服务器返回的数据。
    2. 使用动态创建 <script> 标签的方式,向跨域的服务器发送 GET 请求,请求的 URL 中包含回调函数的名称和其他参数。
    3. 服务器接收到请求后,将需要返回的数据包装成一个 JavaScript 函数调用的形式,该函数调用的名称就是客户端定义的回调函数名称,将包装好的数据返回给客户端。
    4. 客户端接收到服务器返回的数据后,自动执行回调函数,从而实现跨域请求。

    需要注意的是,JSONP 只支持 GET 请求,并且返回的数据必须是 JSON 格式,否则会出现语法错误。此外,由于 JSONP 是通过动态创建 <script> 标签实现的,因此存在一定的安全风险,容易受到 XSS 攻击。因此,在使用 JSONP 时需要注意安全问题。

  • 使用代理服务器:在 Java 中,可以通过配置代理服务器来实现跨域请求。代理服务器是一个位于客户端和服务器之间的中间层,它可以将客户端的请求转发给服务器,并将服务器的响应返回给客户端,从而实现跨域请求。
  • 使用 WebSocket:WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,它可以实现跨域通信。在 Java 中,可以使用 Spring WebSocket 框架来实现 WebSocket。

  • @CrossOrigin 是 Spring 框架提供的一个注解,用于处理跨域请求。它的原理是通过在响应头中添加 Access-Control-Allow-Origin、Access-Control-Allow-Methods、Access-Control-Allow-Headers 等字段,告诉浏览器该请求允许跨域访问。

    当客户端发起跨域请求时,浏览器会自动发起 OPTIONS 请求,向服务器查询该请求是否允许跨域访问,该请求称为预检请求。服务器接收到预检请求后,会在响应头中添加 Access-Control-Allow-Origin、Access-Control-Allow-Methods、Access-Control-Allow-Headers 等字段,告诉浏览器该请求允许跨域访问。如果服务器返回的响应头中没有包含这些字段,浏览器会阻止该请求,从而防止跨域攻击。

    使用 @CrossOrigin 注解可以简化配置跨域请求的过程,只需要在控制器方法上添加该注解,并指定需要允许跨域访问的域名、请求方法、请求头字段等信息即可。Spring 框架会自动处理跨域请求,并在响应头中添加相应的字段,从而允许跨域访问。

  • 使用服务器端框架或库,例如 Spring 或 Apache Shiro,提供跨域访问支持。