shiro认证权限流程测试精讲篇

shiro权限框架课程目标精讲篇(1)
shiro初识,全面介绍架构精讲篇
shiro三大核心组件介绍精讲篇
shiro配置jar包导入依赖精讲篇
shiro认证权限流程测试精讲篇
shiro权限授权测试精讲篇
shiro权限模型构建数据库表精讲篇
shiro权限框架自定义realm认证测试精讲篇
shiro散列算法密码加盐测试精讲篇
shiro权限管理自定义realm授权测试精讲篇
spring集成shiro不进入自定义realm是为什么?
shiro认证:连接数据库操作doGetAuthenticationInfo
shiro授权:链接数据库doGetAuthorizationInfo
shiro框架:缓存器
Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name
shiro框架:缓存器Ehcache的详细配置流程
shiro框架:sessionManager设置数据会话的存储时间,账号密码到时间清空
shiro框架集成验证码验证配置流程
shiro框架:记住我remberMe自动登入
用户认证,用户去访问系统,系统要验证用户身份的合法性。最常用的用户身份验证的方法:1、用户名密码方式、2、指纹打卡机、3、基于证书验证方法。。系统验证用户身份合法,用户方可访问系统的资源。

用户认证流程

shiro认证权限流程测试精讲篇_第1张图片
image.png

subject:主体,理解为用户,可能是程序,都要去访问系统的资源,系统需要对subject进行身份认证。

principal:身份信息,通常是唯一的,一个主体还有多个身份信息,但是都有一个主身份信息(primary principal)

credential:凭证信息,可以是密码 、证书、指纹。

总结:主体在进行身份认证时需要提供身份信息和凭证信息。

shiro认证权限流程测试精讲篇_第2张图片
image.png

1、应用程序构建了一个终端用户认证信息的AuthenticationToken 实例后,调用Subject.login方法。
�2、Sbuject的实例通常是DelegatingSubject类(或子类)的实例对象,在认证开始时,会委托应用程序设置的securityManager实例调用securityManager.login(token)方法。
�3、SecurityManager接受到token(令牌)信息后会委托内置的Authenticator的实例(通常都是ModularRealmAuthenticator类的实例)调用authenticator.authenticate(token). ModularRealmAuthenticator在认证过程中会对设置的一个或多个Realm实例进行适配,它实际上为Shiro提供了一个可拔插的认证机制。

4、如果在应用程序中配置了多个Realm,ModularRealmAuthenticator会根据配置的AuthenticationStrategy(认证策略)来进行多Realm的认证过程。在Realm被调用后,AuthenticationStrategy将对每一个Realm的结果作出响应。 �注:如果应用程序中仅配置了一个Realm,Realm将被直接调用而无需再配置认证策略。 �5、判断每一个Realm是否支持提交的token,如果支持,Realm将调用getAuthenticationInfo(token); getAuthenticationInfo 方法就是实际认证处理,我们通过覆盖Realm的doGetAuthenticationInfo方法来编写我们自定义的认证处理。

shiro认证权限流程测试精讲篇_第3张图片
image.png

配置shiro.ini

通过Shiro.ini配置文件初始化SecurityManager环境。

配置 eclipse支持ini文件编辑:

shiro认证权限流程测试精讲篇_第4张图片
image.png

在eclipse配置后,在classpath创建shiro.ini配置文件,为了方便测试将用户名和密码配置的shiro.ini配置文件中:
配置数据:


shiro认证权限流程测试精讲篇_第5张图片
image.png

shiro-first.ini

#\u5bf9\u7528\u6237\u4fe1\u606f\u8fdb\u884c\u914d\u7f6e
[users]
#\u7528\u6237\u8d26\u53f7\u548c\u5bc6\u7801 
zhangsan=111111
lisi=22222

认证代码

public class AuthenticationTest {

    // 用户登陆和退出
    @Test
    public void testLoginAndLogout() {

        // 创建securityManager工厂,通过ini配置文件创建securityManager工厂
        Factory factory = new IniSecurityManagerFactory(
                "classpath:shiro-first.ini");

        // 创建SecurityManager
        SecurityManager securityManager = factory.getInstance();

        // 将securityManager设置当前的运行环境中
        SecurityUtils.setSecurityManager(securityManager);

        // 从SecurityUtils里边创建一个subject
        Subject subject = SecurityUtils.getSubject();

        // 在认证提交前准备token(令牌)
        // 这里的账号和密码 将来是由用户输入进去
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan",
                "111111");

        try {
            // 执行认证提交
            subject.login(token);
        } catch (AuthenticationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 是否认证通过
        boolean isAuthenticated = subject.isAuthenticated();

        System.out.println("是否认证通过:" + isAuthenticated);

        // 退出操作
        subject.logout();

        // 是否认证通过
        isAuthenticated = subject.isAuthenticated();

        System.out.println("是否认证通过:" + isAuthenticated);

    }

认证执行流程

1、通过ini配置文件创建securityManager
2、调用subject.login方法主体提交认证,提交的token
3、securityManager进行认证,securityManager最终由ModularRealmAuthenticator进行认证。
4、ModularRealmAuthenticator调用IniRealm(给realm传入token) 去ini配置文件中查询用户信息
5、IniRealm根据输入的token(UsernamePasswordToken)从 shiro-first.ini查询用户信息,根据账号查询用户信息(账号和密码)
如果查询到用户信息,就给ModularRealmAuthenticator返回用户信息(账号和密码)
如果查询不到,就给ModularRealmAuthenticator返回null
6、ModularRealmAuthenticator接收IniRealm返回Authentication认证信息
如果返回的认证信息是null,ModularRealmAuthenticator抛出异常(org.apache.shiro.authc.UnknownAccountException)

如果返回的认证信息不是null(说明inirealm找到了用户),对IniRealm返回用户密码 (在ini文件中存在)和 token中的密码 进行对比,如果不一致抛出异常(org.apache.shiro.authc.IncorrectCredentialsException)

二、认证:
通过ini配置文件创建securityManager--------通过token获取主体的值------subject.login提交认证------执行验证由ModularRealmAuthenticator调用自定义Realm-----获取token身份信息---跟数据库做比对
判断是否为空------如果查询到返回认证信息AuthenticationInfo(认证器),如果查不到,抛出异常,报查询不到用户

常见的异常

 UnknownAccountException
账号不存在异常如下:
org.apache.shiro.authc.UnknownAccountException: No account found for user。。。。

 IncorrectCredentialsException
当输入密码错误会抛此异常,如下:
org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - zhangsan, rememberMe=false] did not match the expected credentials.

更多如下:
DisabledAccountException(帐号被禁用)
LockedAccountException(帐号被锁定)
ExcessiveAttemptsException(登录失败次数过多)
ExpiredCredentialsException(凭证过期)等

catch ( IncorrectCredentialsException ice ) {
message="密码错误";
return ERROR;
} catch ( LockedAccountException lae ) {
message="用户被锁";
return ERROR;
} catch ( ExcessiveAttemptsException eae ) {
message="用户名或密码错误";
return ERROR;
} catch ( AuthenticationException ae ) {
message="用户名或密码错误";
return ERROR;
}
}

currentUser.logout(); //退出代码

小结:

ModularRealmAuthenticator作用进行认证,需要调用realm查询用户信息(在数据库中存在用户信息)
ModularRealmAuthenticator进行密码对比(认证过程)。

realm:需要根据token中的身份信息去查询数据库(入门程序使用ini配置文件),如果查到用户返回认证信息,如果查询不到返回null。

你可能感兴趣的:(shiro认证权限流程测试精讲篇)