2

springsecurityコアとspringsecurity openidプラグインを使用して、私のgrailsアプリケーションのopenIdとSpring Securityの統合に取り組んでいます。統合しましたが、うまく機能しますが、ログインしている人のメールにアクセスする必要があります。どうすればそれを取得できますか。アクセスできるのは、個人を識別するために使用されるトークンだけです。

4

2 に答える 2

4

イアン・ロバーツに感謝します。彼は私にこの返事をくれます。これは私の問題を正確に解決します。 彼の返事は:

たまたま昨日、私のアプリケーションの 1 つにまさにこれを実装しました :-) 残念ながら、それはオープンソースのアプリではないので、私のコードを示すことはできませんが、私が何をしたかを説明することはできます。

spring-security-openid プラグインは、OpenID の「属性交換」メカニズムをサポートしていますが、サポートについてはあまり文書化されていません (あったとしても)。どれだけうまく機能するかは、遠端のプロバイダーによって異なりますが、これは少なくとも Google と Yahoo を使用している私には機能しました。

プロバイダーからメールアドレスをリクエストするには、Config.groovy に以下を追加する必要があります。

grails.plugins.springsecurity.openid.registration.requiredAttributes.email = "http://axschema.org/contact/email"

これをユーザー登録プロセスに組み込むには、S2 ユーザー ドメイン クラスに email フィールドが必要であり、生成された OpenIdController.groovy をいくつかの場所で編集する必要があります。

  • 電子メール プロパティを OpenIdRegisterCommand に追加する

  • createAccount アクションには、ユーザー名、パスワード、openid をパラメーターとして渡す "if(!createNewAccount(...))" 行があります。メソッド定義とともにこれを変更して、これら 2 つのフィールドだけでなく、コマンド オブジェクト全体を渡します。

  • createNewAccount で、メールの値をコマンド オブジェクトからユーザー ドメイン オブジェクト コンストラクターに渡します。

最後に、電子メールの入力フィールドを grails-app/views/openId/createAccount.gsp に追加します。

氏名などの他の属性でも同じことができます。

grails.plugins.springsecurity.openid.registration.requiredAttributes.fullname = "http://axschema.org/namePerson"

これらを結び付ける上で重要なことは、requiredAttributes (この例では fullname) に続く最後のドットの後の部分が、OpenIdRegisterCommand のプロパティの名前と一致している必要があるということです。

よろしく チャル・ジェイン

于 2012-01-21T11:49:18.163 に答える
1

私は springsecurity openid プラグインを使用したことがありませんが、springsecurity コアを使用すると、カスタム を実装することで現在のユーザーに関する追加情報を公開できますUserDetails。私のアプリではname、ログインしているユーザーのプロパティを表示できるように、この実装を追加しました。代わりに電子メール アドレスが公開されるように、これを少し変更する必要があります。

/**
 * Custom implementation of UserDetails that exposes the user's name
 * http://grails-plugins.github.com/grails-spring-security-core/docs/manual/guide/11%20Custom%20UserDetailsService.html
 */
class CustomUserDetails extends GrailsUser {

    // additional property
    final String name

    CustomUserDetails(String username,
                      String password,
                      boolean enabled,
                      boolean accountNonExpired,
                      boolean credentialsNonExpired,
                      boolean accountNonLocked,
                      Collection<GrantedAuthority> authorities,
                      long id,
                      String displayName) {

        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities, id)
        this.name = displayName
    }
}

UserDetailsService次に、上記のクラスのインスタンスを返すカスタム実装を作成する必要があります

class UserDetailsService implements GrailsUserDetailsService {

    /**
     * Some Spring Security classes (e.g. RoleHierarchyVoter) expect at least one role, so
     * we give a user with no granted roles this one which gets past that restriction but
     * doesn't grant anything.
     */
    static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]

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

    UserDetails loadUserByUsername(String username) {
        User.withTransaction { status ->

            User user = User.findByUsername(username)
            if (!user) {
                throw new UsernameNotFoundException('User not found', username)
            }

            def authorities = user.authorities.collect {new GrantedAuthorityImpl(it.authority)}
            return new CustomUserDetails(
                    user.username,
                    user.password,
                    user.enabled,
                    !user.accountExpired,
                    !user.passwordExpired,
                    !user.accountLocked,
                    authorities ?: NO_ROLES,
                    user.id,
                    user.name)
        }
    }
}

このクラスのインスタンスを という名前の Spring Bean として登録する必要がありますuserDetailsService。以下を追加してこれを行いましたResources.groovy

userDetailsService(UserDetailsService)
于 2012-01-19T15:12:55.297 に答える