Shiro学习之旅——SimpleAuthenticationInfo

本文详细介绍了如何在Spring Security中通过`doGetAuthenticationInfo`方法实现用户身份验证,涉及数据库查询、密码匹配和SimpleAuthenticationInfo构造。重点在于参数解析和实际操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例代码

@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
//        token中获得username,再去查数据库
        Users users = userService.findUsersByUsername(username);
        if (users == null) {
            throw new UnknownAccountException("不存在用户");
        } else {
//            肯定有这个用户就比对密码
            //当前realm对象的name
            String name = this.getName();
            System.out.println("realmName:" + name);
            ByteSource credentialsSalt = ByteSource.Util.bytes(users.getUserName());
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(users, users.getPassword(), credentialsSalt, name);
            return info;
        }

new SimpleAuthenticationInfo(users, users.getPassword(), credentialsSalt, name)解释参数

参数1:从数据库获得的用户对象,包括用户名和密码等信息
参数2:从对象中取密码,users.getPassword()是这个用户的数据库中的密码
参数3:盐,可以为空
参数4:当前realm的名字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值