【问题标题】:Getting Actual address in URL instead of Proxy address Url在 URL 中获取实际地址而不是代理地址 Url
【发布时间】:2019-06-08 04:22:15
【问题描述】:

我以example.test.com 对Web 应用程序进行了反向代理,实际地址为*.*.*.56,当我点击example.test.com 时它工作正常,但是当我添加Office 365 身份验证时,我使用httpRequest.getRequestURL(); 通过我的Java 进行调试命令我想要代理地址,但我得到的实际地址是***.***.0.56

我尝试使用 Java 更改 URL,但没有成功

String currentUri = httpRequest.getRequestURL().toString();

我需要在代理服务器中设置任何解决方案来获取当前 URL 中的代理地址。

我在 azure AAD 中说错误,但在反向代理中它是 example.test.com

{"error_description":"AADSTS70002: 验证凭据时出错。AADSTS50011: 回复地址 'https://*...56:8080/abc/' 与回复地址不匹配' https://example.test.com/' 在请求授权码时提供。\r\n跟踪 ID:gddsgc97-5667-6574g-9897h-97536vg688\r\n相关 ID:565gtdf-j7573-087f-9745a-792835t647\r\ n时间戳:2019-01-14 10:09:54Z","error":"invalid_client"}

【问题讨论】:

    标签: java apache azure-active-directory reverse-proxy mod-proxy


    【解决方案1】:

    可以在请求头X-Forwarded-Host中获取原始主机名。

    更新:使用过滤器

    @WebFilter(filterName = "RequestURLFilter", urlPatterns = {"/*"})
    public class RequestURLFilter implements Filter {
        private FilterConfig filterConfig = null;
    
        public RequestURLFilter() {
        }    
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain)
                throws IOException, ServletException {
            ServletRequest wrappedRequest
                    = new HttpServletRequestWrapper((HttpServletRequest) request) {
                @Override
                public String getServerName() {
                    String forwarded = getHeader("X-Forwarded-Host");
                    if (forwarded != null) {
                        return forwarded;
                    } else {
                        return super.getServerName();
                    }
                }
    
                @Override
                public String getRequestURI() {
                    String info = getPathInfo();
                    if (info == null) {
                        info = "";
                    }
                    return getScheme() + "://" + getServerName() + getContextPath()
                            + getServletPath() + info;
                }
    
                @Override
                public StringBuffer getRequestURL() {
                    return new StringBuffer(getRequestURI());
                }
            };
            chain.doFilter(wrappedRequest, response);
        }
    
        public FilterConfig getFilterConfig() {
            return (this.filterConfig);
        }
    
        public void setFilterConfig(FilterConfig filterConfig) {
            this.filterConfig = filterConfig;
        }
    
        @Override
        public void init(FilterConfig filterConfig) {        
            this.filterConfig = filterConfig;
        }
    
        @Override
        public void destroy() {
        }
    }
    

    【讨论】:

    • 我会从那里得到代理地址,但我希望它进入当前的 url
    • @shridhar by "current url",你的意思是httpRequest.getRequestURL()吗?
    • @Maurice Perry 是的
    • 我刚刚编辑了我的问题,希望它能让您更清楚我的问题。
    • @shridhar 恐怕你需要一个过滤器来做到这一点(见我上面的更新)。您可以将其添加到您的 web 应用程序中,也可以将其安装在您的 servlet 容器中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多