简体   繁体   English

Spring MVC代理服务器-维护URL上下文(contextpath)

[英]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.

相关问题 Spring Boot-忽略属性server.contextPath - Spring Boot - ignore property server.contextPath Spring pageContext.request.contextPath 与 Apache 后面的 Tomcat 作为反向代理 - Spring pageContext.request.contextPath with Tomcat behind Apache as reverse proxy Spring 3 Web MVC错误-在此服务器上找不到请求的URL / app-context / page-name - Spring 3 Web MVC error - requested URL /app-context/page-name was not found on this server Spring 丢失 contextPath - Spring losting contextPath 如何在Spring MVC中代理/镜像Websocket连接URL? - How to proxy/mirror a Websocket connection url in Spring MVC? 带有嵌入式 Tomcat 的 Spring Boot 应用程序在请求 /contextPath 时不重定向到 https 而不使用尾部斜杠 - Spring Boot application with embedded Tomcat behind reverse proxy not redirecting to https when asking for /contextPath without trailing slash 如果tomcat服务器在反向代理后面运行,如何确定请求方案,contextpath - How to determine request scheme, contextpath if tomcat server is running behind reverse proxy 在Spring MVC中,此服务器上找不到请求的URL / save - The requested URL /save was not found on this server in spring MVC 在Spring MVC应用程序中运行Tomcat服务器时,上下文初始化失败 - Context initialization failed when running Tomcat server in a Spring MVC application 在Spring MVC中获取上下文 - Getting context in Spring MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM