使用acegi在Grails中不区分大小写的登录

时间:2010-06-19 01:52:26

标签: grails login spring-security

有没有办法强制登录在grails中使用acegi不区分大小写?我没有在SecurityConfig文件中找到要设置的匹配条目。感谢

1 个答案:

答案 0 :(得分:2)

对此没有支持,但创建自定义UserDetailsS​​ervice很容易,请参阅http://www.grails.org/AcegiSecurity+Plugin+-+Custom+UserDetailsService

这样的事情应该有效:

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