java鬼混笔记:shiro 3、shiro下的散列操作(MD5,SHA-1)

来源:互联网 发布:荧光颜料淘宝 编辑:程序博客网 时间:2024/06/03 14:34

这次的笔记是通过shiro进行散列算法操作,常见的两个MD5,SHA-1,直接上代码说明 。

简单的散列操作:

public static void test1() {// 假设是用户输入的密码String password = "123456";// 加入的盐(百度百科有)String salt = "jack";// 这是最原始的MD5加密 (可在网上破解)Md5Hash originalMd5 = new Md5Hash(password);System.out.println(originalMd5.toString());// 输出加密后的密码// 这种也是原始md5加密(可在网上破解)SimpleHash originalMd52 = new SimpleHash("MD5", password);// 构造函数中的 "MD5"的意思是进行md5加密,大小写无关System.out.println(originalMd52.toString());// sha-1 原始加密方法(可在网上破解)SimpleHash originalSha1 = new SimpleHash("SHA-1", password);//System.out.println(originalSha1.toString());// 要加大破解难度,这时候加个'盐'来加密是爽歪歪的// 第1种MD5加盐加密操作Md5Hash newPassword = new Md5Hash(password, salt, 1);// 第三个参数 "1"的意思是循环加密多少次System.out.println(newPassword.toString());// 第2种MD5加盐加密操作SimpleHash sh = new SimpleHash("MD5", password, salt, 1);// 第四个参数 "1"的意思是循环加密多少次System.out.println(sh.toString());// 第2种SHA-1加盐加密操作SimpleHash sh2 = new SimpleHash("SHA-1", password, salt, 1);// 第四个参数 "1"的意思是循环加密多少次System.out.println(sh2.toString());}


Ok,就这个样子 。不过我们平常是得把散列的加密方式配置到ini文件中。配置内容如下:

shiro_hash.ini

[main];认证适配器hashedCredentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher;加密的方式_MD5hashedCredentialsMatcher.hashAlgorithmName=MD5;循环加密多少次hashedCredentialsMatcher.hashIterations=1;自定义的realmHashRealm=com.ywj.TestShiro.HashRealm;自定义的realm的认证适配器HashRealm.credentialsMatcher=$hashedCredentialsMatcher;加入securityManager中securityManager.realms=$HashRealm

自定义的realm:

HashRealm.java

package com.ywj.TestShiro;import org.apache.commons.lang3.StringUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationInfo;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.SimpleAuthenticationInfo;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;import org.apache.shiro.util.ByteSource;public class HashRealm extends AuthorizingRealm {@Overridepublic void setName(String name) {super.setName("HashRealm");// 自定义一个名字}// 认证不关这块的事,先空着先@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {// TODO Auto-generated method stubreturn null;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {// 获取token里的账号String username  = String.valueOf(token.getPrincipal());// 假设这里是逻辑是从数据库获取用户信息不存在if(StringUtils.isBlank(username) || "a".equals(username)) {return null;// 返回一个空,就是没这个用户存在}// 盐String salt = "jack";String password = "4da71e0c7f2fa0ff8a25ecc4f5dab6d4";// 假设这个是数据库里查询出来的用户密码MD5加密后的// 返回认证信息return new SimpleAuthenticationInfo(username, password, ByteSource.Util.bytes(salt), this.getName());}}

测试代码走起
public static void test2() {// 和原来的一样Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:config/shiro_hash.ini");SecurityManager securityManager = factory.getInstance();SecurityUtils.setSecurityManager(securityManager);Subject subject = SecurityUtils.getSubject();UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("ywj", "e10adc3949ba59abbe56e057f20f883e");try {subject.login(usernamePasswordToken);} catch (UnknownAccountException e) {System.out.println("用户不存在");} catch (IncorrectCredentialsException e) {System.out.println("密码不正确");} catch (Exception e) {e.printStackTrace();}boolean flag = subject.isAuthenticated();System.out.println("已登录:" + flag);subject.logout();flag = subject.isAuthenticated();System.out.println("已登录:" + flag);}

OK,,,这些都基本的操作,我自己在网上下载了个hui前端框架,到时把我写的过代码全整进去!

原创粉丝点击