shiro登录过程

本文探讨了作者对Shiro框架登录过程的好奇与研究,通过跟踪源码了解到Shiro并不直接连接数据库验证用户信息,而是需要自定义登录验证。文章邀请读者一起探索并指正理解的不足。
摘要由CSDN通过智能技术生成

接触和使用shiro还是有好大一段时间,可惜并没有搞明白其实的原理和机制。因为工作中使用的框架都封装好了,so......并没有去研究。

原来一直猜想的是shiro可以直接连接数据库,验证用户名和密码。但是又没在实体类中找到有什么特殊的标记注解之类的。

就有点好奇了,于是趁这两天工作不是那么紧张了,就跟了下源码。

如有不对,请留言指正。蟹蟹!

红色部分是主要代码;

UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), password, rememberMe.equals("0") ? false : true);
Subject currentUser = AppUtils.getSubject();
try {
    currentUser.login(token);
} catch (AuthenticationException e) {
    token.clear();
    model.addAttribute("user_err", "用户名或密码错误");
    return loginUrl;
}

将登录委托给subjectManager

public void login(AuthenticationToken token) throws AuthenticationException {
    clearRunAsIdentitiesInternal();
    Subject subject = securityManager.login(this, token);

    PrincipalCollection principals;

    String host = null;

    if (subject instanceof DelegatingSubject) {
        DelegatingSubject delegating = (DelegatingSubject) subject;
        //we have to do this in case there are assumed identities - we don't want to lose the 'real' principals:
        principals = delegating.principals;
        host = delegating.host;
    } else {
        principals = subject.getPrincipals();
    }

    if (principals == null || principals.isEmpty()) {
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值