获取subject对象
1、SecurityUtils.getSubject()
2、调用的是ThreadContext
![](https://i-blog.csdnimg.cn/blog_migrate/a8086c790b4fc1ad6ce8ca8c44555dc7.png)
2、从ThreadContext获取Subject
1、其内部有静态创建了ThreadLocal<Map<Object,Object>>
2、获取subject的key:org.apache.shiro.util.ThreadContext_SUBJECT_KEY
3、WebDelegationgSubject
![](https://i-blog.csdnimg.cn/blog_migrate/f830822f70126c32a38da2d5bcd5fd2c.png)
3、调用login方法
1、创建usernamepasswordtoken
此处验证Remember是否启用
![](https://i-blog.csdnimg.cn/blog_migrate/16504af633410fa2236dc06651fcb744.png)
2、进入login方法
1、clear run as identities internal
清除内部运行身份
![](https://i-blog.csdnimg.cn/blog_migrate/95640f04f26ef95cf17648ad9f9e2781.png)
2、获取session,无则不创建,有删除RUN_AS_PRINCIPALS_SESSION_KEY的值
注意:
获取session方法在DelegationgSubject类中,
![](https://i-blog.csdnimg.cn/blog_migrate/3890486252d5314ac66e022c08427481.png)
4、调用securitymanager的login方法:
该实例对象为DefaultSecurityManager
1、info = authenticate(token);
该步为了获取用户真实信息
1、获取realms,并进行验证
2、ModularRealmAuthenticator为realm管理
2、返回info若是为空则表示验证不成功
notifyfail(token, info);
成功则:notifySuccess(token, info);
此处有AuthenticationListener监听器:
监听验证登录情况:
可以扩展ip限制登录次数,该用用户短时间不能在登录
![](https://i-blog.csdnimg.cn/blog_migrate/99d2a333c054c10ec66e550aabd420c1.jpeg)
5、返回到login方法执行createsubject(token,info,subject)
![](https://i-blog.csdnimg.cn/blog_migrate/e64466bb8078ede316bbe5e000d40d0b.png)
6、
1、创建SubjectContext:
subject容器:1、sessionid等所有信息
![](https://i-blog.csdnimg.cn/blog_migrate/0b7bdeb901bf0544099cb0c27931826f.png)
subject容器:1、设置用户验证成功true
2、存入token和info
3、存入existing即subject
![](https://i-blog.csdnimg.cn/blog_migrate/c48fee9d9b95c8f90f219e4d7bf9b7df.jpeg)
7、调用createSubject(context)
以下是方法讲解:
1、copy将context中属性全部复制到DefaultSecurityManager中的DefaultSubjectcontext
2、查看容器里有没有securitymanager,无则从ThreadContext创建
3、查看容器里面有没有session(暂时不懂其方法)
4、查看容器里面有没有用户信息
5、通过context创建subuject
![](https://i-blog.csdnimg.cn/blog_migrate/07b7f1f4d46204c6e7f7afc5c23c9c28.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/e20afdaa6b23031540222e115a3e8fe1.jpeg)
1、
![](https://i-blog.csdnimg.cn/blog_migrate/8bcc5f87fd9c2664d44d54a74f3f5ec6.jpeg)
2、查看容器里有没有securitymanager,无则从ThreadContext创建
![](https://i-blog.csdnimg.cn/blog_migrate/a6501493499ae7df96a8d3d665aeec4c.png)
最终:创建WebDelegatingSubject
注意此处的SessionEnabled修改为true了,之前所有创建session都为false导致创建失败
![](https://i-blog.csdnimg.cn/blog_migrate/270c9644284208f053846a51f516cd17.png)