Create InitialSetup.java

This commit is contained in:
Anthony Stirling 2023-08-13 01:14:14 +01:00 committed by GitHub
parent e791fee38b
commit d75e84bdff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,27 @@
package stirling.software.SPDF.config.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import jakarta.annotation.PostConstruct;
import stirling.software.SPDF.model.Role;
@Component
public class InitialSetup {
@Autowired
private UserService userService;
@PostConstruct
public void init() {
if(!userService.hasUsers()) {
String initialUsername = System.getenv("INITIAL_USERNAME");
String initialPassword = System.getenv("INITIAL_PASSWORD");
if(initialUsername != null && initialPassword != null) {
userService.saveUser(initialUsername, initialPassword, Role.ADMIN);
} else {
userService.saveUser("admin", "password", Role.ADMIN);
}
}
}
}