Shiro入门学习之散列算法与凭证配置(六)
一、散列算法概述
散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5、SHA等,一般进行散列时最好提供一个salt(“盐”),什么意思?举个栗子:加密密码“admin”,产生的散列值是21232f297a57a5a743894a0e4a801fc3,可以到一些md5解密网站(注:并不是真正解密,而是通过穷举法不断尝试)很容易通过散列值得到密码(admin),即如果直接对密码进行散列相对来说破解更容易,此时我们加入一些只有系统知道的干扰数据,如用户名和ID(盐);这样散列对象是“密码+用户名+ID”,这样生成的散列值来说更难破解。
二、凭证配置
鄙人能力有限,不会叙述其是怎么实现的,有能力的可以自己去百度了。。这里仅仅记录我学习Shiro过程中通过MD5散列算法对密码进行加密,这也是Shiro框架的一部分。
1、新建module,添加如下pom依赖
<properties>
<shiro.version>1.4.1</shiro.version>
<loggingg.version>1.2</loggingg.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${loggingg.version}</version>
</dependency>
</dependencies>
2、配置凭证
(1)第一种方式:在自定义Realm的构造方法中设置匹配器

实现如下:

关键代码如下:
public class UserRealm extends AuthorizingRealm {
private UserService userService = new UserServiceImpl();
private RoleService roleService = new RoleServiceImpl();
private PermissionService permissionService = new PermissionServiceImpl();
public UserRealm()
{
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//指定加密算法
hashedCredentialsMatcher.setHashAlgorithmName("md5");
//指定散列次数
hashedCredentialsMatcher.setHashIterations(2);
//这里没有设置salt(盐)的api,为什么这么设计?因为一般salt(盐)存储在数据库
setCredentialsMatcher(hashedCredentialsMatcher);
}
/**
* 做认证
*
* @param token
* @return
* @throws AuthenticationException
*/
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username1 = token.getPrincipal().toString();
User user1 = userService.queryUserByUserName(username1);
if (user1 != null) {
List<String> roles1 = roleService.queryRoleByUserName(username1);
List<String> permissions1 = permissionService.queryPermissionByUserName(username1);
ActivityUser activityUser1 = new ActivityUser(user1, roles1, permissions1);
System.out.println();
//参数1:可以传任意对象的|参数2:数据库中的用户密码|参数3:当前类名
//SimpleAuthenticationInfo info1 = new SimpleAuthenticationInfo(activityUser1, user1.getPwd(), this.getName());
ByteSource byteSource = ByteSource.Util.bytes("北京");
SimpleAuthenticationInfo info1 = new SimpleAuthenticationInfo(activityUser1, user1.getPwd(), byteSource, this.getName());
return info1;
} else {
return null;
}
}
//授权方法
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
System.out.println("doGetAuthorizationInfo被回调了!");
//
Object primaryPrincipal = principal.getPrimaryPrincipal();
System.out.println(primaryPrincipal);
ActivityUser activityUser = (ActivityUser) principal.getPrimaryPrincipal();
List<String> roles = activityUser.getRoles();
if (roles != null && roles.size() > 0) {
info.addRoles(roles);
}
List<String> permissins = activityUser.getPermissins();
if (permissins!=null&&permissins.size()>0)
{
info.addStringPermissions(permissins);
}
//判断如果是超级管理员
//info.addStringPermission("*:*");
return info;
}
}
关键代码
(2)第2种方式:shiro.ini设置凭证匹配器

(3)第3种方式:代码中设置凭证匹配器

