[英]Spring Security Configuration Fails
I am having some issues with spring web security and my database. 我在Spring Web Security和数据库方面遇到一些问题。 If I use 如果我用
@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=?");
}
}
The method is successfully called, but in the log I see this 该方法已成功调用,但是在日志中我看到了
Using default security password: 81456c65-b6fc-43ee-be41-3137d02b122b
and my database code is never used. 而且我的数据库代码从未使用过。
If, instead, I use (in the same class) 相反,如果我使用(在同一堂课中)
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> cfg = auth.jdbcAuthentication();
... same config code as above
}
it works fine, but sometimes configureGlobal
is called before setDataSource
and I get an IllegalStateException
because dataSource
was not injected before it was used. 它工作正常,但是有时在setDataSource
之前调用configureGlobal
,并且我得到一个IllegalStateException
因为在使用dataSource
之前没有注入它。
I would like to understand what else is needed to make the first method work. 我想了解使第一种方法起作用所需的其他条件。
Also is there any way to control the order of @Autowired
. 还有什么方法可以控制@Autowired
的顺序。 Adding @DependsOn(DataSource)
to configureGlobal
has no effect. 将@DependsOn(DataSource)
添加到configureGlobal
无效。
Use Field Injection instead of Setter Injection : 使用字段注入而不是二传手注入 :
@Configuration
@EnableWebSecurity
public class BBSecurity extends WebSecurityConfigurerAdapter {
@Autowired private DataSource dataSource;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
// Same stuff
}
}
Or inject the Datasource
directly to the configureGlobal
method: 或将Datasource
直接注入configureGlobal
方法:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
// same stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.