gpt4 book ai didi

grails - 在 Grails 服务中注入(inject) LdapTemplate 以返回用户详细信息会导致空指针异常

转载 作者:行者123 更新时间:2023-12-02 14:36:52 25 4
gpt4 key购买 nike

我已经在 Spring Security LDAP 上苦苦挣扎了一段时间,就在我最终认为我已经被打败了的时候,它又把我绊倒了。

场景:我让用户使用他们的组织凭据登录,然后我检查内部用户数据库。如果它们不存在,我会在本地创建它们,然后设置它们的本地权限(我无法触摸 LDAP 服务器,只能对其进行身份验证 - 所以我无法在此处添加权限)。这很好用......但是我想从 LDAP 添加几个字段 - 他们的全名和电子邮件地址,这就是出错的地方。

我有 LDAP 搜索在 Controller 中工作 - 使用以下代码(我已进一步注入(inject) ldapTemplate):

def fullName = ldapTemplate.search("", "(uid=" + uid + "*)", new AttributesMapper() {
@Override
public Object mapFromAttributes(Attributes attrs) throws NamingException {
return attrs.get("cn").get()
}
})

这完美地工作。但是,当我在称为身份验证后的服务中复制此代码时,ldapTemplate 始终为空......我尝试了一些不同的东西,但我无法让它正确填充......任何人都可以阐明为什么?我希望我从凌晨 4 点 21 分开始就在做一些愚蠢的事情,我应该在大约 6 小时前就已经在床上了......

编辑:这是所要求的服务代码,感谢您看一下 Burt - 它目前不是很强大,因为我没有让它工作过去只是创建一个新的本地用户 - 还有工作要做。
package com.myPackage

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.springframework.ldap.core.AttributesMapper
import org.springframework.security.core.Authentication
import org.springframework.security.core.authority.GrantedAuthorityImpl
import org.springframework.security.web.authentication.AuthenticationSuccessHandler
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler

import org.apache.commons.lang.RandomStringUtils

class PostAuthHandlerService implements AuthenticationSuccessHandler {

def springSecurityService
def ldapTemplate

private AuthenticationSuccessHandler target = new SavedRequestAwareAuthenticationSuccessHandler();
private List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]

public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication auth) {
def username = auth.principal.getAt("username")
User.withTransaction { status ->
// Check to see if this user exists in the local db
def user = User.findByUsername(username)
if (!user) { // User doesn't exist yet, so create and make a user...
def userRole = Role.findByAuthority('ROLE_USER') ?:
new Role(authority: 'ROLE_AM').save(failOnError: true)
user = new User(
username: username,
password: auth.getCredentials() ?: placeholderPassword(),
displayName: getLdapDetails("username", "cn") ?: "",
email: getLdapDetails("username", "mail") ?: "",
enabled: true).save(failOnError: true)
UserRole.create user, userRole
}
else {
println("--- You exist already! Updating details")
user.displayName = getLdapDetails("username", "cn") ?: ""
}
target.onAuthenticationSuccess(request, response, auth)
}
}

def getLdapDetails(username, attr) {
return ldapTemplate.search("", "(uid=$username*)", new AttributesMapper() {
@Override
public Object mapFromAttributes(Attributes attrs) throws NamingException {
return attrs.get(attr).get()
}
})
}

def placeholderPassword () {
// This is here because we also allow local logins for some users.
// If the password is not available, then create a big ugly one...
return org.apache.commons.lang.RandomStringUtils.randomAlphanumeric(128)
}

public void proceed(HttpServletRequest request,
HttpServletResponse response, Authentication auth) {
target.onAuthenticationSuccess(request, response, auth)
}

}

第二次编辑:我一直在尝试各种不同的事情——包括尝试使用 springSecurityService.reauthenticate 来让本地用户在 session 期间使用而不是 LDAP 用户——而且这个 bean 也是空的......看起来我根本无法将任何 bean 注入(inject)我的服务。

我最终找到了这篇文章: http://burtbeckwith.com/blog/?p=993这给了我一个解决方法,我现在让它工作......但我仍然想知道为什么它没有放在首位......我在 Grails 中有很多东西要学习 - 而且很难知道在哪里开始。

提前喝彩

史蒂夫

最佳答案

你是如何注入(inject) authenticationSuccessHandler 的?您应该尝试在 resources.xml 中定义 authenticationSuccessHandler

Grails 1.3.5 and Spring Security Core

关于grails - 在 Grails 服务中注入(inject) LdapTemplate 以返回用户详细信息会导致空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9141915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com