如果我使用的是auth.jdbcAuthentication().dataSource(datasource);,它将是jdbcAuthentication
现在我有两个数据库,一个用于app,另一个用于安全性。
我找不到使用authenticationProvider()来配置要使用哪个数据库的方法
@Autowired
@Qualifier("securityDataSource")
private DataSource securityDataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// use jdbc authentication ... oh yeah!!!
auth.jdbcAuthentication().dataSource(securityDataSource);
auth.authenticationProvider(authenticationProvider());
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
auth.setUserDetailsService(userService); //set the custom user details service
auth.setPasswordEncoder(passwordEncoder()); //set the password encoder - bcrypt
return auth;
}
我是如何解决这个问题的
@Configuration
@EnableJpaRepositories(basePackages={"${spring.data.jpa.security.repository.packages}"},
entityManagerFactoryRef = "securityEntityManagerFactory")
public class DemoSecurityDataSourceConfig {
@Bean
@ConfigurationProperties(prefix="security.datasource")
public DataSource securityDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.data.jpa.security")
public LocalContainerEntityManagerFactoryBean securityEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("securityDataSource") DataSource securityDataSource) {
return builder
.dataSource(securityDataSource)
.build();
}
}
@Configuration
@EnableJpaRepositories(basePackages={"${spring.data.jpa.app.repository.packages}"},
entityManagerFactoryRef = "appEntityManagerFactory")
public class DemoAppDataSourceConfig {
@Primary
@Bean
@ConfigurationProperties(prefix="app.datasource")
public DataSource appDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean
@ConfigurationProperties(prefix="spring.data.jpa.app")
public LocalContainerEntityManagerFactoryBean appEntityManagerFactory(EntityManagerFactoryBuilder builder, DataSource appDataSource) {
return builder
.dataSource(appDataSource)
.build();
}
}
发布于 2020-11-03 12:00:45
您可以通过编程方式配置DataSources
,然后使用JdbcUserDetailsManager
来配置Spring Security应该使用哪个and。
在下面的示例中,有两个DataSources
,一个使用H2,一个使用MySQL。
Spring Security使用安全DataSource
,因为我们已经添加了“安全”@Qualifier
。
@Bean("security")
public DataSource securityDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:org/springframework/security/core/userdetails/jdbc/users.ddl")
.build();
}
@Bean
@Primary
public DataSource primaryDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url("jdbc:mysql://localhost:3306/db_example");
dataSourceBuilder.username("springuser");
dataSourceBuilder.password("ThePassword");
return dataSourceBuilder.build();
}
@Bean
UserDetailsManager users(@Qualifier("security") DataSource dataSource) {
UserDetails user = User.builder()
.username("user")
.password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
.roles("USER")
.build();
JdbcUserDetailsManager users = new JdbcUserDetailsManager(dataSource);
users.createUser(user);
return users;
}
https://stackoverflow.com/questions/64584225
复制相似问题