Basically you have more than one problem with your code. First of all, the project you are referencing is created based on JPA (this is a relational database), and if you want to use the same code base with Mongo DB (document storage), at least you need to rethink the data model (User and Power). But first, let's try to make this example work, and in the end I would just advise that this could also change.
First of all, if you use MongoDb for the best experience, the domain fields of the id domain must be of type String, since you know that mongodb stores identifiers as ObjectId bson type, which cannot be converted to Long. So first change User.java to
@Document(collection = "user") public class User { @Id private String _id;
Same thing with the Permissions object
@Document(collection = "authority") public class Authority { @Id private String _id;
And UserRepository
public interface UserRepository extends MongoRepository<User, String> { User findByUsername(String username); }
After these two changes, you will also need to reorganize the links.
Do you have a link from authorities to users? !!! What is this? :) This is something from JPA, with MongoDb this link does not make sense. Just delete it!
The next thing related to the project is that it relies on the fact that the data store has some predefined data, see import.sql , so for MongoDb you need to import the data into mongodb in order to be able to run the project. I converted import.sql to MongoShell format so you can use it.
Paste this into the authority collection:
{ "_id" : ObjectId("587cb19d1db2732183afb0a9"), "_class" : "org.parkcity.model.security.Authority", "name" : "ROLE_ADMIN" } { "_id" : ObjectId("587cb19d1db2732183afb0aa"), "_class" : "org.parkcity.model.security.Authority", "name" : "ROLE_USER" }
Paste this into the user collection:
{ "_id" : ObjectId("587cb19d1db2732183afb0ab"), "_class" : "org.parkcity.model.security.User", "username" : "admin", "password" : "$2a$08$lDnHPz7eUkSi6ao14Twuau08mzhWrL4kyZGGU5xfiGALO/Vxd5DOi", "firstname" : "admin", "lastname" : "admin", "email" : " admin@admin.com ", "enabled" : true, "lastPasswordResetDate" : ISODate("2017-01-16T11:42:21.537+0000"), "authorities" : [ DBRef("authority", ObjectId("587cb19d1db2732183afb0a9")), DBRef("authority", ObjectId("587cb19d1db2732183afb0aa")) ] } { "_id" : ObjectId("587cb1a11db2732183afb0ac"), "_class" : "org.parkcity.model.security.User", "username" : "user", "password" : "$2a$08$UkVvwpULis18S19S5pZFn.YHPZt3oaqHZnDwqbCW9pft6uFtkXKDC", "firstname" : "user", "lastname" : "user", "email" : " user@user.com ", "enabled" : true, "lastPasswordResetDate" : ISODate("2017-01-16T11:42:21.620+0000"), "authorities" : [ DBRef("authority", ObjectId("587cb19d1db2732183afb0aa")) ] }
As you can see, I adjusted the packages from "org.zerhusen" to "org.parkcity".
Here it is, now you can start the project and conduct some tests. As you can see, I did not add a disconnected user (so you can do it yourself).
And just for your reasons, for this particular case using @DBRefs is a pure evil :) You are on MogoDb, so keep credentials in the same collection and object as the users, so it will be much faster.
A fully functional fork (based on MongoDB) has also been created, which is available here https://github.com/babltiga/jwt-spring-security-demo p>