我在spring web安全和我的数据库方面遇到了一些问题。如果我使用
@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
}它工作得很好,但有时在setDataSource之前调用configureGlobal,我会得到一个IllegalStateException,因为dataSource在使用之前没有被注入。
我想知道让第一种方法工作还需要什么。
还有,有没有办法控制@Autowired的顺序。将@DependsOn(DataSource)添加到configureGlobal不起作用。
发布于 2016-04-29 03:55:26
使用字段注入而不是Setter注入
@Configuration
@EnableWebSecurity
public class BBSecurity extends WebSecurityConfigurerAdapter {
@Autowired private DataSource dataSource;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
// Same stuff
}
}或者直接将Datasource注入configureGlobal方法:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
// same stuff
}https://stackoverflow.com/questions/36920186
复制相似问题