shiro之编码加密
2017-12-23 17:59
281 查看
一、Shiro 提供了base64和16进制字符串编码/解码的API支持:
二、散列算法
散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5、SHA等。
Shiro 提供了HashService,默认提供了DefaultHashService实现
三、加密、解密
Shiro 还提供对称式加密/解密算法的支持,如AES、Blowfish 等
四、PasswordService/CredentialsMatcher
Shiro 提供了PasswordService及CredentialsMatcher用于提供加密密码及验证密码服务。
Shiro 默认提供了PasswordService 实现DefaultPasswordService;CredentialsMatcher 实现PasswordMatcher及HashedCredentialsMatcher
简单使用:
service层加密密码保存到数据库
登录时realm验证
String str = "h"; //base64 编码 String base64Encoded = Base64.encodeToString(str.getBytes()); //base64 编码解码 String str2 = Base64.decodeToString(base64Encoded); System.out.println(str.equals(str2)); String str3 = "y"; //16进制字符串编码 String hexEncoded = Hex.encodeToString(str3.getBytes()); //16进制字符串解码 String str4 = new String(Hex.decode(hexEncoded)); System.out.println(str3.equals(str4)); //byte数组与String转换 CodecSupport.toBytes(str, "utf-8"); CodecSupport.toString(str.getBytes(), "utf-8");
二、散列算法
散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5、SHA等。
String str = "h"; String salt = "123"; //盐 //单纯md5加密,易破解 System.out.println(new Md5Hash(str).toString()); //加干扰数据,即盐 System.out.println(new Md5Hash(str,salt).toString()); //指定散列次数,如2次::md5(md5(str)) System.out.println(new Md5Hash(str,salt,2).toString()); //SHA256 算法 还有如SHA1、SHA512算法 System.out.println(new Sha256Hash(str,salt).toString()); //shiro通用散列支持,内部使用了Java 的MessageDigest实现 System.out.println(new SimpleHash("md5",str,salt).toString());
Shiro 提供了HashService,默认提供了DefaultHashService实现
//创建一个DefaultHashService,默认使用SHA-512 算法; DefaultHashService hashService = new DefaultHashService(); //修改算法 hashService.setHashAlgorithmName("md5"); //设置一个私盐,其在散列时自动与用户传入的公盐混合产生一个新盐 hashService.setPrivateSalt(new SimpleByteSource("123")); //在用户没有传入公盐的情况下是否生成公盐,默认false hashService.setGeneratePublicSalt(true); //用于生成公盐 hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator()); //修改默认加密迭代次数 hashService.setHashIterations(2); //构建一个HashRequest,传入算法、数据、公盐、迭代次数。 HashRequest request = new HashRequest.Builder().setAlgorithmName("md5") .setSource(ByteSource.Util.bytes("h")) .setSalt(ByteSource.Util.bytes("123")) .setIterations(2).build(); System.out.println(hashService.computeHash(request).toString()); //SecureRandomNumberGenerator用于生成一个随机数 SecureRandomNumberGenerator srng = new SecureRandomNumberGenerator(); srng.setSeed("123".getBytes()); System.out.println(srng.nextBytes().toHex());
三、加密、解密
Shiro 还提供对称式加密/解密算法的支持,如AES、Blowfish 等
//AES算法 AesCipherService acs = new AesCipherService(); acs.setKeySize(128);//key长度 Key key = acs.generateNewKey();//生成key String str = "h"; //加密 String encrptText = acs.encrypt(str.getBytes(), key.getEncoded()).toHex(); System.out.println(encrptText); //解密 String str2 = new String(acs.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes()); System.out.println(str2);
四、PasswordService/CredentialsMatcher
Shiro 提供了PasswordService及CredentialsMatcher用于提供加密密码及验证密码服务。
public interface PasswordService { //输入明文密码得到密文密码 String encryptPassword(Object plaintextPassword) throws IllegalArgumentException; }
public interface CredentialsMatcher { //匹配用户输入的token 的凭证(未加密)与系统提供的凭证(已加密) boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info); }
Shiro 默认提供了PasswordService 实现DefaultPasswordService;CredentialsMatcher 实现PasswordMatcher及HashedCredentialsMatcher
简单使用:
service层加密密码保存到数据库
//加密密码 user.setPassword(passwordService.encryptPassword(user.getPassword()));
登录时realm验证
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo( account,user.getPassword(),this.getName()); System.out.println("匹配=>"+credentialsMatcher.doCredentialsMatch(token,authenticationInfo)); if( !credentialsMatcher.doCredentialsMatch(token,authenticationInfo)){ throw new IncorrectCredentialsException(); }
相关文章推荐
- shiro之编码/加密
- (七)shiro之编码/加密
- shiro之编码/加密
- 跟开涛老师学shiro -- 编码/加密
- shiro中编码/加密
- Shiro学习(5)编码、加密
- shiro编码和加密代码详解
- 第五章 Shiro编码/加密
- Shiro笔记(四)编码/加密
- Shiro学习(5)编码、加密
- shiro编码/加密
- 使用Shiro1.2中的PasswordService实现密码加密存储和比对
- java base64编码 加密和解密(切记注意乱码问题)
- 教你如何解密js/vbs/vbscript加密的编码异处理小结
- shiro中自定义realm实现md5散列算法加密的模拟(二)
- Base64 算法原理,以及编码、解码【加密、解密】 介绍
- 简洁实用的Java Base64编码加密异常处理类代码
- Qt 中实现 hmac Sha1 加密并以 base64编码输出
- Linux工具开发[02]---编程实现对输入字符序列变换(编码/加密/散列)方式的智能判定
- JAVA中Base64编码加密解密源码