/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.am.gateway.handler.oauth2.authentication;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
public class OAuth2LoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {

    public OAuth2LoginUrlAuthenticationEntryPoint(String loginFormUrl) {
        super(loginFormUrl);
    }

    @Override
    protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
        String url = super.determineUrlToUseForThisRequest(request, response, exception);
        // Does not work because ant pattern matcher does not allow to pass query parameters
    //    return UriComponentsBuilder.fromPath(url).queryParam("client_id", request.getParameter("client_id")).toUriString();
        return url;
    }
}

@@ -687,16 +687,60 @@
 ation;%0A%0A
+import io.gravitee.common.http.HttpHeaders;%0A
 import o
@@ -797,16 +797,16 @@
 eption;%0A
-
 import o
@@ -885,16 +885,74 @@
 ryPoint;
+%0Aimport org.springframework.web.util.UriComponentsBuilder;
 %0A%0Aimport
@@ -1701,13 +1701,14 @@
     
-//
+ 
    
+//
  ret
@@ -1845,11 +1845,721 @@
 l;%0A    %7D
+%0A%0A    @Override%0A    protected String buildRedirectUrlToLoginPage(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) %7B%0A        String url = super.buildRedirectUrlToLoginPage(request, response, authException);%0A%0A        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);%0A%0A        String scheme = request.getHeader(HttpHeaders.X_FORWARDED_PROTO);%0A        if (scheme != null && !scheme.isEmpty()) %7B%0A            builder.scheme(scheme);%0A        %7D%0A%0A        String host = request.getHeader(HttpHeaders.X_FORWARDED_HOST);%0A        if (host != null && !host.isEmpty()) %7B%0A            builder.host(host);%0A        %7D%0A%0A        return builder.toUriString();%0A    %7D
 %0A%7D%0A

Use x-forwarded-for-host and proto if defined in the incoming HTTP request