shiro学习(一)---认证入门程序
来源:互联网 发布:高一历史优化学案答案 编辑:程序博客网 时间:2024/06/05 17:48
1、导入jar包
2、创建shiro-first.ini文件
[users]zhangsan=111111lisi=22222
3、创建AuthenticationTest测试类
public class AuthenticationTest { // 用户登陆和退出 @Test public void testLoginAndLogout() { // 创建securityManager工厂,通过ini配置文件创建securityManager工厂 Factory<SecurityManager> 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); }}
4、进行Junit Test
获得结果
执行流程
- 通过ini配置文件创建securityManager。
- 调用subject.login方法提交认证。
内部通过securityManager进行认证,securityManager通过authenticated最终由ModularRealmAuthenticator进行认证。 ModularRealmAuthenticator拿着输入的token(UsernamePasswordToken)和shiro-first.ini中的配置的用户名和密码进行对比,IniRealm负责从shiro-first.ini中查询用户信息(账号和密码)
iniRealm查询账号时找不到给ModularRealmAuthenticator返回null(说明账号不存在)
账号不存在则抛出异常:
org.apache.shiro.authc.UnknownAccountException: Realm [org.apache.shiro.realm.text.IniRealm@6a9dd62a] was unable to find account data。。。如果上一步查询到用户
ModularRealmAuthenticator进行密码匹配,如果不对,ModularRealmAuthenticator抛出异常:
org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - zhangsan, rememberMe=false] did not match the expected credentials.
- shiro学习(一)---认证入门程序
- shiro认证入门程序
- shiro 认证(一)
- Shiro入门学习一
- Shiro入门学习一
- shiro学习(二)---授权入门程序
- Shiro 学习记录 Shiro 入门程序
- shiro 入门,认证
- Shiro入门—认证
- 深入浅出学Shiro(一)--登录认证
- 深入浅出学Shiro(一)--登录认证
- shiro学习(一)
- shiro学习(一)
- shiro学习(一)
- Apache Shiro学习(二)认证
- Shiro-学习总结-认证
- apache shiro 入门(一)
- Shiro入门5:Shiro认证的HelloWorld
- 二分查找
- Lasso Regression
- ${0##*/} ${0#*/} ${0%/*} ${0%%/*}
- springBoot集成MongoDB
- 第十四周 项目一 验证算法 1.
- shiro学习(一)---认证入门程序
- C++的深拷贝与浅拷贝
- 小甲的数据爬虫与人工智能直播
- 程序员女神跟你聊聊比特币与区块链
- 关于VMware虚拟机安装镜像时黑屏的解决办法
- 741. Cherry Pickup
- Hbase 相关概念
- mysql字符串类型的字段按数值大小排序
- 第十二周——“Dijkstra算法的验证”