前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Shiro认证过程

Shiro认证过程

作者头像
疯狂的KK
发布2020-02-19 11:23:03
5990
发布2020-02-19 11:23:03
举报
文章被收录于专栏:Java项目实战

Shiro的架构了解之后,走一下debug,跟一下认证的流程。使用Realm来认证用户名密码。

使用realm访问数据库里的数据

获取当前的subject

校验subject是否已经登录

若没有认证则封装用户名密码

1.0创建表单页面 存储提交

2.0请求提交到mvc的handler

3.0获取用户名密码

4.0执行登录:调用subject的login(token)

5.0自定义realm,从数据库获取对应记录,返回给shiro

Realm实现类AuthenticatingRealm

代码语言:javascript
复制
protected abstract AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken var1) throws AuthenticationException;

实现该方法

6.0Shiro完成对密码的比对

代码语言:javascript
复制
currentUser.login(token);login方法的实现
void login(AuthenticationToken var1) throws AuthenticationException;

向下走,看下实现

代码语言:javascript
复制
public void login(AuthenticationToken token) throws AuthenticationException {
    this.clearRunAsIdentitiesInternal();
    Subject subject = this.securityManager.login(this, token);
    String host = null;
    PrincipalCollection principals;
    if (subject instanceof DelegatingSubject) {
        DelegatingSubject delegating = (DelegatingSubject)subject;
        principals = delegating.principals;
        host = delegating.host;
    } else {
        principals = subject.getPrincipals();
    }

    if (principals != null && !principals.isEmpty()) {
        this.principals = principals;
        this.authenticated = true;
        if (token instanceof HostAuthenticationToken) {
            host = ((HostAuthenticationToken)token).getHost();
        }

        if (host != null) {
            this.host = host;
        }

        Session session = subject.getSession(false);
        if (session != null) {
            this.session = this.decorate(session);
        } else {
            this.session = null;
        }

    } else {
        String msg = "Principals returned from securityManager.login( token ) returned a null or empty value.  This value must be non null and populated with one or more elements.";
        throw new IllegalStateException(msg);
    }
}
代码语言:javascript
复制
info = this.authenticate(token);

public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
    return this.authenticator.authenticate(token);
}

最终调用的

代码语言:javascript
复制
==============
public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    AuthenticationInfo info = this.getCachedAuthenticationInfo(token);
    if (info == null) {
        info = this.doGetAuthenticationInfo(token);
        log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);
        if (token != null && info != null) {
            this.cacheAuthenticationInfoIfPossible(token, info);
        }
    } else {
        log.debug("Using cached authentication info [{}] to perform credentials matching.", info);
    }

    if (info != null) {
        this.assertCredentialsMatch(token, info);
    } else {
        log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}].  Returning null.", token);
    }

    return info;
}
代码语言:javascript
复制
int i = token.hashCode();
此时的hashcode与 info = this.doGetAuthenticationInfo(token);
doGetAuthenticationInfo获取的token是一致的

但是会由于缓存 不能达到登录后在返回同样验证的效果

Shiro如何比对密码

token中保存了从数据库获取的密码 以及从前台传过来的密码

然后进行比对

密码的比对

代码语言:javascript
复制
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
    Object tokenCredentials = getCredentials(token);
    Object accountCredentials = getCredentials(info);
    return equals(tokenCredentials, accountCredentials);
}
public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
    this.credentialsMatcher = credentialsMatcher;
}

认证流程走完,能够明确Shiro还是需要数据库中的数据来跟前台数据进行比对密码,如果不能跳转页面或者走到方法,需要在applicationcontext.xml中配置URL

上天给了你一份礼物,你很会讲故事的,但除非有人替你看好,孩子很容易丢掉自己的礼物,如果你的父母没办法照顾好你,那只好由我来

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-02-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 赵KK日常技术记录 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档