1

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?

konstantin_doncov
  • 2,725
  • 4
  • 40
  • 100

1 Answers1

0

There are fields constant storing the query.

for example, you have to

setCreateUserSql(String createUserSql) 

before using createUser method.

http://docs.spring.io/spring-security/site/docs/4.0.2.RELEASE/apidocs/org/springframework/security/provisioning/JdbcUserDetailsManager.html

Please refer to the constant and make sure you filled in correctly.

PSo
  • 958
  • 9
  • 25
  • Thanks for the answer! But I still have some questions: If I use `serDetailsManager.setCreateUserSql("insert into employee . . .`, then I get `user lacks privilege or object not found: EMPLOYEE`. After that I remembered that I used `@Table(name = "\"Employee\"")` as Employee class annotation, so I tried to use `setCreateUserSql("insert into \"employee\" . . .`, but this also fails: `user lacks privilege or object not found: AUTHORITIES`. What I should to fix now? – konstantin_doncov May 25 '17 at 16:37
  • For user lacks privilege or object not found error, i guess the most related one is the programme cannot find the table or column that is stated, for example USERS, EMPLOYEE, AUTHORITIES. You may google it and find the anwser easily. – PSo May 26 '17 at 03:15