[英]spring mvc Proxy server - maintaining url context (contextpath)
I have the below piece spring REST controller class. 我有下面的spring REST控制器类。
@RestController
@RequestMapping("/global")
public class ProxyController extends BaseController{
@RequestMapping(value = "/**")
public ResponseEntity<String> proxy(HttpServletRequest request, HttpServletResponse response ) throws Exception {
try {
String restOfTheUrl = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
URL uri = new URL("https://myrealserver" +
restOfTheUrl);
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
return resp;
} catch (Exception e) {
logger.error("Error ", e);
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
What I am trying to achieve here is to hide a server behind a proxy, which blindly forwards requests to the server. 我在这里想要实现的是将服务器隐藏在代理之后,该代理将请求盲目转发到服务器。
This piece of code is invoked with url 这段代码是使用url调用的
https://myproxyserver/myapp1/end/point1
which in turn returns an html page with few clickable links. 依次返回一个带有很少可点击链接的html页面。 Now when the user clicks I am expecting the link to be invoked as
现在,当用户单击时,我希望链接被调用为
https://myproxyserver/myapp1/end/point2
Where as actually the endpoint invoked is 实际调用的端点在哪里
https://myproxyserver/end/point2
In the html page returned by the actual server, the path is end/point2 and has no mention of myapp1. 在实际服务器返回的html页面中,路径为end / point2,没有提及myapp1。 So on click on those links my context changes to https://myproxyserver/end/point2 instead of https://myproxyserver/myapp1/end/point2
因此,在单击这些链接时,我的上下文更改为https:// myproxyserver / end / point2而不是https:// myproxyserver / myapp1 / end / point2
How do I ensure that the root context is always https://myproxyserver/myapp1 and not https://myproxyserver ? 如何确保根上下文始终为https:// myproxyserver / myapp1而不是https:// myproxyserver ?
You want to get your server context path. 您想要获取服务器上下文路径。 this is sample code.
这是示例代码。
like this : 像这样 :
public static String getServerNameAndContextPath(HttpServletRequest req) {
return "https://" + req.getServerName() + req.getContextPath();
}
Finally I resolved the problem by taking what DD suggested. 最后,我根据DD的建议解决了这个问题。 I scanned through the whole response body, fortunately I had a pattern that I could use to scan and appended the context of the url to where ever required.
我浏览了整个响应正文,幸运的是,我有一个模式可以用来扫描并将URL的上下文附加到需要的地方。 That resolved the problem for me this problem.
那为我解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.