gpt4 book ai didi

spring - 休息服务通过 Spring

转载 作者:行者123 更新时间:2023-12-04 20:13:45 24 4
gpt4 key购买 nike

我已经构建了 spring 应用程序,我有一个要求来使用一个休息服务,并且由于一些安全性,我只通过我的 spring 应用程序公开它

即对于我使用的其余服务,我提供了只有我的应用程序应该知道并通过我的 spring 应用程序公开相同服务的安全凭证。

我可以使用相应的身份验证为我使用的每个休息服务编写包装休息服务,并公开这些包装服务,这些服务可以使用我的 spring 应用程序身份验证凭据进行身份验证

但这是很多工作,我本质上做的是使用 web 服务并使用一些身份验证映射公开相同的内容。在 Spring 有没有选择通过休息服务

最佳答案

为什么不为每个 HTTP 请求类型公开一个 REST 服务,并根据路径中的信息对其进行路由?例如(未经测试,可能无法按原样工作,但您已了解基本概念):

@Autowired
RestTemplate restTemplate;

@Value("${rest.proxy.target.base.url}")
String targetBaseUrl;

@RequestMapping(value = "/restProxy/{restUrlPath}", method = RequestMethod.GET)
public @ResponseBody String restProxyGet(@PathVariable("restUrlPath") String restUrlPath) {
return restTemplate.getForObject(targetBaseUrl+ "/" + restUrlPath, String.class);
}

@RequestMapping(value = "/restProxy/{restUrlPath}", method = RequestMethod.POST)
public @ResponseBody String restProxyPost(@PathVariable("restUrlPath") String restUrlPath, @RequestBody String body) {
return restTemplate.postForObject(targetBaseUrl + "/" + restUrlPath, body, String.class);
}

//Can also add additional methods for PUT, DELETE, etc. if desired

如果您需要与不同的主机通信,您只需添加另一个路径变量,作为存储不同目标基本 URL 的映射的键。您可以从 Controller 添加任何您想要的身份验证,或通过 Spring Security 中的自定义身份验证。

您的问题在细节上有些不足,因此您的具体情况可能会使事情复杂化,但基本方法应该可行。

关于spring - 休息服务通过 Spring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com