Nav*_*ger 5 rest model-view-controller spring
我已经构建了spring应用程序,我有一个要求使用休息服务,并且由于某些安全性而通过我的spring应用程序公开它
即对于我消耗的其他服务,我提供的安全凭证只有我的应用程序应该知道并通过我的spring应用程序公开相同的服务.
我可以为我使用相应身份验证的每个休息服务编写包装器休息服务,并公开这些可以使用我的spring应用程序Auth凭证进行身份验证的包装器服务
但是我做的很多工作就是消耗webservice并使用一些auth映射公开它.在春天,可以选择通过其他服务
为什么不为每个 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
Run Code Online (Sandbox Code Playgroud)
如果您需要与不同的主机通信,您只需添加另一个路径变量,该变量充当存储不同目标基本 URL 的映射的键。您可以从控制器添加任何您想要的身份验证,或通过 Spring Security 中的自定义身份验证。
您的问题在细节上有些不足,因此您的具体情况可能会使事情复杂化,但基本方法应该可行。
| 归档时间: |
|
| 查看次数: |
3713 次 |
| 最近记录: |