1.看验证方式数据库规则,IDEA的请按ctrl+shift+R搜索
2.users.ddl里面就是jdbcAuthentication()验证要使用的具体规则,下面代码就是
create table users(username varchar_ignorecase(50) not null primary key,password varchar_ignorecase(500) not null,enabled boolean not null);
create table authorities (username varchar_ignorecase(50) not null,authority varchar_ignorecase(50) not null,constraint fk_authorities_users foreign key(username) references users(username));
create unique index ix_auth_username on authorities (username,authority);
3.用上面代码放入sql执行创建好数据库具体如图
4.你设置的权限前缀放入数据库一定要加ROLE_,不然不会生效,enabled属性是用户是否生效1是0否
5.注意这里的密码通过jdbcAuthentication()验证一定要使用加密,不加密它识别不了
6.我用的是BCryptPasswordEncoder
7.现在直接上jdbcAuthentication()验证代码,久等了。(已配置加密方法BCryptPasswordEncoder())
import javax.sql.DataSource;
@Autowired
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username,password,enabled from users WHERE username=?")
.authoritiesByUsernameQuery("select username,authority from authorities where username=?")
.passwordEncoder(new BCryptPasswordEncoder());
}
8.注册请加密
public static String encodePassword(String password) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder.encode(password);
}
9.就是通过username查看的,你看sql语句就知道了,数据源你自己配
spring.datasource.url=jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver