我的应用程序中有一个控制器 需要用户使用外部数据库 进行验证
我已经设置了一个自定义用户对象,
class CustomUserDetails extends GrailsUser {
final String externalId
CustomUserDetails(String username, String password, boolean enabled,
boolean accountNonExpired, boolean credentialsNonExpired,
boolean accountNonLocked,
Collection<GrantedAuthority> authorities,
long id, String externalId) {
super(username, password, enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, authorities, id)
this.externalId = externalId
}
}
和自定义验证文件Provider
class CustomAuthenticationProvider implements AuthenticationProvider {
def springSecurityService
Authentication authenticate(Authentication customAuth) {
/* Do stuff to validate the user s credentials here */
def userDetails = new CustomUserDetails(customAuth.getPrincipal(), customAuth.getCredentials(), true, true, true, true,
[new GrantedAuthorityImpl( ROLE_SPECIAL_USER )], 9999999, "externalDatabaseIdString")
def token = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.authorities)
return token
}
boolean supports(Class authentication) {
return true
}
}
我在配置. groovy 中输入了条目, 将它添加到春季安全性。 提供者Names 列表中, 并在 conf/ spring/ resources. groovy 中添加了以下内容。 groovy
beans = {
customAuthenticationProvider(info.proadvisors.auth.CustomAuthenticationProvider){ bean -> bean.autowire = "byName" }
userDetailsService(org.codehaus.groovy.grails.plugins.springsecurity.GormUserDetailsService){ bean -> bean.autowire = "byName" }
}
这里的问题 - 在我的控制器中, 弹簧保安正在被注射, 但弹簧保安是无效的。 getCrentUser () 。 当我试图访问外部标识属性时, 返回一个无效指针例外 。 外部标识属性应该在验证的用户对象上 。
如果在我的自定义授权Provider中,我用GormUser Details Service给我一个圣杯用户的物品,并用它来制造标记,那么控制器就能正常工作,获得当前用户的作品。
有什么想法 为什么这不起作用吗?