Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic way to customize serverBaseUrl based on request headers. #2571

Open
mshima opened this issue Apr 12, 2024 · 0 comments · May be fixed by #2582
Open

Add a generic way to customize serverBaseUrl based on request headers. #2571

mshima opened this issue Apr 12, 2024 · 0 comments · May be fixed by #2582

Comments

@mshima
Copy link
Contributor

mshima commented Apr 12, 2024

Is your feature request related to a problem? Please describe.

We use gateway and routes serving /services/*.
/services/* are stripped from url before passing to microservices.

We don't use *-ui packages.

We want to rebuild the serverBaseUrl based on the gateway populated X-Forwarded-Prefix.

Using mvc:

    @RequestScope
    @Bean
    public ServerBaseUrlCustomizer microserviceBaseUrlCustomizer(HttpServletRequest request) {
        return serverBaseUrl -> {
            String forwardedPrefix = request.getHeader("X-Forwarded-Prefix");
            return forwardedPrefix == null
                ? serverBaseUrl
                : UriComponentsBuilder.fromUriString(serverBaseUrl).path(forwardedPrefix).build().toString();
        };
    }

Using webflux without groups:

@RestController
public class JHipsterOpenApiWebfluxResource extends OpenApiWebfluxResource {

    @Autowired
    public JHipsterOpenApiWebfluxResource(
        ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory,
        AbstractRequestService requestBuilder,
        GenericResponseService responseBuilder,
        OperationService operationParser,
        SpringDocConfigProperties springDocConfigProperties,
        SpringDocProviders springDocProviders,
        SpringDocCustomizers springDocCustomizers
    ) {
        super(
            openAPIBuilderObjectFactory,
            requestBuilder,
            responseBuilder,
            operationParser,
            springDocConfigProperties,
            springDocProviders,
            springDocCustomizers
        );
    }

    @Override
    protected String getServerUrl(ServerHttpRequest serverHttpRequest, String apiDocsUrl) {
        String serverBaseUrl = super.getServerUrl(serverHttpRequest, apiDocsUrl);
        List<String> forwardedPrefix = serverHttpRequest.getHeaders().get("X-Forwarded-Prefix");
        if (forwardedPrefix.size() > 0) {
            return UriComponentsBuilder.fromUriString(serverBaseUrl).path(forwardedPrefix.get(0)).build().toString();
        }
        return serverBaseUrl;
    }
}

But there is no way to customize using webflux and multiple groups.

Describe the solution you'd like

Add ServerBaseUrlCustomizer alternatives with request for webflux:

@FunctionalInterface
public interface ServerBaseUrlRequestCustomizer {
	String customize(ServerHttpRequest serverBaseUrl);
}

And servlet:

@FunctionalInterface
public interface ServerBaseUrlRequestCustomizer {
	String customize(HttpServletRequest serverBaseUrl);
}

Describe alternatives you've considered

Additional context

@mshima mshima changed the title Add a generic way to customize serverBaseUrl based on request. Add a generic way to customize serverBaseUrl based on request headers. Apr 22, 2024
@mshima mshima linked a pull request Apr 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant