私はスプリングウェブのセキュリティとデータベースに問題があります。私は、メソッドが正常に呼び出されスプリングセキュリティの設定に失敗する
@Configuration
@EnableWebSecurity
public class BBSecurity extends WebSecurityConfigurerAdapter {
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> cfg = auth.jdbcAuthentication();
cfg.dataSource(dataSource);
cfg.usersByUsernameQuery("SELECT user_name, password, true FROM user_data WHERE user_name=?");
cfg.passwordEncoder(new MyPasswordEncoder());
cfg.authoritiesByUsernameQuery("SELECT user_name, concat('ROLE_',role) FROM user_data WHERE user_name=?");
}
}
を使用する場合は、しかし、ログに私はこの
Using default security password: 81456c65-b6fc-43ee-be41-3137d02b122b
と私のデータベースのコードが使用されることはありません参照してください。
場合は、代わりに、私は(同じクラスに)
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> cfg = auth.jdbcAuthentication();
... same config code as above
}
を使用し、それが正常に動作しますが、時にはconfigureGlobal
がsetDataSource
前に呼び出され、それが使用された前dataSource
が注入されていなかったので、私はIllegalStateException
を取得します。
私は最初のメソッドを動作させるために必要なことを理解したいと思います。
@Autowired
の順番を制御する方法もあります。 @DependsOn(DataSource)
〜configureGlobal
を追加しても効果はありません。
はconfigure' 'に' Autowired'を置くようにしてください方法。また、 'configureGlobal'と' configure'を同時に使いましたか? –
autowiredをconfigureメソッドに置くことは、2番目のメソッドである 'AuthenticationManagerBuilder'の注入であり、configureのオーバーライドではありません。 configureは、http://stackoverflow.com/questions/35218354/difference-between-registerglobal-configure-configureglobal-configureglo – dcsalmon
への答えに示唆されています。注入順序の問題を除いて、Autowiredの作業は、以前に 'AuthenticationManagerBuilder'が注入されることがあります'dataSource'ですが、なぜconfigureが動作するように見えるのか興味がありますが、設定された' AuthenticationManagerBuilder'はその後認証に使用されません。 – dcsalmon