spring security5使用jdbcAuthentication()验证

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
  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值