I'm trying to add a new user using Spring Security programmatically using this answer. But unfortunately I get issue. Code:
SecurityConfig:
private JdbcUserDetailsManager employeeDetailsManager;
@Bean(name = "employeeDetailsManager")
public JdbcUserDetailsManager getEmployeeDetailsManager() {
return employeeDetailsManager;
}
Application:
private static void createUser(ApplicationContext context){
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("supervisor"));
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
UserDetails user = new User("supervisor1", passwordEncoder.encode("supervisor1"), authorities);
JdbcUserDetailsManager userDetailsManager = (JdbcUserDetailsManager) context.getBean("employeeDetailsManager");
userDetailsManager.createUser(user);//Exception!
Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, authorities);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
On the line userDetailsManager.createUser(user)
I get exception:
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: USERS
And I understand why I get it: I really don't have a table Users
. Instead of this I have table Employees
, so I need to save new user in this table.
So how can I fix this error and create and save user to the table Employees
?