Read Console input on spring boot with tomcat

后端 未结 4 1261
轮回少年
轮回少年 2021-02-20 01:12

Is it possible to read console input just before the embedded tomcat on the spring boot starts? The supposedly application flow is, request username and password from the user,

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-20 01:25

    You can use nohup to start the jar with the parameters, they just won't be prompted for them on new lines in the terminal. The User could add them as parameters when starting the jar. See details below.

    Example:

    Main Class

    public static void main(String[] args) {
            String username = args[0];
            String password = args[1];
            if(username.equals("admin") && password.equals("password")) {
                SpringApplication.run(NohupApplication.class, args);
            } else {
                System.out.println("You are not authorized to start this application.");
            }
        }
    

    With Invalid Credentials

    Command

    nohup java -jar example.jar user password
    

    nohup.out

    You are not authorized to start this application.
    

    With Valid Credentials

    Command

    nohup java -jar example.jar admin password
    

    nohup.out

      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.2.RELEASE)
    

提交回复
热议问题