PDA

View Full Version : Spring Data - MongoDB - Doesn't write GrantedAuthority instances



chiwi
May 21st, 2011, 01:11 PM
Hey there, I'm using SpringData MongoDB - latest snapshot, and Spring Security 3.0.5.

I have the following class I want to persist:



@Document
public class MyUser implements UserDetails {

@Id
private String username;
private String password;
private boolean accountEnabled = true;
private List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

...getters/setters...
}


I have also created a new UserDetailsManager since there is none that uses mongoDb as a db backend.



public class UserDetailsManagerMongoImpl implements UserDetailsManager {

@Autowired
private MongoTemplate template;


@Override
public void createUser(UserDetails user) {
MyUser myUser = (MyUser) user;
template.insert("users", myUser);
}

[...]
}


But even though I set the roles correctly to the user (i can see them when I debug the app), whenever I store the user, it doesn't persist the roles, it writes this:


{ "_id" : "someId", "_class" : "com.x.y.z.MyUser", "authorities" : [ { } ], "accountEnabled" : true, "password" : "mysecret" }

Note the empty authorities.

I started to debug the MappingMongoConverter, and found out that is converting the List<GrantedAuthority>, when it tries to convert each roles (GrantedAuthorityImpl), it skips the following code in MappingMongoConverter:



// Write the properties
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
public void doWithPersistentProperty(MongoPersistentProperty prop) {
Object propertyObj;
try {
propertyObj = getProperty(obj, prop, prop.getType(), useFieldAccessOnly);
} catch (IllegalAccessException e) {
throw new MappingException(e.getMessage(), e);
} catch (InvocationTargetException e) {
throw new MappingException(e.getMessage(), e);
}
if (null != propertyObj) {
if (!isSimpleType(propertyObj.getClass())) {
writePropertyInternal(prop, propertyObj, dbo);
} else {
writeSimpleInternal(prop.getKey(), propertyObj, dbo);
}
}
}
});


this is in method protected void writeInternal(final Object obj, final DBObject dbo, MongoPersistentEntity<?> entity). I don't know if it has anything to do, but seemed strange.

Do I have to add a converter for GrantedAuthorityImpl?

Thank you very much

Fede.

Oliver Gierke
May 22nd, 2011, 08:32 AM
Seems you discovered a bug :).

Ticket - https://jira.springsource.org/browse/DATADOC-145
Fix - https://github.com/SpringSource/spring-data-document/commit/22ab2007da1419b9855954935e339135c305c76d

Snapshot build should be available in our repositories by now.

chiwi
May 22nd, 2011, 11:44 AM
Hey, thank you very much!, that was a quick fix :)

I'll try the latest snapshot

thanks


Fede.

chiwi
May 24th, 2011, 06:54 PM
hey, worked great.
Thanks!

Fede.