我正在使用带有spring-security-rest,spring-security-core和spring-security-ui插件的Grails v2.4.2。
我编写了一个自定义UserDetailsService来使用户名不区分大小写。我所做的只是试图覆盖
UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException
我的 com.example.core.CaseInsensitiveUserDetailsService 类定义为:
class CaseInsensitiveUserDetailsService extends GormUserDetailsService {
/**
* Make The Username Case Insensitive
*/
UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException {
Person.withTransaction { status ->
log.debug "Case Insensitive User Details Service"
// Find The Username
def user = Person.findByUsernameIlike(username)
// If User Not Found, Throw Exception
if (!user) {
log.warn "User not found: $username"
throw new UsernameNotFoundException('User not found', username)
}
Collection<GrantedAuthority> authorities = loadAuthorities(user, username, loadRoles)
createUserDetails user, authorities
}
}
}
我的 resources.groovy 包含:
beans = {
userDetailsService(com.example.core.CaseInsensitiveUserDetailsService)
credentialsExtractor(Grails24CredentialExtractor)
// Some Custom Filters Are Also Defined (securityContextRepository, securityContextPersistenceFilter, multipartResolver)
}
它成功编译,但它实际上从未运行我的自定义 CaseInsensitiveUserDetailsService 。在控制台中,我看到来自实际GormUserDetailsService的调试语句而不是我的自定义调试语句。如何使用我的自定义UserDetailsService?
** 注意:我一直在关注这两个教程:
答案 0 :(得分:0)
在使用不同的插件和Spring Security做类似的事情时,我遇到了类似的问题,并且需要明确地通过名称进行布线。试试resources.groovy
:
userDetailsService(com.example.core.CaseInsensitiveUserDetailsService) { bean->
bean.autowire = "byName"
}
答案 1 :(得分:0)
我为教育(自我)目的写了这个例子,也许它可能有所帮助: https://github.com/grails-coder/grails-spring-security
它关注:
请注意它是在grails 3.2.5
之上