1、定义bean
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
2、自动装配bean
@Autowired
DataSource dataSource;
注意:该类需要
@ComponentScan("com.zte.blog")
3、配置
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username,password,enabled,register_time from users where username = ?")
.authoritiesByUsernameQuery("select username,authority from authorities where username = ?").rolePrefix("ROLE_")
.passwordEncoder(new StandardPasswordEncoder());
}
4、新建用户及授权的两张表
CREATE TABLE `users` (
`username` varchar(100) NOT NULL,
`password` varchar(100) DEFAULT NULL,
`enabled` tinyint(1) DEFAULT NULL,
`register_time` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `authorities` (
`username` varchar(100) NOT NULL,
`authority` varchar(100) DEFAULT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
注意:更新users表的同时一定要更新authorities表