mirror of
https://github.com/jeecgboot/jeecg-boot.git
synced 2025-09-20 19:18:38 +00:00
将请求方式修改为大写,修复openapi回调执行调用接口失败的问题
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package org.jeecg.modules.openapi.controller;
|
package org.jeecg.modules.openapi.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
@@ -11,6 +12,8 @@ import org.jeecg.common.constant.CommonConstant;
|
|||||||
import org.jeecg.common.system.base.controller.JeecgController;
|
import org.jeecg.common.system.base.controller.JeecgController;
|
||||||
import org.jeecg.common.system.query.QueryGenerator;
|
import org.jeecg.common.system.query.QueryGenerator;
|
||||||
import org.jeecg.common.system.util.JwtUtil;
|
import org.jeecg.common.system.util.JwtUtil;
|
||||||
|
import org.jeecg.common.util.RedisUtil;
|
||||||
|
import org.jeecg.common.util.RestUtil;
|
||||||
import org.jeecg.modules.openapi.entity.OpenApi;
|
import org.jeecg.modules.openapi.entity.OpenApi;
|
||||||
import org.jeecg.modules.openapi.entity.OpenApiAuth;
|
import org.jeecg.modules.openapi.entity.OpenApiAuth;
|
||||||
import org.jeecg.modules.openapi.entity.OpenApiHeader;
|
import org.jeecg.modules.openapi.entity.OpenApiHeader;
|
||||||
@@ -28,9 +31,12 @@ import org.springframework.http.HttpMethod;
|
|||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.net.URI;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @date 2024/12/10 9:11
|
* @date 2024/12/10 9:11
|
||||||
@@ -42,6 +48,8 @@ public class OpenApiController extends JeecgController<OpenApi, OpenApiService>
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
@Autowired
|
@Autowired
|
||||||
|
private RedisUtil redisUtil;
|
||||||
|
@Autowired
|
||||||
private ISysUserService sysUserService;
|
private ISysUserService sysUserService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private OpenApiAuthService openApiAuthService;
|
private OpenApiAuthService openApiAuthService;
|
||||||
@@ -140,24 +148,71 @@ public class OpenApiController extends JeecgController<OpenApi, OpenApiService>
|
|||||||
result.put("data", null);
|
result.put("data", null);
|
||||||
return Result.error("失败", result);
|
return Result.error("失败", result);
|
||||||
}
|
}
|
||||||
List<OpenApiHeader> headers = JSON.parseArray(openApi.getHeadersJson(),OpenApiHeader.class);
|
|
||||||
String url = openApi.getOriginUrl();
|
|
||||||
String method = openApi.getRequestMethod();
|
|
||||||
|
|
||||||
HttpHeaders httpHeaders = new HttpHeaders();
|
HttpHeaders httpHeaders = new HttpHeaders();
|
||||||
|
if (StrUtil.isNotEmpty(openApi.getHeadersJson())) {
|
||||||
|
List<OpenApiHeader> headers = JSON.parseArray(openApi.getHeadersJson(),OpenApiHeader.class);
|
||||||
|
if (headers.size()>0) {
|
||||||
for (OpenApiHeader header : headers) {
|
for (OpenApiHeader header : headers) {
|
||||||
httpHeaders.put(header.getHeaderKey(), Lists.newArrayList(request.getHeader(header.getHeaderKey())));
|
httpHeaders.put(header.getHeaderKey(), Lists.newArrayList(request.getHeader(header.getHeaderKey())));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String url = openApi.getOriginUrl();
|
||||||
|
String method = openApi.getRequestMethod();
|
||||||
String appkey = request.getHeader("appkey");
|
String appkey = request.getHeader("appkey");
|
||||||
OpenApiAuth openApiAuth = openApiAuthService.getByAppkey(appkey);
|
OpenApiAuth openApiAuth = openApiAuthService.getByAppkey(appkey);
|
||||||
SysUser systemUser = sysUserService.getById(openApiAuth.getSystemUserId());
|
SysUser systemUser = sysUserService.getById(openApiAuth.getSystemUserId());
|
||||||
String token = JwtUtil.sign(systemUser.getUsername(), systemUser.getPassword());
|
String token = this.getToken(systemUser.getUsername(), systemUser.getPassword());
|
||||||
httpHeaders.put("X-Access-Token", Lists.newArrayList(token));
|
httpHeaders.put("X-Access-Token", Lists.newArrayList(token));
|
||||||
|
httpHeaders.put("Content-Type",Lists.newArrayList("application/json"));
|
||||||
HttpEntity<String> httpEntity = new HttpEntity<>(json, httpHeaders);
|
HttpEntity<String> httpEntity = new HttpEntity<>(json, httpHeaders);
|
||||||
|
url = RestUtil.getBaseUrl() + url;
|
||||||
return restTemplate.exchange(url, HttpMethod.resolve(method), httpEntity, Result.class, request.getParameterMap()).getBody();
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
||||||
|
if (HttpMethod.GET.matches(method)
|
||||||
|
|| HttpMethod.DELETE.matches(method)
|
||||||
|
|| HttpMethod.OPTIONS.matches(method)
|
||||||
|
|| HttpMethod.TRACE.matches(method)) {
|
||||||
|
//拼接参数
|
||||||
|
if (!request.getParameterMap().isEmpty()) {
|
||||||
|
if (StrUtil.isNotEmpty(openApi.getParamsJson())) {
|
||||||
|
List<OpenApiParam> params = JSON.parseArray(openApi.getParamsJson(),OpenApiParam.class);
|
||||||
|
if (params.size()>0) {
|
||||||
|
Map<String, OpenApiParam> openApiParamMap = params.stream().collect(Collectors.toMap(p -> p.getParamKey(), p -> p, (e, r) -> e));
|
||||||
|
request.getParameterMap().forEach((k, v) -> {
|
||||||
|
OpenApiParam openApiParam = openApiParamMap.get(k);
|
||||||
|
if (Objects.nonNull(openApiParam)) {
|
||||||
|
if(v==null&&StrUtil.isNotEmpty(openApiParam.getDefaultValue())){
|
||||||
|
builder.queryParam(openApiParam.getParamKey(), openApiParam.getDefaultValue());
|
||||||
}
|
}
|
||||||
|
if (v!=null){
|
||||||
|
builder.queryParam(openApiParam.getParamKey(), v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
URI targetUrl = builder.build().encode().toUri();
|
||||||
|
return restTemplate.exchange(targetUrl.toString(), Objects.requireNonNull(HttpMethod.resolve(method)), httpEntity, Result.class, request.getParameterMap()).getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成接口访问令牌 Token
|
||||||
|
*
|
||||||
|
* @param USERNAME
|
||||||
|
* @param PASSWORD
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getToken(String USERNAME, String PASSWORD) {
|
||||||
|
String token = JwtUtil.sign(USERNAME, PASSWORD);
|
||||||
|
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
|
||||||
|
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, 60);
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/json")
|
@GetMapping("/json")
|
||||||
public SwaggerModel swaggerModel() {
|
public SwaggerModel swaggerModel() {
|
||||||
|
@@ -74,34 +74,34 @@ export const formSchema: FormSchema[] = [
|
|||||||
componentProps:{
|
componentProps:{
|
||||||
dictOptions: [
|
dictOptions: [
|
||||||
{
|
{
|
||||||
text: 'post',
|
text: 'POST',
|
||||||
value: 'post',
|
value: 'POST',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'get',
|
text: 'GET',
|
||||||
value: 'get',
|
value: 'GET',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'head',
|
text: 'HEAD',
|
||||||
value: 'head',
|
value: 'HEAD',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'put',
|
text: 'PUT',
|
||||||
value: 'put',
|
value: 'PUT',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'patch',
|
text: 'PATCH',
|
||||||
value: 'patch',
|
value: 'PATCH',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'delete',
|
text: 'DELETE',
|
||||||
value: 'delete',
|
value: 'DELETE',
|
||||||
},{
|
},{
|
||||||
text: 'options',
|
text: 'OPTIONS',
|
||||||
value: 'options',
|
value: 'OPTIONS',
|
||||||
},{
|
},{
|
||||||
text: 'trace',
|
text: 'TRACE',
|
||||||
value: 'trace',
|
value: 'TRACE',
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user