Browse Source

Implement userDatabaseRunner with example data

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.3-alpha
Pekka Helenius 4 years ago
parent
commit
5e64570c56
1 changed files with 96 additions and 1 deletions
  1. +96
    -1
      bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java

+ 96
- 1
bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java View File

@ -11,7 +11,15 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.fjordtek.bookstore.model.auth.Role;
import com.fjordtek.bookstore.model.auth.RoleRepository;
import com.fjordtek.bookstore.model.auth.User;
import com.fjordtek.bookstore.model.auth.UserRepository;
import com.fjordtek.bookstore.model.auth.UserRole;
import com.fjordtek.bookstore.model.auth.UserRoleRepository;
import com.fjordtek.bookstore.model.book.Author; import com.fjordtek.bookstore.model.book.Author;
import com.fjordtek.bookstore.model.book.AuthorRepository; import com.fjordtek.bookstore.model.book.AuthorRepository;
import com.fjordtek.bookstore.model.book.Book; import com.fjordtek.bookstore.model.book.Book;
@ -31,6 +39,9 @@ import com.fjordtek.bookstore.model.book.CategoryRepository;
*/ */
@SpringBootApplication @SpringBootApplication
/*@SpringBootApplication(
exclude = org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
)*/
public class BookstoreApplication extends SpringBootServletInitializer { public class BookstoreApplication extends SpringBootServletInitializer {
private static final Logger commonLogger = LoggerFactory.getLogger(BookstoreApplication.class); private static final Logger commonLogger = LoggerFactory.getLogger(BookstoreApplication.class);
@ -38,6 +49,90 @@ public class BookstoreApplication extends SpringBootServletInitializer {
SpringApplication.run(BookstoreApplication.class, args); SpringApplication.run(BookstoreApplication.class, args);
} }
@Bean
public CommandLineRunner userDatabaseRunner(
UserRepository userRepository,
RoleRepository roleRepository,
UserRoleRepository userRoleRepository
) {
return (args) -> {
PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
commonLogger.info("Add new roles to database");
Role adminAR = new Role("ADMIN");
Role helpdeskAR = new Role("HELPDESK");
Role userAR = new Role("USER");
Role salesAR = new Role("MARKETING");
roleRepository.save(adminAR);
roleRepository.save(helpdeskAR);
roleRepository.save(userAR);
roleRepository.save(salesAR);
commonLogger.info("Add new users to database");
User adminAU = new User(
"admin",
passwordEncoder.encode("admin"),
"admin@fjordtek.com"
);
User helpdeskAU = new User(
"helpdesk",
passwordEncoder.encode("helpdesk"),
"helpdesk@fjordtek.com"
);
User salesManagerAU = new User(
"salesmanager",
passwordEncoder.encode("salesmanager"),
"salesmanager@fjordtek.com"
);
User userAU = new User(
"user",
passwordEncoder.encode("user"),
"user@fjordtek.com"
);
userRepository.save(adminAU);
userRepository.save(helpdeskAU);
userRepository.save(userAU);
userRepository.save(salesManagerAU);
// Add example roles for admin user
userRoleRepository.save(new UserRole(adminAU, adminAR));
userRoleRepository.save(new UserRole(adminAU, salesAR));
userRoleRepository.save(new UserRole(adminAU, userAR));
// Add an example role for helpdesk user
userRoleRepository.save(new UserRole(helpdeskAU, helpdeskAR));
// Add an example role for sales manager
userRoleRepository.save(new UserRole(salesManagerAU, salesAR));
// Add an example role for user
userRoleRepository.save(new UserRole(userAU, userAR));
commonLogger.info("Sample roles in the database");
for (Role role : roleRepository.findAll()) {
commonLogger.info(role.toString());
}
commonLogger.info("Sample users in the database");
commonLogger.info("**ENCRYPTED PASSWORDS ARE PRINTED ONLY FOR DEMO PURPOSES**");
for (User user : userRepository.findAll()) {
commonLogger.info(user.toString());
}
commonLogger.info("Sample user roles in the database");
for (UserRole userRole : userRoleRepository.findAll()) {
commonLogger.info(userRole.toString());
}
};
}
@Bean @Bean
public CommandLineRunner bookDatabaseRunner( public CommandLineRunner bookDatabaseRunner(
BookRepository bookRepository, BookRepository bookRepository,


Loading…
Cancel
Save