Login username with case insensitive not working

Hi,

When i am trying to login with KM6 i m able to login successfully but when i try login with km6 in lowercase it shows bad credentials.
I am able to get results from database for KM675 and km675. I tried debugging, error message bad credentials comes when in code there is a condition to check access of ui_login and it fails due to case sensitive. Any solution for this?
Thank you in advance

You should override loadUserByUsername() method of your DatabaseUserRepository class as follows:

@Primary
@Component("UserRepository")
public class DatabaseUserRepository extends AbstractDatabaseUserRepository<User> {

    private final UnconstrainedDataManager dataManager;
    private final RoleAssignmentRepository roleAssignmentRepository;
    private final ResourceRoleRepository resourceRoleRepository;
    private final RowLevelRoleRepository rowLevelRoleRepository;

    public DatabaseUserRepository(UnconstrainedDataManager dataManager,
                                  RoleAssignmentRepository roleAssignmentRepository,
                                  ResourceRoleRepository resourceRoleRepository,
                                  RowLevelRoleRepository rowLevelRoleRepository) {
        this.dataManager = dataManager;
        this.roleAssignmentRepository = roleAssignmentRepository;
        this.resourceRoleRepository = resourceRoleRepository;
        this.rowLevelRoleRepository = rowLevelRoleRepository;
    }

    @Override
    public User loadUserByUsername(String username) throws UsernameNotFoundException {
        List<User> users = dataManager.load(User.class)
                .query("lower(e.username) = ?1", username.toLowerCase())
                .list();
        if (!users.isEmpty()) {
            User user = users.get(0);
            if (user != null) {
                ((AcceptsGrantedAuthorities) user).setAuthorities(createAuthorities(user.getUsername()));
            }
            return user;
        } else {
            throw new UsernameNotFoundException("User not found");
        }
    }

    private Collection<? extends GrantedAuthority> createAuthorities(String username) {
        return roleAssignmentRepository.getAssignmentsByUsername(username).stream()
                .map(this::createAuthority)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    private GrantedAuthority createAuthority(RoleAssignment roleAssignment) {
        GrantedAuthority authority = null;
        if (RoleAssignmentRoleType.RESOURCE.equals(roleAssignment.getRoleType())) {
            ResourceRole role = resourceRoleRepository.findRoleByCode(roleAssignment.getRoleCode());
            if (role != null) {
                authority = RoleGrantedAuthority.ofResourceRole(role);
            }
        } else if (RoleAssignmentRoleType.ROW_LEVEL.equals(roleAssignment.getRoleType())) {
            RowLevelRole role = rowLevelRoleRepository.findRoleByCode(roleAssignment.getRoleCode());
            if (role != null) {
                authority = RoleGrantedAuthority.ofRowLevelRole(role);
            }
        }
        return authority;
    }

// ...
2 Likes

Thank you