一、散列算法概述

  散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如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入门学习之散列算法与凭证配置(六)的更多相关文章

  1. java学习-sha1散列算法

    直接调用HashKit.sha1(String str)方法就可以了,,返回的是16进制的字符串长度是40, 也就是用md.digest()方法解析出来的字节数是160字节长度. 而MD5散列算法生成 ...

  2. shiro进行散列算法操作

    shiro最闪亮的四大特征:认证,权限,加密,会话管理 为了提高应用系统的安全性,这里主要关注shiro提供的密码服务模块: 1.加密工具类的熟悉 首先来个结构图,看看shiro提供了哪些加密工具类: ...

  3. shiro中自定义realm实现md5散列算法加密的模拟

    shiro中自定义realm实现md5散列算法加密的模拟.首先:我这里是做了一下shiro 自定义realm散列模拟,并没有真正链接数据库,因为那样东西就更多了,相信学到shiro的人对连接数据库的一 ...

  4. PHP密码散列算法的学习

    不知道大家有没有看过 Laravel 的源码.在 Laravel 源码中,对于用户密码的加密,使用的是 password_hash() 这个函数.这个函数是属于 PHP 密码散列算法扩展中所包含的函数 ...

  5. shiro入门学习--使用MD5和salt进行加密|练气后期

    写在前面 在上一篇文章<Shiro入门学习---使用自定义Realm完成认证|练气中期>当中,我们学会了使用自定义Realm实现shiro数据源的切换,我们可以切换成从关系数据库如MySQ ...

  6. Shiro入门学习与实战(一)

    一.概述 1.Shiro是什么? Apache Shiro是java 的一个安全框架,主要提供:认证.授权.加密.会话管理.与Web集成.缓存等功能,其不依赖于Spring即可使用: Spring S ...

  7. Android数据加密之SHA安全散列算法

    前言: 对于SHA安全散列算法,以前没怎么使用过,仅仅是停留在听说过的阶段,今天在看图片缓存框架Glide源码时发现其缓存的Key采用的不是MD5加密算法,而是SHA-256加密算法,这才勾起了我的好 ...

  8. PKI和加密,散列算法

    Day 11-PKI和加密,散列算法 PKI(Public Key Infrastructure公钥基础设施) 1 PKI(Public Key Infrastructure公钥基础设施)回顾学习 - ...

  9. 个人理解c#对称加密 非对称加密 散列算法的应用场景

    c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...

随机推荐

  1. uGUI源码调试

    uGUI源代码地址:https://bitbucket.org/Unity-Technologies/ui 工具编译后转换位置{Unity3D_Vserion}\Editor\Data\UnityEx ...

  2. 慎用--skip-grant-tables命令

    该命令作用是跳过授权表,也就是说谁都能进入mysql看到所有数据表,输入任意字符账号密码都可以 当忘记账号密码时可以使用改命令修改密码,但是要随用随关,重启mysql,不然服务器上会有很大的风险. 介 ...

  3. array.find()方法

    //array.find(function(currentValue, index, arr),thisValue) 方法说明 let tempArray = [ {"key":1 ...

  4. 结合sqlmap进行sql注入过程

    结合sqlmap进行sql注入:(-r后面是通过burp suite抓出来的请求包:-p后面是注入点,即该请求里携带的某个参数) Get请求的注入: ./sqlmap.py -r rss_test.t ...

  5. 联想ideapad关闭Fn

    1.打开bios 开启/重启电脑的时候长按Fn+F2,就可以打开bios面板 2.切换到configuration菜单 使用键盘的右箭头将切换到configuration 3.关闭Fn 使用键盘下箭头 ...

  6. 记一次if控制器的使用

    1.添加if控制器 2.输入判断条件:常见的就是某个变量是不是等于某个值 3.或者用函数助手中的函数 每个版本jmeter函数助手的入口不同,我的直接在菜单上: 选择__jexl3,输入判断条件,点击 ...

  7. import matplotlib.pyplot as plt出错

    >>>import matplotlib.pyplot as plt /usr/lib/python2.7/dist-packages/matplotlib/font_manager ...

  8. 类欧几里得模板 p5170

    //类欧几里得的模板题 p5170 //求这三个式子: //第一个跟后两个没关联 //后两个跟其余两个都有关联: #include<cstdio> #include<algorith ...

  9. windows 动态库导出

    以下内容来自博客:https://blog.csdn.net/fengbingchun/article/details/78825004 __declspec是Microsoft VC中专用的关键字, ...

  10. python map、join函数

    map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表. map(fun ...