Announcement Announcement Module
Collapse
No announcement yet.
Spring Data - MongoDB - Doesn't write GrantedAuthority instances Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data - MongoDB - Doesn't write GrantedAuthority instances

    Hey there, I'm using SpringData MongoDB - latest snapshot, and Spring Security 3.0.5.

    I have the following class I want to persist:

    Code:
    @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.

    Code:
    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:

    Code:
    { "_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:

    Code:
    		// 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.

  • #2
    Seems you discovered a bug .

    Ticket - https://jira.springsource.org/browse/DATADOC-145
    Fix - https://github.com/SpringSource/spri...339135c305c76d

    Snapshot build should be available in our repositories by now.

    Comment


    • #3
      Hey, thank you very much!, that was a quick fix

      I'll try the latest snapshot

      thanks


      Fede.

      Comment


      • #4
        hey, worked great.
        Thanks!

        Fede.

        Comment

        Working...
        X