views:

81

answers:

1

Hi, is there a way to force the login to be case insensitive using acegi in grails ? I didn't find a matching entry to be set in the SecurityConfig file. Thanks

+2  A: 

There's no support for this, but it's easy enough to create a custom UserDetailsService, see http://www.grails.org/AcegiSecurity+Plugin+-+Custom+UserDetailsService

Something like this should work:

package com.yourcompany.yourapp

import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserImpl

import org.springframework.security.GrantedAuthority
import org.springframework.security.GrantedAuthorityImpl
import org.springframework.security.userdetails.UserDetails
import org.springframework.security.userdetails.UserDetailsService
import org.springframework.security.userdetails.UsernameNotFoundException

class MyUserDetailsService implements UserDetailsService {

   UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      def userDetails

      User.withTransaction { status ->
         def users = User.executeQuery(
            'from User where lower(username) = :username',
            [username: username.toLowerCase()])
         if (users.size() == 1) {
            def user = users[0]
            GrantedAuthority[] authorities = user.authorities.collect {
               new GrantedAuthorityImpl(it.authority) }

            userDetails = new GrailsUserImpl(
                  user.username, user.password, user.enabled,
                  true, true, true, authorities, user)
         }
      }

      if (!userDetails) {
         throw new UsernameNotFoundException('User not found', username)
      }

      userDetails
   }
}
Burt Beckwith