Getting Actual address in URL instead of Proxy address Url

Viewed 1248

I have done a reverse proxy to web application as example.test.com and actual address is *.*.*.56 and it's working fine when I hit example.test.com but when I added Office 365 authentication, I debug through my Java using httpRequest.getRequestURL(); command I want proxy address but I am getting the actual address as ***.***.0.56

I tried to change the URL using Java but no luck

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

I need any solution to set up in proxy server to get the proxy address in my current URL.

and i am getting error saying in azure AAD saying but in reverse proxy it is there as example.test.com

{"error_description":"AADSTS70002: Error validating credentials. AADSTS50011: The reply address 'https://*...56:8080/abc/' does not match the reply address 'https://example.test.com/' provided when requesting Authorization code.\r\nTrace ID: gddsgc97-5667-6574g-9897h-97536vg688\r\nCorrelation ID: 565gtdf-j7573-087f-9745a-792835t647\r\nTimestamp: 2019-01-14 10:09:54Z","error":"invalid_client"}

1 Answers

You can get the original host name in the request header X-Forwarded-Host.

UPDATE: with a filter

@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() {
    }
}
Related