Shiro 框架进阶(四) ----------加盐加密

                                                       Shiro 框架进阶(四)

                                                                                                           ----------加盐加密

 

 

在上一篇自定义Realm当中我们提到了盐(slat)这样一个概念,为什么要有盐这种东西呢?

和我们现实生活当中一样,我们加一点盐才有味道,在shiro当中加盐得目的就是更好得保护用户,即使你是输入得密码是123456,经过shiro得加盐加密,你们存储内容也是唯一的,加盐会将你的密码变得更加的具有唯一性之,下面我们就来对这个盐进行测试:

 

  1. 测试加密算法  盐值加密

如果几个人密码一样,那么加密后的密码则一致。

这样不安全,要解决这个问题,可以在密码上加盐。

一般会选择不重复的值作为盐值,当然一些网站要求你的用户名是不能允许重复的,那么他们就是用用户名来当作盐的。另一种方式就是利用唯一的随机字符串的方式来作为盐,这样就不会阻碍用户去使用自己想用的名字,增加用户的体验;

 

public class MD5Test {

@Test   

public void test(){

        //原始 密码      

 String source = "123456";       

 //盐       

String salt = "helen";        

//散列次数      

 int hashIterations = 1024;

  //构造方法:      

 //第一个参数:散列算法      

 //第二个参数:明文,原始密码      

 //第三个参数:盐,通过使用随机数      

 //第四个参数:散列的次数,比如散列两次,相当 于md5(md5(''))      

//这个加密的方法名不是乱写的,具体要看api,shiro提供了相当丰富的加密 方式,只是你//写的名字必须要和其一致

 SimpleHash simpleHash = new SimpleHash("md5", source, salt, hashIterations); 

 

       String md5 =  simpleHash.toString();        System.out.println(md5);  

 

System.out.println(md5);

}

}

 

  1. 数据源ini配置

shiro­realm­md5.ini 

 

 

[main]

#定义凭证匹配器

credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher

#散列算法 credentialsMatcher.hashAlgorithmName=md5

散列次数 credentialsMatcher.hashIterations=1024

#开启加盐(无需设置,realm中使用的SimpleAuthenticationInfo 是 SaltedAuthenticationInfo 接口的实现类,默认开启的加盐功能) #credentialsMatcher.hashSalted=true

#自定义 realm

customRealm=com.qfedu.shirodemo.realm.CustomRealmMd5 customRealm.credentialsMatcher=$credentialsMatcher

#将realm设置到securityManager,相当 于spring中注入 securityManager.realms=$customRealm

 

  1. 定义realm 盐值加密的密码从前面的测试结果中获取

 

 

public class CustomRealmMd5 extends AuthenticatingRealm{

    @Override   

protected AuthenticationInfo doGetAuthenticationInfo

(AuthenticationToken authenticationToken) throws AuthenticationException {

       

//已知的正确的用户名和密码       

String username = "Sulliven";        

//String password = "123";       

String password = "250d6bc11dd9015cc6557a1e56741002";//加盐加密后的密码         //获取salt       

String salt = username;

       

//获取用户输入:用户名       

String inputUsername = (String)authenticationToken.getPrincipal();     

 

if(!inputUsername.equals(username)){            

throw new UnknownAccountException("用户不存在");     

   }

 

//注意:inputPassword是用户输入的没经过加密,password是数据库中取出的,这里不能直接比较。       

//可以不在这里比较,将密码校验的过程交给shiro处理,也就是后面的SimpleAuthenticationInfo处传递password     

  //也可以先将用户输入加密,然后在这个位置判断密码正确与否。      

 //总结:如果了利用shiro的自动比较功能,则不需要写这段代码

/* String inputPassword =

new String((char[]) authenticationToken.getCredentials());        

 

if(!inputPassword.equals(password)) {            

throw new IncorrectCredentialsException("密码不正确");     

   } */

 

//认证信息AuthenticationInfo(用户名和密码)     

 

 String realmName = getName();//ini中配置的realm的名字       

 

//因为在SimpleAuthenticationInfo当中第三个参数的类型的ByteSource类型的,所以你要//封装还需要对salt进行处理用的就是下这样一个方法

ByteSource byteSalt = ByteSource.Util.bytes(salt);      

 SimpleAuthenticationInfo info =

new SimpleAuthenticationInfo(username, password, byteSalt, realmName);         return info;   

 }

 }

 

  1. 测试:

做的也只是将加载配置文件的名称换一下就行了;

 

以上就是我们对加盐加密的介绍以及测试,下一篇博文我们会讲到授权!!!

敬请期待!!!!!!!!

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_42112846

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值