1

私はしばらくの間SpringSecurityLDAPに苦労してきましたが、ついに提出に打ち負かされたと思ったとき、再びつまずきました。

シナリオ:ユーザーに組織の資格情報を使用してログインさせ、それを内部ユーザーデータベースと照合します。それらが存在しない場合は、ローカルで作成してからローカル権限を設定します(LDAPサーバーにアクセスできず、LDAPサーバーに対して認証するだけなので、そこに権限を追加することはできません)。これは問題なく機能します...しかし、LDAPからいくつかのフィールドを追加したいと思います-それらのフルネームと電子メールアドレス、そしてこれはそれがうまくいかないところです。

コントローラでLDAP検索を実行しています-次のコードを使用しています(ldapTemplateをさらに上に挿入しました):

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

これは完全に機能します。しかし、認証後と呼ばれるサービスでこのコードを複製すると、ldapTemplateは常にnullになります...いくつかの異なることを試しましたが、正しく入力できません...誰かが光を当てることができますかなぜですか?午前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)
}

}

2番目の編集:SpringSecurityService.reauthenticateを使用して、LDAPではなくセッション中にローカルユーザーを使用できるようにするなど、さまざまなことを試みてきましたが、このBeanもnullです...私のサービスにBeanをまったく注入できません。

私は最終的にこの投稿を見つけました:http://burtbeckwith.com/blog/ ?p = 993回避策を与えてくれました、そして今それは機能しています...しかし私はそれが最初になかった理由を知りたいです場所...私はGrailsで学ぶことがたくさんあります-そしてどこから始めればよいのかわかりません。

よろしくお願いします

スティーブ

4

1 に答える 1

2

authenticationSuccessHandlerをどのように注入していますか?resources.xmlでauthenticationSuccessHandlerを定義してみてください

Grails1.3.5とSpringSecurityCore

于 2012-02-05T02:18:50.273 に答える