shiro进行散列算法操作

shiro最闪亮的四大特征:认证,权限,加密,会话管理

为了提高应用系统的安全性,这里主要关注shiro提供的密码服务模块;

1.加密工具类的熟悉

首先来个结构图,看看shiro提供了哪些加密工具类:

image

2.通过shiro进行散列算法操作,常见的有两个MD5,SHA-1

简单的散列操作:代码如下

    <span style="font-size:18px;">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());  
        }</span>  

不过我们平常是得把散列的加密方式配置到ini文件中,实际项目上我们不可能把用户的账号和密码写在ini配置文件中,而是通过数据库查询进行处理的,所以要用到自定义,其实就是加上数据库的操作而已。配置文件如下:shiro_hash.ini   注意:ini文件是以;开始的

    <span style="font-size:18px;">[main] 
    ;认证适配器  
    hashedCredentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher  
    ;加密的方式_MD5  
    hashedCredentialsMatcher.hashAlgorithmName=MD5;
    ;循环加密多少次  
    hashedCredentialsMatcher.hashIterations=1自定义的realm 认证 
    HashRealm=com.ywj.TestShiro.HashRealm 
    ;自定义的realm的认证适配器  
    HashRealm.credentialsMatcher=$hashedCredentialsMatcher 
    ;加入securityManager中  
    securityManager.realms=$HashRealm;  
    </span>  

自定义的Realm:

    <span style="font-size:18px;">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 {  
      
          
        @Override  
        public void setName(String name) {  
            super.setName("HashRealm");// 自定义一个名字  
        }  
          
        // 认证不关这块的事,先空着先  
        @Override  
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  
            // TODO Auto-generated method stub  
            return null;  
        }  
      
        @Override  
        protected 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());  
        }  
      
    }<span style="color:#ff6666;">  
    </span></span>  

 http://blog.csdn.net/u013845177/article/details/77620957

posted @ 2018-03-01 15:36  听世界的喧闹。。  阅读(905)  评论(0编辑  收藏  举报