Using email and username as login in spring security plugin in Grails

I use the spring security plugin in my Grails application, and I would like users to sign in to the application with the username or email address provided during the registration process. (like fb)

I already looked on the Internet and turned to Grails docs: http://grails-plugins.imtqy.com/grails-spring-security-core/docs/manual/guide/single.html#4.1%20Person%20Class

but did not find any solution.

Any help would be greatly appreciated.

Thanks in advance

+4
source share
2 answers

spring -security, org.springframework.security.core.userdetails.UserDetailsService

public class MyUserServiceImpl implements UserDetailsService{

    @Autowired
    private AdminUserDao dao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {

    ... Here comes your logic to get your the user based on email or userid ...

    }
}

spring UserDetails:

<authentication-manager>
    <authentication-provider user-service-ref="myUserServiceImpl" />
</authentication-manager>

HTH

+8

, - . spring security/grails 3.2.8. :

, . , attributes.email User.findByUsernameOrEmail(username,username)

, UserEmailUserService :

package com.app.users

import grails.plugin.springsecurity.userdetails.GormUserDetailsService
import grails.transaction.Transactional
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException

class UserEmailUserService extends GormUserDetailsService{

    UserDetails loadUserByUsername(String username, boolean loadRoles)
            throws UsernameNotFoundException {
        return loadUserByUsername(username)
    }

    @Transactional
    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //enable login with either username or password
        User user = User.find {
            username == username || attributes.email == username
        }
        if (!user) throw new UsernameNotFoundException('User not found', username)
        UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.username, user.getPassword(),
                user.enabled, !user.accountExpired, !user.passwordExpired, !user.accountLocked, getAuthorities(user.roles))
        Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities())
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return userDetails
    }

    public static List<GrantedAuthority> getAuthorities(Set<Role> roles) {
       List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>()
       roles?.each { role ->
          authorities.add(new SimpleGrantedAuthority(role.authority))
       }
       return authorities
   }
}

conf/spring/resources.groovy:

import com.app.users.UserEmailUserService

// Place your Spring DSL code here
beans = {
      userDetailsService(UserEmailUserService){
            grailsApplication = ref('grailsApplication')
        }
}

, user.roles , . ..:

Set<Role> getRoles() {
    UserRole.findAllByUser(this)*.role
}
0

All Articles