开发者

Case insensitive login in Grails with acegi

开发者 https://www.devze.com 2023-01-04 13:09 出处:网络
is there a way to force the login to开发者_Python百科 be case insensitive using acegi in grails ? I didn\'t find a matching entry to be set in the SecurityConfig file. ThanksThere\'s no support for th

is there a way to force the login to开发者_Python百科 be case insensitive using acegi in grails ? I didn't find a matching entry to be set in the SecurityConfig file. Thanks


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
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消