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程序 ...
随机推荐
- adb shell 杀进程以及端口占用,adbserver服务重启失败
linux: adb shell ps |grep netease 杀进程: adb shell kill [PID] //杀死进程 C:\Users\chenquan>adb shell ...
- C++-HDU1000,1001,1002-格式是真的坑
#include <cstdio> int main(){ for(int a,b;~scanf("%d%d",&a,&b);printf(" ...
- 【译】PHP 内核 — 字符串管理
[译]PHP 内核 - 字符串管理 (Strings management: zend_string 译文) 原文地址:http://www.phpinternalsbook.com/php7/int ...
- 拓扑排序 判断给定图是否存在合法拓扑序列 自家oj1393
//拓扑排序判断是否有环 #include<cstdio> #include<algorithm> #include<string.h> #include<m ...
- Python之xml读写
遇到问题xml文件读写,没有子节点需要新建ChildNode. # -*- coding: utf-8 -*- import os import shutil import xml.dom.minid ...
- 源码安装python 报错,openssl: error while loading shared libraries: libssl.so.1.1
在执行openssl version出现如下错误: openssl: error while loading shared libraries: libssl.so.1.1: cannot open ...
- python+selenium:浏览器webdriver操作(1)--基本对象定位
1.简单对象的定位-----自动化测试的核心 对象的定位应该是自动化测试的核心,要想操作一个对象,首先应该识别这个对象.一个对象就是一个人一样,他会有各种的特征(属性),如比我们可以通过一个人的身份证 ...
- 【c++】零基础的自修课 01-开发工具的安装(code::blocks)
1/开发工具的下载地址: ·(选用 visual studio开发工具,有区分mac和windows版本)https://visualstudio.microsoft.com/zh-hans/down ...
- AcWing 240. 食物链
#include <iostream> using namespace std; ; int n, m; int p[N], d[N]; //p是baba,d是距离 int find(in ...
- 短网址(t.cn、url.cn)生成,网址缩短接口API
简要说明 短网址api接口有很多格式,不同的接口生成的短网址格式也不同,比如常见的t.cn.url.cn.w.url.cn等格式.总而言之短网址接口就是用来将一个冗长的链接缩短成10个字符以内的短链接 ...