散列算法,Remal使用散列算法

一、散列算法

散列算法让其保证不可逆,安全。这里举一个例子sh1的摘要算法。上代码

/*** 散列算法* @author Administrator*/
public class HashRsaUtil {/*** 加密方式*/public static final String SHA1="SHA-1";/*** 加密次数*/public static final Integer ITERATIONS=512;/*** sh1摘要算法* @param input 传入的参数* @param salt 干扰素(加盐)* @return*/public static String sha1(String input, String salt){return new SimpleHash(SHA1,input,salt,ITERATIONS).toString();}/*** 随机生成salt* @return 返回一个hex编码的salt*/public static String generateSalt(){SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator();return generator.nextBytes().toHex();}/*** 铭文加密返回密文格式* @param inscription 要加密的铭文* @return 返回salt和密文*/public static Map<String,String> encryptInscription( String inscription){Map<String,String> map = new HashMap<>(16);String salt = generateSalt();String ciphertext = sha1(inscription, salt);map.put("salt",salt);map.put("ciphertext",ciphertext);return map;}

二、Remal使用散列算法

1.修改service模拟数据库出来的数据
/*** 模拟数据库出来的数据* 将123转成密文* @param userName* @return*/@Overridepublic Map<String, String> findPasswordByName(String userName) {return HashRsaUtil.encryptInscription("123");}
2.修改自定义的remal
package com.example.config;import com.example.service.impl.SecurityServiceImpl;
import com.example.untils.HashRsaUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.util.StringUtils;import java.util.Map;/*** 自定义的realm* 继承授权的接口** @author Administrator*/
public class DefinitionRealm extends AuthorizingRealm {/*** 鉴权** @param principalCollection* @return*/@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {return null;}/*** 指定完比较器之后还需要修改比较器,因为当前使用的还是默认的* 比较器,需要改成咱们自己的比较器*/public DefinitionRealm(){//指定密码匹配方式has1//使用Hashed密码比较器//指定算法HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(HashRsaUtil.SHA1);//指定密码迭代次数matcher.setHashIterations(HashRsaUtil.ITERATIONS);//使用父层方法使匹配方式生效,将我们指定的比较器写进去setCredentialsMatcher(matcher);}/*** 认证** @param authenticationToken* @return* @throws AuthenticationException*/@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {//获取登录名String principal = (String) authenticationToken.getPrincipal();//然后模拟用登录名去数据库拿到密码SecurityServiceImpl securityService = new SecurityServiceImpl();//获取密文密码Map<String, String> map = securityService.findPasswordByName(principal);//判断拿到的密码是否为空if (StringUtils.isEmpty(map)) {throw new UnknownAccountException("该用户不存在!");}String salt = map.get("salt");String password = map.get("ciphertext");return new SimpleAuthenticationInfo(principal, password, ByteSource.Util.bytes(salt), getName());}
}
3.测试
 @Testpublic void shiroLoginTest() {//导入ini配置创建工厂IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");//工厂构建安全管理器SecurityManager securityManager = factory.getInstance();//使用工具生效安全管理器SecurityUtils.setSecurityManager(securityManager);//使用工具获取subject的主体Subject subject = SecurityUtils.getSubject();//构建账号密码UsernamePasswordToken passwordToken = new UsernamePasswordToken("zhangSan", "123");//使用subject主体去登录subject.login(passwordToken);//打印登录信息System.out.println("登录结果" + subject.isAuthenticated());}

结果:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/328739.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

简化得最没道理的6个汉字,让人大跌眼镜

文章来源于网络&#xff0c;侵删&#xff01;&#xff01;&#xff01; 1、“進”被简化为“进”&#xff0c;“進”字是让人“越走越佳”。 简化字却把它改成了“进”字&#xff0c;让你越走越走到“井”里去了&#xff0c;井底之蛙自生自灭。 2、“廠”被简化为“厂”字&…

c++

#include <iostream> #include<cmath> using namespace std; int main() { int x; double a; double b; while(cin >> x ){//注意while处理多个case for(int i 0 ;i < x ; i ){ cin>>a; …

在docker中运行ASP.NET Core Web API应用程序

本文是一篇指导快速演练的文章&#xff0c;将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤&#xff0c;在介绍的过程中&#xff0c;也会对docker的使用进行一些简单的描述。对于.NET Core以及docker的基本概念&#xff0c;网上已经有很多文章对其进行介绍了&…

谈谈 Java 的克隆

转载自 谈谈 Java 的克隆为什么要克隆对象 做开发很少用到克隆的。我能想得到的是用于调用方法时作为参数传递&#xff0c;为了保证方法调用前后对象的内部结构不被破坏&#xff0c;可以克隆一个对象作为参数传递。 使类具有克隆能力 有人可能注意到 Object 类中有一个 native…

android启调支付宝

网上找了一个可以起吊支付宝的appdemo &#xff0c;它集成了服务器端&#xff0c;我先将其分离为app和服务器端&#xff0c;保证app在接收参数后可以启调支付宝 &#xff08;保证app这边是正确的 不然出错都不知道是服务器出错还是app出错&#xff09;&#xff0c;在 找网上资…

shiro-身份授权流程、案例

一、身份授权流程 首先调用Subject.isPermitted/hasRole接口&#xff0c;委托给SecurityManager.SecurityManager接着会委托给内部组件Authorizer.Authorizer再将其请求委托给我们的Realm去做&#xff0c;Realm才是真正干活的.realm将用户请求的参数封装成权限对象&#xff0c…

对Java的URL类支持的协议进行扩展的方法

转载自 对Java的URL类支持的协议进行扩展的方法JAVA默认提供了对file,ftp,gopher,http,https,jar,mailto,netdoc协议的支持。当我们要利用这些协议来创建应用时&#xff0c;主要会涉及到如下几个类&#xff1a;java.net.URL、java.net.URLConnection、InputStream。URL类默认…

在.Net项目中使用Redis作为缓存服务

最近由于项目需要&#xff0c;在系统缓存服务部分上了redis&#xff0c;终于有机会在实际开发中玩一下&#xff0c;之前都是自己随便看看写写&#xff0c;很零碎也没沉淀下来什么&#xff0c;这次算是一个系统学习和实践过程的总结。 和Redis有关的基础知识 Redis是一个开源的分…

中国的程序员培训是不是有问题?

内容来源于&#xff0c;看最下面的出处&#xff0c;侵删 中国技术开放日的出海团对日本进行了为期一周的访问。笔者随行了头两天&#xff0c;参加Slush Asia大会&#xff0c;并访问了Gungho和Deloitte两家企业。虽然已经在日本生活了四年&#xff0c;但这样的体验却甚少&#x…

后台回调支付宝

https://blog.csdn.net/u012552275/article/details/78320051 网上找了一个可以起吊支付宝的appdemo &#xff0c;它集成了服务器端&#xff0c;我先将其分离为app和服务器端&#xff0c;保证app在接收参数后可以启调支付宝 &#xff08;保证app这边是正确的 不然出错都不知道…

解决高版本SpringBoot整合swagger时启动报错:Failed to start bean ‘documentationPluginsBootstrapper‘ 问题

一、控制台的报错信息 2021-12-29 15:15:04 [main] ERROR org.springframework.boot.SpringApplication - Application run failed org.springframework.context.ApplicationContextException: Failed to start bean documentationPluginsBootstrapper; nested exception is j…

java图片格式转化(例如jpg格式转化png)

转载自 java图片格式转化&#xff08;例如jpg格式转化png&#xff09; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Scanner;import javax.imageio.*; public class FormatConversion {public static final Str…

微软开源PowerShell并支持Linux

建议在Wifi 环境下观看视频 class"video_iframe" data-vidtype"1" style" z-index:1; " height"375" width"500" frameborder"0" data-src"https://v.qq.com/iframe/preview.html?vidv0322g7kd3f&width…

招银网络科技笔试

记录一下 招银网络笔试 2017年09月11日 14:32:53 阅读数&#xff1a;2450 Part1. 30道单选 涉及Java&#xff0c;C&#xff0c;多线程&#xff0c;算法&#xff0c;数据结构&#xff0c;CPU&#xff0c;NP问题&#xff0c;SQL语句&#xff0c;IP地址转换&#xff0c;行测。…

mybatisGenerator逆向工程

一、在pom文件中导入依赖和generator插件 <dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency&…

2016最佳温情小说:雨还在下....

作者 | 李德霞 来源 | 小小说选刊 哗&#xff0c;一道闪电&#xff1b;轰&#xff0c;一个响雷。 暴雨倾盆&#xff0c;天地间浑沌一片…… 老大扑腾腾坐起来&#xff0c;心也跟着扑腾腾地跳。老大拉亮灯&#xff0c;推推身边的媳妇。媳妇一骨碌爬起来&#xff0c;咋&#xf…

java 中 image 和 byte[] 相互转换

转载自 java 中 image 和 byte[] 相互转换只需要一个存储了图片信息的二进制串&#xff08;byte[]&#xff09; 然后&#xff0c;这样&#xff1a; InputStream buffin new ByteArrayInputStream(/*二进制串*/, /*起始位置*/,/*二进制串长度*/)); BufferedImage img ImageIO…

招银网络

记录一下 招银网络笔试 2017年09月11日 14:32:53 阅读数&#xff1a;2451 Part1. 30道单选 涉及Java&#xff0c;C&#xff0c;多线程&#xff0c;算法&#xff0c;数据结构&#xff0c;CPU&#xff0c;NP问题&#xff0c;SQL语句&#xff0c;IP地址转换&#xff0c;行测。…

Java 文件和byte数组转换

转载自 Java 文件和byte数组转换 /** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer null; try { File file new File(filePath); FileInputStream fis new FileInputStream(file); ByteArrayOutputStream bos new ByteAr…

json大文件导入数据库

json文件导入数据库 使用Navicat的客户端工具也可以实现json文件导入数据库&#xff0c;但是数据量大了之后&#xff0c;字段的值过于冗长可能会导致数据的截取&#xff0c;是的数据导入不是完整的。 所以另辟蹊径使用其他方法 创建一个新的工程用原始的jdbc实现数据的导入 一…