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

Prefil h2-console login data by request parameter #3979

Open
Fachher opened this issue Jan 24, 2024 · 0 comments
Open

Prefil h2-console login data by request parameter #3979

Fachher opened this issue Jan 24, 2024 · 0 comments

Comments

@Fachher
Copy link

Fachher commented Jan 24, 2024

Hi,

I have written a Spring ApplicationListener which logs the application URL after the application has been started. So that I can click on it and my browser opens the URL. Now I want to enhance this listener. It should also print the URL to the h2-console where the user enters a username, password, and jdbc url. Then I thought about a more convenient way. Providing this information via request parameter should be possible so that the input fields are prefilled. The user only needs to hit the connect button.

What do you think about this idea? I can create a pull request.

@Profile({ "default", "local" })
@AllArgsConstructor
@Slf4j
@Component
@ExcludeFromGeneratedCoverage // configuration
public class ApplicationInfoProvider implements ApplicationListener<ApplicationStartedEvent> {

    @Override
    public void onApplicationEvent(final ApplicationStartedEvent event) {
        final ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
        final String contextPath = environment.getProperty("server.servlet.context-path", "").replace("/", "");
        final String serverPort = environment.getProperty("server.port", "8080");
        final String h2consolePath = environment.getProperty("spring.h2.console.path", "h2-console").replace("/", "");
        final String jdbcUrl = environment.getProperty("spring.datasource.url");
        final String dataSourceUsername = environment.getProperty("spring.datasource.username");
        final String dataSourcePassword = environment.getProperty("spring.datasource.password");

        final UriComponentsBuilder serverUri = UriComponentsBuilder.fromUriString("http://127.0.0.1").port(serverPort).pathSegment(contextPath);
        final UriComponentsBuilder h2Uri = serverUri.cloneBuilder().pathSegment(h2consolePath).queryParam("user_name", dataSourceUsername).queryParam("password", dataSourcePassword).queryParam("jdbc_url", jdbcUrl);
        log.info("Server url: {}", serverUri.toUriString());
        log.info("H2 url: {}", h2Uri.toUriString());
    }

}
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

No branches or pull requests

1 participant