royclarkson / spring-rest-service-oauth

A simple OAuth protected REST service built with Spring Boot and Spring Security OAuth
Apache License 2.0
664 stars 339 forks source link

Adding new users #35

Closed 28formica77 closed 8 years ago

28formica77 commented 9 years ago

Hi - I have this working nicely but want to be able to add new users. I can't work out how to get the entries into the table that maps users to roles. If I try to save the object without the role mapping I get a constraint violation but the id is only set on save so how can I do this? Any advice greatly appreciated.

Thanks

Anthony

royclarkson commented 9 years ago

In this sample app the users are being loaded from /src/main/resources/import.sql. How are you trying to add users?

28formica77 commented 9 years ago

I wanted to add users from a client registration page so I created an appropriate method in the controller. I have this working with custom SQL inserts, first to the user_info table then to the user_role table. This works fine but I am bypassing most of the hibernate code with custom SQL. I never found a way of saving a user object [userRepository.save(newUser)] with a valid hashset to include the roles. My solution feels unsatisfactory in that I am not using the Spring features fully.

soulmachine commented 9 years ago

I think create a rest API named /register in a controller and add .antMatchers("/oauth/**", "/register").permitAll() to ResourceServerConfiguration.configure() will do the trick.