Reputation: 289
I am confused here, and I don't know whether I am asking a foolish question. The scenario is this, I have created an application in Development Environment and I want to deploy the application in Production therefore I emptied all the tables in the Database including the User. I don't have a registration page where I can add users. The Registration is done by the Administrator. As I Truncated the tables of users I don't have an administrator too. But at first run I need to create a Super Admin who can add users and configure the application.
Is there any other logic for this Issue? Please correct me if I am doing it in a wrong way. I use Spring MVC and Hibernate for the development.
Upvotes: 1
Views: 656
Reputation: 1059
Get access to the module where you have your application bootstrap, then register manually a super-user(admin) on the DB table.
Remember that you'll have to set a control if it already exists or you'll fill the table very fast ;) If you want, you could simply call a function that verify if the table is empty: if it is, just ask username and pw (if you register in this way an user), and just register the first user as super-user.
Upvotes: 2
Reputation: 102
Yes you can do this by implementing WebApplication initialiser.
Add the following class to your project
public class SpringDispatcherConfig implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(ApplicationConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(ApplicationConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("SpringDispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}
}
And then you need ApplicationEventListnerClass and annotated method gets triggered on application start where you can implement functionality to create user
@Component
public class ApplicationStartListener {
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
String id = event.getApplicationContext().getId();
if (id.startsWith("org.springframework.web.context.WebApplicationContext:") && !id.endsWith("SpringDispatcher")) {
//implement your on startup functionality here
}
}
}
Upvotes: 1