【Shiro】第三章 Shiro入门(二)

简介: 【Shiro】第三章 Shiro入门(二)

【2.2.3】定义DefinitionRealm

1. package com.itheima.shiro.realm;
2. 
3. import com.itheima.shiro.service.SecurityService;
4. import com.itheima.shiro.service.impl.SecurityServiceImpl;
5. import org.apache.shiro.authc.*;
6. import org.apache.shiro.authz.AuthorizationInfo;
7. import org.apache.shiro.realm.AuthorizingRealm;
8. import org.apache.shiro.subject.PrincipalCollection;
9. 
10. /**
11.  * @Description:声明自定义realm
12.  */
13. public class DefinitionRealm extends AuthorizingRealm {
14. 
15. /**
16.      * @Description 认证接口
17.      * @param token 传递登录token
18.      * @return
19.      */
20. @Override
21. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
22. //从AuthenticationToken中获得登录名称
23. String loginName = (String) token.getPrincipal();
24. SecurityService securityService = new SecurityServiceImpl();
25. String password = securityService.findPasswordByLoginName(loginName);
26. if ("".equals(password)||password==null){
27. throw new UnknownAccountException("账户不存在");
28.         }
29. //传递账号和密码
30. return  new SimpleAuthenticationInfo(loginName,password,getName());
31.     }
32. 
33. 
34. @Override
35. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
36. return null;
37.     }
38. 
39. }

【2.2.4】编辑shiro.ini

1. #声明自定义的realm,且为安全管理器指定realms
2. [main]
3. definitionRealm=com.itheima.shiro.realm.DefinitionRealm
4. securityManager.realms=$definitionRealm
5. #声明用户账号
6. #[users]
7. #jay=123

【3】认证源码跟踪

(1)通过debug模式追踪源码subject.login(token) 发现。首先是进入Subject接口的默认实现类。果然,Subject将用户的用户名密码委托给了securityManager去做。

(2)然后,securityManager说:“卧槽,认证器authenticator小弟,听说你的大学学的专业就是认证呀,那么这个认证的任务就交给你咯”。遂将用户的token委托给内部认证组件authenticator去做

(3)事实上,securityManager的内部组件一个比一个懒。内部认证组件authenticator说:“你们传过来的token我需要拿去跟数据源Realm做对比,这样吧,这个光荣的任务就交给Realm你去做吧”。Realm对象:“一群大懒虫!”。

(4)Realm在接到内部认证组件authenticator组件后很伤心,最后对电脑前的你说:“大兄弟,对不住了,你去实现一下呗”。从图中的方法体中可以看到,当前对象是Realm类对象,即将调用的方法是doGetAuthenticationInfo(token)。而这个方法,就是你即将要重写的方法。如果帐号密码通过了,那么返回一个认证成功的info凭证。如果认证失败,抛出一个异常就好了。你说:“什么?最终还是劳资来认证?”没错,就是苦逼的你去实现了,谁叫你是程序猿呢。所以,你不得不查询一下数据库,重写doGetAuthenticationInfo方法,查出来正确的帐号密码,返回一个正确的凭证info

(5)好了,这个时候你自己编写了一个类,继承了AuthorizingRealm,并实现了上述doGetAuthenticationInfo方法。你在doGetAuthenticationInfo中编写了查询数据库的代码,并将数据库中存放的用户名与密码封装成了一个AuthenticationInfo对象返回。可以看到下图中,info这个对象是有值的,说明从数据库中查询出来了正确的帐号密码

(6)那么,接下来就很简单了。把用户输入的帐号密码与刚才你从数据库中查出来的帐号密码对比一下即可。token封装着用户的帐号密码,AuthenticationInfo封装着从数据库中查询出来的帐号密码。再往下追踪一下代码,最终到了下图中的核心区域。如果没有报异常,说明本次登录成功。

3、编码、散列算法

【1】编码与解码

Shiro提供了base64和16进制字符串编码/解码的API支持,方便一些编码解码操作。

Shiro内部的一些数据的【存储/表示】都使用了base64和16进制字符串

【1.1】需求

理解base64和16进制字符串编码/解码

【1.2】新建项目

新建shiro-day01-03encode-decode

【1.3】新建EncodesUtil

1. package com.itheima.shiro.tools;
2. 
3. import org.apache.shiro.codec.Base64;
4. import org.apache.shiro.codec.Hex;
5. 
6. /**
7.  * @Description:封装base64和16进制编码解码工具类
8.  */
9. public class EncodesUtil {
10. 
11. /**
12.      * @Description HEX-byte[]--String转换
13.      * @param input 输入数组
14.      * @return String
15.      */
16. public static String encodeHex(byte[] input){
17. return Hex.encodeToString(input);
18.     }
19. 
20. /**
21.      * @Description HEX-String--byte[]转换
22.      * @param input 输入字符串
23.      * @return byte数组
24.      */
25. public static byte[] decodeHex(String input){
26. return Hex.decode(input);
27.     }
28. 
29. /**
30.      * @Description Base64-byte[]--String转换
31.      * @param input 输入数组
32.      * @return String
33.      */
34. public static String encodeBase64(byte[] input){
35. return Base64.encodeToString(input);
36.     }
37. 
38. /**
39.      * @Description Base64-String--byte[]转换
40.      * @param input 输入字符串
41.      * @return byte数组
42.      */
43. public static byte[] decodeBase64(String input){
44. return Base64.decode(input);
45.     }
46. 
47. }
相关文章
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17713 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1399 5
|
14天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2446 8
|
19天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3970 22
|
21天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。

热门文章

最新文章