关键代码如下:
public class TestAuthorizationRealm
{
public static void main(String[] args)
{
//1.模拟前台传递的用户名和密码
String username = "zhangsan";
String password = "123456";
//2.创建安全管理器的工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//3.通过安全管理器工厂获取安全管理器
DefaultSecurityManager securityManager = (DefaultSecurityManager)factory.getInstance();
//4.创建自定义的Realm
UserRealm userRealm = new UserRealm();
//4.1设置凭证匹配器
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//4.2指定加密算法
hashedCredentialsMatcher.setHashAlgorithmName("md5");
//4.3指定散列次数
hashedCredentialsMatcher.setHashIterations(2);
//4.4这里没有设置salt(盐)的api,为什么这么设计?因为一般salt(盐)存储在数据库
userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
//5.设置自定义的Realm
securityManager.setRealm(userRealm);
//6.将安全管理器绑定到当前运行环境
SecurityUtils.setSecurityManager(securityManager);
//7.从当前环境中获取Subject主体
Subject subject1 = SecurityUtils.getSubject();
//8.调用主体的登录方法
try
{
subject1.login(new UsernamePasswordToken(username,password));
System.out.println("登录成功~"); // Object principal = subject1.getPrincipal();
// System.out.println(principal); } catch (IncorrectCredentialsException e) {
System.out.println("密码不正确");
}catch (UnknownAccountException e) {
System.out.println("用户名不存在");
} boolean role1 = subject1.hasRole("role1");
boolean role2 = subject1.hasRole("role1");
System.out.println(role1); boolean permitted = subject1.isPermitted("user:add");
System.out.println(permitted);
}
}
关键代码
三、总结
1、以前我们是拿用户名和密码去匹配数据库中有没有User对象,而在Shiro中是通过用户名查询User对象,将前台获取到的明文进行加密(md5、sha等等)与数据库中的密文密码作比对
2、以上3种设置凭证匹配器本质都是一样的
Shiro入门学习之散列算法与凭证配置(六)的更多相关文章
- java学习-sha1散列算法
直接调用HashKit.sha1(String str)方法就可以了,,返回的是16进制的字符串长度是40, 也就是用md.digest()方法解析出来的字节数是160字节长度. 而MD5散列算法生成 ...
- shiro进行散列算法操作
shiro最闪亮的四大特征:认证,权限,加密,会话管理 为了提高应用系统的安全性,这里主要关注shiro提供的密码服务模块: 1.加密工具类的熟悉 首先来个结构图,看看shiro提供了哪些加密工具类: ...
- shiro中自定义realm实现md5散列算法加密的模拟
shiro中自定义realm实现md5散列算法加密的模拟.首先:我这里是做了一下shiro 自定义realm散列模拟,并没有真正链接数据库,因为那样东西就更多了,相信学到shiro的人对连接数据库的一 ...
- PHP密码散列算法的学习
不知道大家有没有看过 Laravel 的源码.在 Laravel 源码中,对于用户密码的加密,使用的是 password_hash() 这个函数.这个函数是属于 PHP 密码散列算法扩展中所包含的函数 ...
- shiro入门学习--使用MD5和salt进行加密|练气后期
写在前面 在上一篇文章<Shiro入门学习---使用自定义Realm完成认证|练气中期>当中,我们学会了使用自定义Realm实现shiro数据源的切换,我们可以切换成从关系数据库如MySQ ...
- Shiro入门学习与实战(一)
一.概述 1.Shiro是什么? Apache Shiro是java 的一个安全框架,主要提供:认证.授权.加密.会话管理.与Web集成.缓存等功能,其不依赖于Spring即可使用: Spring S ...
- Android数据加密之SHA安全散列算法
前言: 对于SHA安全散列算法,以前没怎么使用过,仅仅是停留在听说过的阶段,今天在看图片缓存框架Glide源码时发现其缓存的Key采用的不是MD5加密算法,而是SHA-256加密算法,这才勾起了我的好 ...
- PKI和加密,散列算法
Day 11-PKI和加密,散列算法 PKI(Public Key Infrastructure公钥基础设施) 1 PKI(Public Key Infrastructure公钥基础设施)回顾学习 - ...
- 个人理解c#对称加密 非对称加密 散列算法的应用场景
c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...
随机推荐
- sql查询 —— 连接查询
-- 执行sql文件 test.sql -- 打开终端 -- cd sql文件所在路径 -- 进入mysql -- use 数据库 -- 执行 source test.sql; -- 自关联 -- 一 ...
- SSM项目中的.tld文件是什么,有什么作用?怎么自定义tld文件
原文链接:https://www.cnblogs.com/guaishoubiubiu/p/8721277.html TLD术语解释:标签库描述文件,如要在JSP页面中使用自定义JSP标签,必须首先定 ...
- Hibernate的基本工作原理
Hibernate开发过程中会用到5个核心接口: 1.Configuration2.SessionFactory3.Session4.Transaction5.QueryHibernate就是通过这些 ...
- codeforces div2_604 E. Beautiful Mirrors(期望+费马小定理)
题目链接:https://codeforces.com/contest/1265/problem/E 题意:有n面镜子,你现从第一面镜子开始询问,每次问镜子"今天我是否美丽",每天 ...
- Mysql中判断是否存在
不能像sqlserver一样用if not exists或者exists,应该这样: DECLARE p_count int; set p_count=0; select 1 into p_count ...
- Uncaught TypeError: o.a is not a constructor
最近在学webpack打包工具,看着挺好玩的,虽然也是工作需要 首先安装啥的我就不说了,后面慢慢写,打包完了以后,运行代码发现提示这个 找半天代码没问题啊,我这个js基础薄弱的人用这个webpack还 ...
- 安装Elasticsearch出现 node validation exception 的问题处理
es报错如下: [2019-10-11T16:23:28,945][ERROR][o.e.b.Bootstrap ] [es-node-1] node validation exception[3] ...
- Dreamoon and WiFi
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands throug ...
- bootstrap的字体设置
@font-face { font-family: 'Glyphicons Halflings'; src: url('../fonts/glyphicons-halflings-regular.eo ...
- python 网页中文显示Unicode码
print repr(a).decode("unicode–escape") 注:a是要输出的结果,