I have a custom authentication provider and all I did was implement org.springframework.security.userdetails.UserDetai lsService and override the loadUserByUsername method. It is declared in the config simply as
Code:
<security:authentication-provider
user-service-ref="userDetailsService" />
The loadUserByUsername must return a org.springframework.security.userdetails.UserDetai ls object and I return a custom object that extends that abstract class and provides the additional properties I need
Code:
package demo.service.impl.user;
import javax.persistence.NoResultException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import demo.domain.aggregates.user.User;
import demo.domain.applicationservice.UserApplicationService;
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserApplicationService userApplicationService;
@Autowired
private UserServiceAssembler assembler;
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
try {
User user = userApplicationService.findUserByUsername(username);
return assembler.buildAuthenticatedUserDTOFromUser(user);
} catch (NoResultException e) {
throw new UsernameNotFoundException(e.getLocalizedMessage());
}
}
}
Code:
package demo.service.api.user.dto;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.User;
public class AuthenticatedUserDto extends User {
private static final long serialVersionUID = 1L;
private String displayName;
private Integer userId;
public String getDisplayName() {
return displayName;
}
public Integer getUserId() {
return userId;
}
public AuthenticatedUserDto(String username, String password,
boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked,
GrantedAuthority[] authorities, String displayName, Integer userId)
throws IllegalArgumentException {
super(username, password, enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, authorities);
this.displayName = displayName;
this.userId = userId;
}
}