0

Spring Security で同じ問題に再び直面しています:「パスワードが保存された値と一致しません」。

カスタム クラスを使用してグラフ (SpringData/Neo4J を使用) に 4 つのアカウントをインポートGraphPopulatorし、1 つ ("fbiville"/"s3cret") でログインしようとしています。

認証は次のように構成されます。

<beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder">
    <beans:constructor-arg value="******" />
</beans:bean>

<beans:bean id="userService" class="com.lateralthoughts.devinlove.service.LoginService" />

<authentication-manager>
    <authentication-provider user-service-ref="userService">
        <password-encoder ref="encoder" />
    </authentication-provider>
</authentication-manager>

また、アカウントの永続化を担当するクラスは、部分的にカスタム SpringData リポジトリ実装に基づいています。

class PersonRepositoryImpl implements PersonRepositoryCustom {

    private final PasswordEncoder passwordEncoder;
    private final Neo4jOperations template;

    @Autowired
    public PersonRepositoryImpl(PasswordEncoder passwordEncoder,
                            Neo4jOperations template) {

        this.passwordEncoder = passwordEncoder;
        this.template = template;
    }

    @Override
    @Transactional
    public void persist(Person person) {
        person.setPassword(passwordEncoder.encode(person.getPassword()));
        template.save(person);
    }
}

最後に、ログイン プロセスは次のように構成されます。

<http auto-config='true' use-expressions='true' realm="devinlove: love is just another algorithm">
    <form-login   login-page="/login"
                  default-target-url="/"
                  authentication-failure-url="/login" />
    <intercept-url pattern="/login" access="isAnonymous()" />
    <intercept-url pattern="/**" access="hasRole('USER')" />
</http>

アカウントの作成時とユーザーのログイン試行時に StandardPasswordEncoder をデバッグしたところ、明らかに認証エラーにつながるソルトが一致していないことに気付きました。

問題を再現したい場合は、リポジトリを複製できます。

前もって感謝します !

ロルフ

4

1 に答える 1

-1

org.springframework.security.authentication.encoding.PasswordEncoderではなく、実装を使用する必要がありますorg.springframework.security.crypto.password.PasswordEncoder

したがって、次のように簡単に使用できます。

<authentication-manager>
    <authentication-provider user-service-ref="userService">
        <password-encoder hash="sha" base64="true">
            <!--Single salt - the very same as you are using in `encoder` instance-->
            <salt-source system-wide="********"/>
        </password-encoder>
    </authentication-provider>
</authentication-manager>

@Autowiredこれはプロパティで適切に機能することに注意してください。ただし、@QualifierBean は一度だけ表す必要があります。

このようなインターフェースの実装を公開します:org.springframework.security.authentication.encoding.PasswordEncoderおよびorg.springframework.security.authentication.dao.SaltSource.

したがって、特定のケースでは、次のようになります。

class PersonRepositoryImpl implements PersonRepositoryCustom {

    private final PasswordEncoder passwordEncoder;
    private final Neo4jOperations template;

    @Autowired
    public PersonRepositoryImpl(PasswordEncoder passwordEncoder,
                                SaltSource saltSource
                                Neo4jOperations template) {

        this.passwordEncoder = passwordEncoder;
        this.saltSource = saltSource;
        this.template = template;
    }

    @Override
    @Transactional
    public void persist(Person person) {
        person.setPassword(passwordEncoder.encode(person.getPassword(), saltSource));
        template.save(person);
    }
}
于 2013-04-01T09:42:08.603 に答える